-
Notifications
You must be signed in to change notification settings - Fork 879
Add exemplar support to Prometheus exporter #5929
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8550c31
Add exemplar support to Prometheus exporter
saul 606b69e
Update README
saul 24b2c6a
Merge branch 'main' into aspnet-prom-exemplars
saul 4261b1d
Merge branch 'main' into aspnet-prom-exemplars
cijothomas bb24629
Merge branch 'main' into aspnet-prom-exemplars
Kielek 725a730
Merge branch 'main' into aspnet-prom-exemplars
Kielek bf24a25
remove non-ascii signs from comment
Kielek 0b0e001
Fix CHANGELOG
Kielek 9cac3e7
Fix event source test
saul 86cb1c7
Merge branch 'main' into aspnet-prom-exemplars
Kielek f336968
Merge branch 'main' into aspnet-prom-exemplars
saul e17a2af
Merge branch 'main' into aspnet-prom-exemplars
Kielek 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
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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry.Metrics; | ||
|
|
||
| namespace OpenTelemetry.Exporter.Prometheus; | ||
|
|
@@ -28,8 +29,11 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
| cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric, openMetricsRequested); | ||
| cursor = WriteHelpMetadata(buffer, cursor, prometheusMetric, metric.Description, openMetricsRequested); | ||
|
|
||
| var isLong = metric.MetricType.IsLong(); | ||
| if (!metric.MetricType.IsHistogram()) | ||
| { | ||
| var isSum = metric.MetricType.IsSum(); | ||
|
|
||
| foreach (ref readonly var metricPoint in metric.GetMetricPoints()) | ||
| { | ||
| var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); | ||
|
|
@@ -40,12 +44,9 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
|
|
||
| buffer[cursor++] = unchecked((byte)' '); | ||
|
|
||
| // TODO: MetricType is same for all MetricPoints | ||
| // within a given Metric, so this check can avoided | ||
| // for each MetricPoint | ||
| if (((int)metric.MetricType & 0b_0000_1111) == 0x0a /* I8 */) | ||
| if (isLong) | ||
| { | ||
| if (metric.MetricType.IsSum()) | ||
| if (isSum) | ||
| { | ||
| cursor = WriteLong(buffer, cursor, metricPoint.GetSumLong()); | ||
| } | ||
|
|
@@ -56,7 +57,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
| } | ||
| else | ||
| { | ||
| if (metric.MetricType.IsSum()) | ||
| if (isSum) | ||
| { | ||
| cursor = WriteDouble(buffer, cursor, metricPoint.GetSumDouble()); | ||
| } | ||
|
|
@@ -70,16 +71,27 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
|
|
||
| cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested); | ||
|
|
||
| if (isSum && openMetricsRequested && metricPoint.TryGetExemplars(out var exemplarCollection)) | ||
| { | ||
| cursor = WriteSumExemplar(buffer, cursor, metric, exemplarCollection); | ||
| } | ||
|
|
||
| buffer[cursor++] = ASCII_LINEFEED; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Debug.Assert(!isLong, "Expected histogram metric to be of type `double`"); | ||
|
|
||
| foreach (ref readonly var metricPoint in metric.GetMetricPoints()) | ||
| { | ||
| var tags = metricPoint.Tags; | ||
| var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); | ||
|
|
||
| metricPoint.TryGetExemplars(out var exemplarCollection); | ||
saul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var exemplars = exemplarCollection.GetEnumerator(); | ||
| var hasExemplar = exemplars.MoveNext(); | ||
|
|
||
| long totalCount = 0; | ||
| foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets()) | ||
| { | ||
|
|
@@ -107,6 +119,19 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
|
|
||
| cursor = WriteTimestamp(buffer, cursor, timestamp, openMetricsRequested); | ||
|
|
||
| if (hasExemplar && openMetricsRequested) | ||
| { | ||
| if (exemplars.Current.DoubleValue <= histogramMeasurement.ExplicitBound) | ||
| { | ||
| cursor = WriteExemplar(buffer, cursor, exemplars.Current, metric.Name, isLong: false); | ||
| } | ||
|
|
||
| while (hasExemplar && exemplars.Current.DoubleValue <= histogramMeasurement.ExplicitBound) | ||
| { | ||
| hasExemplar = exemplars.MoveNext(); | ||
| } | ||
| } | ||
|
|
||
| buffer[cursor++] = ASCII_LINEFEED; | ||
| } | ||
|
|
||
|
|
@@ -142,4 +167,99 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe | |
|
|
||
| return cursor; | ||
| } | ||
|
|
||
| private static int WriteSumExemplar( | ||
| byte[] buffer, | ||
| int cursor, | ||
| in Metric metric, | ||
| in ReadOnlyExemplarCollection exemplarCollection) | ||
| { | ||
| var exemplars = exemplarCollection.GetEnumerator(); | ||
| if (!exemplars.MoveNext()) | ||
| { | ||
| return cursor; | ||
| } | ||
|
|
||
| ref readonly Exemplar maxExemplar = ref exemplars.Current; | ||
| var isLong = metric.MetricType.IsLong(); | ||
|
|
||
| while (exemplars.MoveNext()) | ||
| { | ||
| if (isLong) | ||
| { | ||
| if (exemplars.Current.LongValue >= maxExemplar.LongValue) | ||
| { | ||
| maxExemplar = ref exemplars.Current; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (exemplars.Current.DoubleValue >= maxExemplar.DoubleValue) | ||
| { | ||
| maxExemplar = ref exemplars.Current; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return WriteExemplar(buffer, cursor, maxExemplar, metric.Name, isLong); | ||
| } | ||
|
|
||
| private static int WriteExemplar(byte[] buffer, int cursor, in Exemplar exemplar, string metricName, bool isLong) | ||
|
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. Should this go into |
||
| { | ||
| buffer[cursor++] = unchecked((byte)' '); | ||
| buffer[cursor++] = unchecked((byte)'#'); | ||
| buffer[cursor++] = unchecked((byte)' '); | ||
|
|
||
| buffer[cursor++] = unchecked((byte)'{'); | ||
| var labelSetCursorStart = cursor; | ||
| cursor = WriteAsciiStringNoEscape(buffer, cursor, "trace_id=\""); | ||
| cursor = WriteAsciiStringNoEscape(buffer, cursor, exemplar.TraceId.ToHexString()); | ||
| cursor = WriteAsciiStringNoEscape(buffer, cursor, "\",span_id=\""); | ||
| cursor = WriteAsciiStringNoEscape(buffer, cursor, exemplar.SpanId.ToHexString()); | ||
| buffer[cursor++] = unchecked((byte)'"'); | ||
| buffer[cursor++] = unchecked((byte)','); | ||
|
|
||
| var labelSetWritten = cursor - labelSetCursorStart - 8; | ||
|
|
||
| var tagResetCursor = cursor; | ||
|
|
||
| foreach (var tag in exemplar.FilteredTags) | ||
| { | ||
| var prevCursor = cursor; | ||
| cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value); | ||
|
|
||
| // From the spec: | ||
| // Other characters in the text rendering of an exemplar such as ",= are not included in this limit | ||
| // for implementation simplicity and for consistency between the text and proto formats. | ||
| labelSetWritten += cursor - prevCursor - 3; // subtract 2 x " and 1 x = character | ||
|
|
||
| buffer[cursor++] = unchecked((byte)','); | ||
|
|
||
| // From the spec: | ||
| // The combined length of the label names and values of an Exemplar's LabelSet MUST NOT exceed 128 UTF-8 character code points. | ||
| if (labelSetWritten > 128) | ||
| { | ||
| cursor = tagResetCursor; | ||
| PrometheusExporterEventSource.Log.ExemplarTagsTooLong(metricName); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra. | ||
| buffer[cursor++] = unchecked((byte)' '); | ||
|
|
||
| if (isLong) | ||
| { | ||
| cursor = WriteLong(buffer, cursor, exemplar.LongValue); | ||
| } | ||
| else | ||
| { | ||
| cursor = WriteDouble(buffer, cursor, exemplar.DoubleValue); | ||
| } | ||
|
|
||
| buffer[cursor++] = unchecked((byte)' '); | ||
| cursor = WriteTimestamp(buffer, cursor, exemplar.Timestamp.ToUnixTimeMilliseconds(), useOpenMetrics: true); | ||
|
|
||
| return cursor; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.