Skip to content

Commit 05b0ca4

Browse files
authored
[otlp-metrics] Support exemplars for all metric types (#5397)
1 parent 278e246 commit 05b0ca4

File tree

3 files changed

+279
-47
lines changed

3 files changed

+279
-47
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
as it is mandated by the specification.
3131
([#5316](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5268))
3232

33+
* **Experimental (pre-release builds only):** Add support in
34+
`OtlpMetricExporter` for emitting exemplars supplied on Counters, Gauges, and
35+
ExponentialHistograms.
36+
([#5397](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5397))
37+
3338
## 1.7.0
3439

3540
Released 2023-Dec-08

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
158158
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
159159

160160
dataPoint.AsInt = metricPoint.GetSumLong();
161+
162+
if (metricPoint.TryGetExemplars(out var exemplars))
163+
{
164+
foreach (ref readonly var exemplar in exemplars)
165+
{
166+
dataPoint.Exemplars.Add(
167+
ToOtlpExemplar(exemplar.LongValue, in exemplar));
168+
}
169+
}
170+
161171
sum.DataPoints.Add(dataPoint);
162172
}
163173

@@ -185,6 +195,16 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
185195
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
186196

187197
dataPoint.AsDouble = metricPoint.GetSumDouble();
198+
199+
if (metricPoint.TryGetExemplars(out var exemplars))
200+
{
201+
foreach (ref readonly var exemplar in exemplars)
202+
{
203+
dataPoint.Exemplars.Add(
204+
ToOtlpExemplar(exemplar.DoubleValue, in exemplar));
205+
}
206+
}
207+
188208
sum.DataPoints.Add(dataPoint);
189209
}
190210

@@ -206,6 +226,16 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
206226
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
207227

208228
dataPoint.AsInt = metricPoint.GetGaugeLastValueLong();
229+
230+
if (metricPoint.TryGetExemplars(out var exemplars))
231+
{
232+
foreach (ref readonly var exemplar in exemplars)
233+
{
234+
dataPoint.Exemplars.Add(
235+
ToOtlpExemplar(exemplar.LongValue, in exemplar));
236+
}
237+
}
238+
209239
gauge.DataPoints.Add(dataPoint);
210240
}
211241

@@ -227,6 +257,16 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
227257
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
228258

229259
dataPoint.AsDouble = metricPoint.GetGaugeLastValueDouble();
260+
261+
if (metricPoint.TryGetExemplars(out var exemplars))
262+
{
263+
foreach (ref readonly var exemplar in exemplars)
264+
{
265+
dataPoint.Exemplars.Add(
266+
ToOtlpExemplar(exemplar.DoubleValue, in exemplar));
267+
}
268+
}
269+
230270
gauge.DataPoints.Add(dataPoint);
231271
}
232272

@@ -320,7 +360,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
320360
dataPoint.Positive.BucketCounts.Add((ulong)bucketCount);
321361
}
322362

323-
// TODO: exemplars.
363+
if (metricPoint.TryGetExemplars(out var exemplars))
364+
{
365+
foreach (ref readonly var exemplar in exemplars)
366+
{
367+
dataPoint.Exemplars.Add(
368+
ToOtlpExemplar(exemplar.DoubleValue, in exemplar));
369+
}
370+
}
324371

325372
histogram.DataPoints.Add(dataPoint);
326373
}
@@ -333,29 +380,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
333380
return otlpMetric;
334381
}
335382

336-
private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<OtlpCommon.KeyValue> attributes)
337-
{
338-
foreach (var tag in tags)
339-
{
340-
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
341-
{
342-
attributes.Add(result);
343-
}
344-
}
345-
}
346-
347-
private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
348-
{
349-
foreach (var tag in meterTags)
350-
{
351-
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
352-
{
353-
attributes.Add(result);
354-
}
355-
}
356-
}
357-
358-
private static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exemplar exemplar)
383+
internal static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exemplar exemplar)
359384
where T : struct
360385
{
361386
var otlpExemplar = new OtlpMetrics.Exemplar
@@ -399,4 +424,26 @@ private static OtlpMetrics.Exemplar ToOtlpExemplar<T>(T value, in Metrics.Exempl
399424

400425
return otlpExemplar;
401426
}
427+
428+
private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<OtlpCommon.KeyValue> attributes)
429+
{
430+
foreach (var tag in tags)
431+
{
432+
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
433+
{
434+
attributes.Add(result);
435+
}
436+
}
437+
}
438+
439+
private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
440+
{
441+
foreach (var tag in meterTags)
442+
{
443+
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var result))
444+
{
445+
attributes.Add(result);
446+
}
447+
}
448+
}
402449
}

0 commit comments

Comments
 (0)