-
Notifications
You must be signed in to change notification settings - Fork 971
Limit exemplar label characters to conform to Prometheus limits #8362
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
Open
anuq
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
anuq:fix/prometheus-exemplar-label-length-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,14 @@ | |
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.KeyValue; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.TraceFlags; | ||
| import io.opentelemetry.api.trace.TraceState; | ||
| import io.opentelemetry.sdk.common.InstrumentationScopeInfo; | ||
| import io.opentelemetry.sdk.metrics.data.AggregationTemporality; | ||
| import io.opentelemetry.sdk.metrics.data.MetricData; | ||
| import io.opentelemetry.sdk.metrics.data.MetricDataType; | ||
| import io.opentelemetry.sdk.metrics.internal.data.ImmutableDoubleExemplarData; | ||
| import io.opentelemetry.sdk.metrics.internal.data.ImmutableDoublePointData; | ||
| import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramBuckets; | ||
| import io.opentelemetry.sdk.metrics.internal.data.ImmutableExponentialHistogramData; | ||
|
|
@@ -39,6 +43,7 @@ | |
| import io.opentelemetry.sdk.resources.Resource; | ||
| import io.prometheus.metrics.expositionformats.ExpositionFormats; | ||
| import io.prometheus.metrics.model.snapshots.CounterSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.Labels; | ||
| import io.prometheus.metrics.model.snapshots.MetricSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.MetricSnapshots; | ||
|
|
@@ -555,4 +560,136 @@ void validateCacheIsBounded() { | |
| // it never saw those resources before. | ||
| assertThat(predicateCalledCount.get()).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
|
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. Quick Question - would it be possible to convert these added unit tests into parameterized tests? |
||
| void exemplarLabelsWithinLimit() { | ||
| SpanContext spanContext = | ||
| SpanContext.create( | ||
| "00000000000000000000000000000001", | ||
| "0000000000000001", | ||
| TraceFlags.getSampled(), | ||
| TraceState.getDefault()); | ||
| ImmutableDoubleExemplarData exemplar = | ||
| (ImmutableDoubleExemplarData) | ||
| ImmutableDoubleExemplarData.create( | ||
| Attributes.of(stringKey("short"), "val"), 1000L, spanContext, 1.0); | ||
|
|
||
| MetricData metricData = | ||
| ImmutableMetricData.createDoubleGauge( | ||
| Resource.getDefault(), | ||
| InstrumentationScopeInfo.create("test"), | ||
| "my.gauge", | ||
| "desc", | ||
| "unit", | ||
| ImmutableGaugeData.create( | ||
| Collections.singletonList( | ||
| ImmutableDoublePointData.create( | ||
| 0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar))))); | ||
|
|
||
| MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData)); | ||
| assertThat(snapshots).isNotNull(); | ||
| // Labels within limit — both trace/span and filtered attribute should be present | ||
| GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0); | ||
| Labels exemplarLabels = point.getExemplar().getLabels(); | ||
| assertThat(exemplarLabels.get("trace_id")).isEqualTo(spanContext.getTraceId()); | ||
| assertThat(exemplarLabels.get("span_id")).isEqualTo(spanContext.getSpanId()); | ||
| assertThat(exemplarLabels.get("short")).isEqualTo("val"); | ||
| } | ||
|
|
||
| @Test | ||
| void exemplarLabelsExceedingLimitDropsFilteredAttributes() { | ||
| SpanContext spanContext = | ||
| SpanContext.create( | ||
| "00000000000000000000000000000001", | ||
| "0000000000000001", | ||
| TraceFlags.getSampled(), | ||
| TraceState.getDefault()); | ||
| // Build a filtered attribute whose name+value alone would push total over 128 | ||
| char[] chars = new char[100]; | ||
| Arrays.fill(chars, 'x'); | ||
| String longValue = new String(chars); | ||
| ImmutableDoubleExemplarData exemplar = | ||
| (ImmutableDoubleExemplarData) | ||
| ImmutableDoubleExemplarData.create( | ||
| Attributes.of(stringKey("long_attr"), longValue), 1000L, spanContext, 1.0); | ||
|
|
||
| MetricData metricData = | ||
| ImmutableMetricData.createDoubleGauge( | ||
| Resource.getDefault(), | ||
| InstrumentationScopeInfo.create("test"), | ||
| "my.gauge", | ||
| "desc", | ||
| "unit", | ||
| ImmutableGaugeData.create( | ||
| Collections.singletonList( | ||
| ImmutableDoublePointData.create( | ||
| 0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar))))); | ||
|
|
||
| MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData)); | ||
| assertThat(snapshots).isNotNull(); | ||
| GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0); | ||
| Labels exemplarLabels = point.getExemplar().getLabels(); | ||
| // trace_id and span_id are preserved | ||
| assertThat(exemplarLabels.get("trace_id")).isEqualTo(spanContext.getTraceId()); | ||
| assertThat(exemplarLabels.get("span_id")).isEqualTo(spanContext.getSpanId()); | ||
| // filtered attribute is dropped to stay within limit | ||
| assertThat(exemplarLabels.get("long_attr")).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void exemplarWithoutSpanContextExceedingLimitDropsFilteredAttributes() { | ||
| char[] chars = new char[150]; | ||
| Arrays.fill(chars, 'x'); | ||
| String longValue = new String(chars); | ||
| ImmutableDoubleExemplarData exemplar = | ||
| (ImmutableDoubleExemplarData) | ||
| ImmutableDoubleExemplarData.create( | ||
| Attributes.of(stringKey("long_attr"), longValue), | ||
| 1000L, | ||
| SpanContext.getInvalid(), | ||
| 1.0); | ||
|
|
||
| MetricData metricData = | ||
| ImmutableMetricData.createDoubleGauge( | ||
| Resource.getDefault(), | ||
| InstrumentationScopeInfo.create("test"), | ||
| "my.gauge", | ||
| "desc", | ||
| "unit", | ||
| ImmutableGaugeData.create( | ||
| Collections.singletonList( | ||
| ImmutableDoublePointData.create( | ||
| 0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar))))); | ||
|
|
||
| MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData)); | ||
| GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0); | ||
| Labels exemplarLabels = point.getExemplar().getLabels(); | ||
| // No span context means no trace_id/span_id, and oversized filtered attr is dropped | ||
| assertThat(exemplarLabels.size()).isZero(); | ||
| } | ||
|
|
||
| @Test | ||
| void exemplarWithoutSpanContextWithinLimit() { | ||
| ImmutableDoubleExemplarData exemplar = | ||
| (ImmutableDoubleExemplarData) | ||
| ImmutableDoubleExemplarData.create( | ||
| Attributes.of(stringKey("short"), "val"), 1000L, SpanContext.getInvalid(), 1.0); | ||
|
|
||
| MetricData metricData = | ||
| ImmutableMetricData.createDoubleGauge( | ||
| Resource.getDefault(), | ||
| InstrumentationScopeInfo.create("test"), | ||
| "my.gauge", | ||
| "desc", | ||
| "unit", | ||
| ImmutableGaugeData.create( | ||
| Collections.singletonList( | ||
| ImmutableDoublePointData.create( | ||
| 0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar))))); | ||
|
|
||
| MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData)); | ||
| GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0); | ||
| Labels exemplarLabels = point.getExemplar().getLabels(); | ||
| assertThat(exemplarLabels.get("short")).isEqualTo("val"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the OpenMetrics Exemplar spec, I think this function should be using
codePointCountinstead oflength. This was also used in a previous PR addressing the same issue.That PR already had reviews. Not sure if you've looked through that PR (it seems it has been inactive for a while now).