Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/elasticsearch] Use metrics builder for resource metrics #9757

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
### 💡 Enhancements 💡

- `cmd/mdatagen`: Replace enum attributes values with typed constants (#9683)
- `elasticsearchreceiver`: Update metrics scope name from `otelcol/elasticsearch`
to `otelcol/elasticsearchreceiver` (#9757)
- `k8sclusterreceiver`: Validate that k8s API supports a resource before setting up a watcher for it (#9523)
- `internal/stanza`: Add support for `remove` operator (#9524)
- `k8sattributesprocessor`: Support regex capture groups in tag_name (#9525)
Expand Down
9 changes: 7 additions & 2 deletions receiver/elasticsearchreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ metrics:
enabled: <true|false>
```

## Resource attributes

| Name | Description | Type |
| ---- | ----------- | ---- |
| elasticsearch.cluster.name | The name of the elasticsearch cluster. | String |
| elasticsearch.node.name | The name of the elasticsearch node. | String |

## Metric attributes

| Name | Description | Values |
Expand All @@ -56,8 +63,6 @@ metrics:
| direction | The direction of network data. | received, sent |
| disk_usage_state (state) | The state of a section of space on disk. | used, free |
| document_state (state) | The state of the document. | active, deleted |
| elasticsearch.cluster.name | The name of the elasticsearch cluster. | |
| elasticsearch.node.name | The name of the elasticsearch node. | |
| fs_direction (direction) | The direction of filesystem IO. | read, write |
| health_status (status) | The health status of the cluster. | green, yellow, red |
| memory_pool_name (name) | The name of the JVM memory pool. | |
Expand Down
52 changes: 0 additions & 52 deletions receiver/elasticsearchreceiver/internal/metadata/emitters.go

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion receiver/elasticsearchreceiver/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
name: elasticsearchreceiver

attributes:
resource_attributes:
elasticsearch.cluster.name:
description: The name of the elasticsearch cluster.
type: string
elasticsearch.node.name:
description: The name of the elasticsearch node.
type: string

attributes:
cache_name:
description: The name of cache.
enum:
Expand Down
35 changes: 8 additions & 27 deletions receiver/elasticsearchreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/elasticsearchreceiver/internal/metadata"
)

const instrumentationLibraryName = "otelcol/elasticsearch"

var errUnknownClusterStatus = errors.New("unknown cluster status")

type elasticsearchScraper struct {
Expand All @@ -56,21 +54,18 @@ func (r *elasticsearchScraper) start(_ context.Context, host component.Host) (er
}

func (r *elasticsearchScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
metrics := pmetric.NewMetrics()
rms := metrics.ResourceMetrics()

errs := &scrapererror.ScrapeErrors{}

now := pcommon.NewTimestampFromTime(time.Now())

r.scrapeNodeMetrics(ctx, now, rms, errs)
r.scrapeClusterMetrics(ctx, now, rms, errs)
r.scrapeNodeMetrics(ctx, now, errs)
r.scrapeClusterMetrics(ctx, now, errs)

return metrics, errs.Combine()
return r.mb.Emit(), errs.Combine()
}

// scrapeNodeMetrics scrapes adds node-level metrics to the given MetricSlice from the NodeStats endpoint
func (r *elasticsearchScraper) scrapeNodeMetrics(ctx context.Context, now pcommon.Timestamp, rms pmetric.ResourceMetricsSlice, errs *scrapererror.ScrapeErrors) {
func (r *elasticsearchScraper) scrapeNodeMetrics(ctx context.Context, now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
if len(r.cfg.Nodes) == 0 {
return
}
Expand All @@ -82,14 +77,6 @@ func (r *elasticsearchScraper) scrapeNodeMetrics(ctx context.Context, now pcommo
}

for _, info := range nodeStats.Nodes {
rm := rms.AppendEmpty()
resourceAttrs := rm.Resource().Attributes()
resourceAttrs.InsertString(metadata.A.ElasticsearchClusterName, nodeStats.ClusterName)
resourceAttrs.InsertString(metadata.A.ElasticsearchNodeName, info.Name)

ilms := rm.ScopeMetrics().AppendEmpty()
ilms.Scope().SetName(instrumentationLibraryName)

r.mb.RecordElasticsearchNodeCacheMemoryUsageDataPoint(now, info.Indices.FieldDataCache.MemorySizeInBy, metadata.AttributeCacheNameFielddata)
r.mb.RecordElasticsearchNodeCacheMemoryUsageDataPoint(now, info.Indices.QueryCache.MemorySizeInBy, metadata.AttributeCacheNameQuery)

Expand Down Expand Up @@ -171,11 +158,12 @@ func (r *elasticsearchScraper) scrapeNodeMetrics(ctx context.Context, now pcommo

r.mb.RecordJvmThreadsCountDataPoint(now, info.JVMInfo.JVMThreadInfo.Count)

r.mb.EmitNodeMetrics(ilms.Metrics())
r.mb.EmitForResource(metadata.WithElasticsearchClusterName(nodeStats.ClusterName),
metadata.WithElasticsearchNodeName(info.Name))
}
}

func (r *elasticsearchScraper) scrapeClusterMetrics(ctx context.Context, now pcommon.Timestamp, rms pmetric.ResourceMetricsSlice, errs *scrapererror.ScrapeErrors) {
func (r *elasticsearchScraper) scrapeClusterMetrics(ctx context.Context, now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
if r.cfg.SkipClusterMetrics {
return
}
Expand All @@ -186,13 +174,6 @@ func (r *elasticsearchScraper) scrapeClusterMetrics(ctx context.Context, now pco
return
}

rm := rms.AppendEmpty()
resourceAttrs := rm.Resource().Attributes()
resourceAttrs.InsertString(metadata.A.ElasticsearchClusterName, clusterHealth.ClusterName)

ilms := rm.ScopeMetrics().AppendEmpty()
ilms.Scope().SetName(instrumentationLibraryName)

r.mb.RecordElasticsearchClusterNodesDataPoint(now, clusterHealth.NodeCount)

r.mb.RecordElasticsearchClusterDataNodesDataPoint(now, clusterHealth.DataNodeCount)
Expand All @@ -219,5 +200,5 @@ func (r *elasticsearchScraper) scrapeClusterMetrics(ctx context.Context, now pco
errs.AddPartial(1, fmt.Errorf("health status %s: %w", clusterHealth.Status, errUnknownClusterStatus))
}

r.mb.EmitClusterMetrics(ilms.Metrics())
r.mb.EmitForResource(metadata.WithElasticsearchClusterName(clusterHealth.ClusterName))
}