-
Notifications
You must be signed in to change notification settings - Fork 3.6k
exporter/prometheus: Expose scope name, version, schema URL and attributes as labels #40004
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: bug_fix | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
| component: prometheusexporter | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Expose Scope Name, Version, Schema URL, and Attributes as labels. | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [40004] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: This work is done to comply with https://github.com/open-telemetry/opentelemetry-specification/issues/4223. | ||
|
|
||
| # If your change doesn't affect end users or the exported elements of any package, | ||
| # you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [api] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,10 @@ type accumulatedValue struct { | |
| // updated indicates when metric was last changed. | ||
| updated time.Time | ||
|
|
||
| scope pcommon.InstrumentationScope | ||
| scopeName string | ||
| scopeVersion string | ||
| scopeSchemaURL string | ||
| scopeAttributes pcommon.Map | ||
| } | ||
|
|
||
| // accumulator stores aggregated values of incoming metrics | ||
|
|
@@ -35,7 +38,7 @@ type accumulator interface { | |
| Accumulate(resourceMetrics pmetric.ResourceMetrics) (processed int) | ||
| // Collect returns a slice with relevant aggregated metrics and their resource attributes. | ||
| // The number or metrics and attributes returned will be the same. | ||
| Collect() (metrics []pmetric.Metric, resourceAttrs []pcommon.Map) | ||
| Collect() (metrics []pmetric.Metric, resourceAttrs []pcommon.Map, scopeNames []string, scopeVersions []string, scopeSchemaURLs []string, scopeAttributes []pcommon.Map) | ||
| } | ||
|
|
||
| // LastValueAccumulator keeps last value for accumulated metrics | ||
|
|
@@ -68,25 +71,25 @@ func (a *lastValueAccumulator) Accumulate(rm pmetric.ResourceMetrics) (n int) { | |
|
|
||
| metrics := ilm.Metrics() | ||
| for j := 0; j < metrics.Len(); j++ { | ||
| n += a.addMetric(metrics.At(j), ilm.Scope(), resourceAttrs, now) | ||
| n += a.addMetric(metrics.At(j), ilm.Scope().Name(), ilm.Scope().Version(), ilm.SchemaUrl(), ilm.Scope().Attributes(), resourceAttrs, now) | ||
| } | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func (a *lastValueAccumulator) addMetric(metric pmetric.Metric, il pcommon.InstrumentationScope, resourceAttrs pcommon.Map, now time.Time) int { | ||
| func (a *lastValueAccumulator) addMetric(metric pmetric.Metric, scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, resourceAttrs pcommon.Map, now time.Time) int { | ||
| a.logger.Debug(fmt.Sprintf("accumulating metric: %s", metric.Name())) | ||
|
|
||
| switch metric.Type() { | ||
| case pmetric.MetricTypeGauge: | ||
| return a.accumulateGauge(metric, il, resourceAttrs, now) | ||
| return a.accumulateGauge(metric, scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, resourceAttrs, now) | ||
| case pmetric.MetricTypeSum: | ||
| return a.accumulateSum(metric, il, resourceAttrs, now) | ||
| return a.accumulateSum(metric, scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, resourceAttrs, now) | ||
| case pmetric.MetricTypeHistogram: | ||
| return a.accumulateHistogram(metric, il, resourceAttrs, now) | ||
| return a.accumulateHistogram(metric, scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, resourceAttrs, now) | ||
| case pmetric.MetricTypeSummary: | ||
| return a.accumulateSummary(metric, il, resourceAttrs, now) | ||
| return a.accumulateSummary(metric, scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, resourceAttrs, now) | ||
| default: | ||
| a.logger.With( | ||
| zap.String("data_type", string(metric.Type())), | ||
|
|
@@ -97,12 +100,12 @@ func (a *lastValueAccumulator) addMetric(metric pmetric.Metric, il pcommon.Instr | |
| return 0 | ||
| } | ||
|
|
||
| func (a *lastValueAccumulator) accumulateSummary(metric pmetric.Metric, il pcommon.InstrumentationScope, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| func (a *lastValueAccumulator) accumulateSummary(metric pmetric.Metric, scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| dps := metric.Summary().DataPoints() | ||
| for i := 0; i < dps.Len(); i++ { | ||
| ip := dps.At(i) | ||
|
|
||
| signature := timeseriesSignature(il.Name(), metric, ip.Attributes(), resourceAttrs) | ||
| signature := timeseriesSignature(scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, metric, ip.Attributes(), resourceAttrs) | ||
| if ip.Flags().NoRecordedValue() { | ||
| a.registeredMetrics.Delete(signature) | ||
| return 0 | ||
|
|
@@ -119,19 +122,19 @@ func (a *lastValueAccumulator) accumulateSummary(metric pmetric.Metric, il pcomm | |
|
|
||
| m := copyMetricMetadata(metric) | ||
| ip.CopyTo(m.SetEmptySummary().DataPoints().AppendEmpty()) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| } | ||
|
|
||
| return n | ||
| } | ||
|
|
||
| func (a *lastValueAccumulator) accumulateGauge(metric pmetric.Metric, il pcommon.InstrumentationScope, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| func (a *lastValueAccumulator) accumulateGauge(metric pmetric.Metric, scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| dps := metric.Gauge().DataPoints() | ||
| for i := 0; i < dps.Len(); i++ { | ||
| ip := dps.At(i) | ||
|
|
||
| signature := timeseriesSignature(il.Name(), metric, ip.Attributes(), resourceAttrs) | ||
| signature := timeseriesSignature(scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, metric, ip.Attributes(), resourceAttrs) | ||
| if ip.Flags().NoRecordedValue() { | ||
| a.registeredMetrics.Delete(signature) | ||
| return 0 | ||
|
|
@@ -141,7 +144,7 @@ func (a *lastValueAccumulator) accumulateGauge(metric pmetric.Metric, il pcommon | |
| if !ok { | ||
| m := copyMetricMetadata(metric) | ||
| ip.CopyTo(m.SetEmptyGauge().DataPoints().AppendEmpty()) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| continue | ||
| } | ||
|
|
@@ -154,13 +157,13 @@ func (a *lastValueAccumulator) accumulateGauge(metric pmetric.Metric, il pcommon | |
|
|
||
| m := copyMetricMetadata(metric) | ||
| ip.CopyTo(m.SetEmptyGauge().DataPoints().AppendEmpty()) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (a *lastValueAccumulator) accumulateSum(metric pmetric.Metric, il pcommon.InstrumentationScope, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| func (a *lastValueAccumulator) accumulateSum(metric pmetric.Metric, scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| doubleSum := metric.Sum() | ||
|
|
||
| // Drop metrics with unspecified aggregations | ||
|
|
@@ -177,7 +180,7 @@ func (a *lastValueAccumulator) accumulateSum(metric pmetric.Metric, il pcommon.I | |
| for i := 0; i < dps.Len(); i++ { | ||
| ip := dps.At(i) | ||
|
|
||
| signature := timeseriesSignature(il.Name(), metric, ip.Attributes(), resourceAttrs) | ||
| signature := timeseriesSignature(scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, metric, ip.Attributes(), resourceAttrs) | ||
| if ip.Flags().NoRecordedValue() { | ||
| a.registeredMetrics.Delete(signature) | ||
| return 0 | ||
|
|
@@ -189,7 +192,7 @@ func (a *lastValueAccumulator) accumulateSum(metric pmetric.Metric, il pcommon.I | |
| m.SetEmptySum().SetIsMonotonic(metric.Sum().IsMonotonic()) | ||
| m.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
| ip.CopyTo(m.Sum().DataPoints().AppendEmpty()) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| continue | ||
| } | ||
|
|
@@ -215,21 +218,21 @@ func (a *lastValueAccumulator) accumulateSum(metric pmetric.Metric, il pcommon.I | |
| m.SetEmptySum().SetIsMonotonic(metric.Sum().IsMonotonic()) | ||
| m.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
| ip.CopyTo(m.Sum().DataPoints().AppendEmpty()) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (a *lastValueAccumulator) accumulateHistogram(metric pmetric.Metric, il pcommon.InstrumentationScope, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| func (a *lastValueAccumulator) accumulateHistogram(metric pmetric.Metric, scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, resourceAttrs pcommon.Map, now time.Time) (n int) { | ||
| histogram := metric.Histogram() | ||
| a.logger.Debug("Accumulate histogram.....") | ||
| dps := histogram.DataPoints() | ||
|
|
||
| for i := 0; i < dps.Len(); i++ { | ||
| ip := dps.At(i) | ||
|
|
||
| signature := timeseriesSignature(il.Name(), metric, ip.Attributes(), resourceAttrs) // uniquely identify this time series you are accumulating for | ||
| signature := timeseriesSignature(scopeName, scopeVersion, scopeSchemaURL, scopeAttributes, metric, ip.Attributes(), resourceAttrs) // uniquely identify this time series you are accumulating for | ||
| if ip.Flags().NoRecordedValue() { | ||
| a.registeredMetrics.Delete(signature) | ||
| return 0 | ||
|
|
@@ -241,7 +244,7 @@ func (a *lastValueAccumulator) accumulateHistogram(metric pmetric.Metric, il pco | |
| m := copyMetricMetadata(metric) | ||
| ip.CopyTo(m.SetEmptyHistogram().DataPoints().AppendEmpty()) | ||
| m.Histogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| continue | ||
| } | ||
|
|
@@ -284,18 +287,22 @@ func (a *lastValueAccumulator) accumulateHistogram(metric pmetric.Metric, il pco | |
| // unsupported temporality | ||
| continue | ||
| } | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scope: il, updated: now}) | ||
| a.registeredMetrics.Store(signature, &accumulatedValue{value: m, resourceAttrs: resourceAttrs, scopeName: scopeName, scopeVersion: scopeVersion, scopeSchemaURL: scopeSchemaURL, scopeAttributes: scopeAttributes, updated: now}) | ||
| n++ | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Collect returns a slice with relevant aggregated metrics and their resource attributes. | ||
| func (a *lastValueAccumulator) Collect() ([]pmetric.Metric, []pcommon.Map) { | ||
| func (a *lastValueAccumulator) Collect() ([]pmetric.Metric, []pcommon.Map, []string, []string, []string, []pcommon.Map) { | ||
| a.logger.Debug("Accumulator collect called") | ||
|
|
||
| var metrics []pmetric.Metric | ||
| var resourceAttrs []pcommon.Map | ||
| var scopeNames []string | ||
| var scopeVersions []string | ||
| var scopeSchemaURLs []string | ||
| var scopeAttributes []pcommon.Map | ||
| expirationTime := time.Now().Add(-a.metricExpiration) | ||
|
|
||
| a.registeredMetrics.Range(func(key, value any) bool { | ||
|
|
@@ -308,23 +315,38 @@ func (a *lastValueAccumulator) Collect() ([]pmetric.Metric, []pcommon.Map) { | |
|
|
||
| metrics = append(metrics, v.value) | ||
| resourceAttrs = append(resourceAttrs, v.resourceAttrs) | ||
| scopeNames = append(scopeNames, v.scopeName) | ||
| scopeVersions = append(scopeVersions, v.scopeVersion) | ||
| scopeSchemaURLs = append(scopeSchemaURLs, v.scopeSchemaURL) | ||
| scopeAttributes = append(scopeAttributes, v.scopeAttributes) | ||
| return true | ||
| }) | ||
|
|
||
| return metrics, resourceAttrs | ||
| return metrics, resourceAttrs, scopeNames, scopeVersions, scopeSchemaURLs, scopeAttributes | ||
| } | ||
|
|
||
| func timeseriesSignature(ilmName string, metric pmetric.Metric, attributes pcommon.Map, resourceAttrs pcommon.Map) string { | ||
| func timeseriesSignature(scopeName string, scopeVersion string, scopeSchemaURL string, scopeAttributes pcommon.Map, metric pmetric.Metric, attributes pcommon.Map, resourceAttrs pcommon.Map) string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't block this PR, but we are building massive strings to compute signatures here (contains all resource and metric attributes). That is probably using a huge amount of memory. We should strongly consider using something like internal/exp/metrics/identity.OfResourceMetric instead of computing large strings.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, moving to the identity package would also solve the string collision problem. Would you be okay with me working on those in a follow-up PR? That way I don't need to solve the same problem twice :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i'm OK with a follow-up |
||
| var b strings.Builder | ||
| b.WriteString(metric.Type().String()) | ||
| b.WriteString("*" + ilmName) | ||
| b.WriteString("*" + metric.Name()) | ||
| attrs := make([]string, 0, attributes.Len()) | ||
| b.WriteString(metric.Type().String()) | ||
| b.WriteString("*" + scopeName) | ||
| b.WriteString("*" + scopeVersion) | ||
| b.WriteString("*" + scopeSchemaURL) | ||
|
|
||
| attrs := make([]string, 0, scopeAttributes.Len()) | ||
| for k, v := range scopeAttributes.All() { | ||
| attrs = append(attrs, k+"*"+v.AsString()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this uses the same delimiter |
||
| } | ||
| sort.Strings(attrs) | ||
| b.WriteString("*" + strings.Join(attrs, "*")) | ||
|
|
||
| attrs = make([]string, 0, attributes.Len()) | ||
| for k, v := range attributes.All() { | ||
| attrs = append(attrs, k+"*"+v.AsString()) | ||
| } | ||
| sort.Strings(attrs) | ||
| b.WriteString("*" + strings.Join(attrs, "*")) | ||
|
|
||
| if job, ok := extractJob(resourceAttrs); ok { | ||
| b.WriteString("*" + model.JobLabel + "*" + job) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.