diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index 4bd9772b217..9df0212f91c 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -186,6 +186,17 @@ public override ExportResult Export(in Batch batch) valueDisplay = metricPoint.GetGaugeLastValueLong().ToString(CultureInfo.InvariantCulture); } } + else if (metricType.IsDecimal()) + { + if (metricType.IsSum()) + { + valueDisplay = metricPoint.GetSumDecimal().ToString(CultureInfo.InvariantCulture); + } + else + { + valueDisplay = metricPoint.GetGaugeLastValueDecimal().ToString(CultureInfo.InvariantCulture); + } + } var exemplarString = new StringBuilder(); if (metricPoint.TryGetExemplars(out var exemplars)) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index 07ed2ddbf64..d2031280eba 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -212,6 +212,43 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) break; } + case MetricType.DecimalSum: + case MetricType.DecimalSumNonMonotonic: + { + var sum = new Sum + { + IsMonotonic = metric.MetricType == MetricType.DecimalSum, + AggregationTemporality = temporality, + }; + + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) + { + var dataPoint = new NumberDataPoint + { + StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + }; + + AddAttributes(metricPoint.Tags, dataPoint.Attributes); + + dataPoint.AsDouble = (double)metricPoint.GetSumDecimal(); + + if (metricPoint.TryGetExemplars(out var exemplars)) + { + foreach (ref readonly var exemplar in exemplars) + { + dataPoint.Exemplars.Add( + ToOtlpExemplar(exemplar.DoubleValue, in exemplar)); + } + } + + sum.DataPoints.Add(dataPoint); + } + + otlpMetric.Sum = sum; + break; + } + case MetricType.LongGauge: { var gauge = new Gauge(); @@ -274,6 +311,37 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) break; } + case MetricType.DecimalGauge: + { + var gauge = new Gauge(); + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) + { + var dataPoint = new NumberDataPoint + { + StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), + TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), + }; + + AddAttributes(metricPoint.Tags, dataPoint.Attributes); + + dataPoint.AsDouble = (double)metricPoint.GetGaugeLastValueDecimal(); + + if (metricPoint.TryGetExemplars(out var exemplars)) + { + foreach (ref readonly var exemplar in exemplars) + { + dataPoint.Exemplars.Add( + ToOtlpExemplar(exemplar.DoubleValue, in exemplar)); + } + } + + gauge.DataPoints.Add(dataPoint); + } + + otlpMetric.Gauge = gauge; + break; + } + case MetricType.Histogram: { var histogram = new Histogram diff --git a/src/OpenTelemetry/.publicApi/Stable/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/Stable/PublicAPI.Unshipped.txt index 4fc9635e9f7..94d4c5430d9 100644 --- a/src/OpenTelemetry/.publicApi/Stable/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/Stable/PublicAPI.Unshipped.txt @@ -31,3 +31,4 @@ OpenTelemetry.ReadOnlyFilteredTagCollection.Enumerator.MoveNext() -> bool OpenTelemetry.ReadOnlyFilteredTagCollection.GetEnumerator() -> OpenTelemetry.ReadOnlyFilteredTagCollection.Enumerator OpenTelemetry.ReadOnlyFilteredTagCollection.ReadOnlyFilteredTagCollection() -> void static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.SetExemplarFilter(this OpenTelemetry.Metrics.MeterProviderBuilder! meterProviderBuilder, OpenTelemetry.Metrics.ExemplarFilterType exemplarFilter) -> OpenTelemetry.Metrics.MeterProviderBuilder! +static OpenTelemetry.Metrics.MetricTypeExtensions.IsDecimal(this OpenTelemetry.Metrics.MetricType self) -> bool diff --git a/src/OpenTelemetry/Metrics/MetricTypeExtensions.cs b/src/OpenTelemetry/Metrics/MetricTypeExtensions.cs index c21b1be3dd2..61f2cf4de59 100644 --- a/src/OpenTelemetry/Metrics/MetricTypeExtensions.cs +++ b/src/OpenTelemetry/Metrics/MetricTypeExtensions.cs @@ -95,4 +95,17 @@ public static bool IsLong(this MetricType self) { return (self & POINT_KIND_MASK) == POINT_KIND_I8; } + + /// + /// Determines if the supplied is a decimal definition. + /// + /// . + /// if the supplied + /// is a decimal definition. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsDecimal(this MetricType self) + { + MetricType mask = MetricType.DecimalSum | MetricType.DecimalSumNonMonotonic | MetricType.DecimalGauge; + return (self & POINT_KIND_MASK) == (mask & POINT_KIND_MASK); + } }