diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index 95ad61f286d..1e38181b000 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -19,6 +19,9 @@ using Google.Protobuf; using Google.Protobuf.Collections; using OpenTelemetry.Metrics; +using OpenTelemetry.Proto.Metrics.V1; +using AggregationTemporality = OpenTelemetry.Metrics.AggregationTemporality; +using Metric = OpenTelemetry.Metrics.Metric; using OtlpCollector = OpenTelemetry.Proto.Collector.Metrics.V1; using OtlpCommon = OpenTelemetry.Proto.Common.V1; using OtlpMetrics = OpenTelemetry.Proto.Metrics.V1; @@ -28,15 +31,15 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; internal static class MetricItemExtensions { - private static readonly ConcurrentBag MetricListPool = new(); + private static readonly ConcurrentBag MetricListPool = new(); internal static void AddMetrics( this OtlpCollector.ExportMetricsServiceRequest request, OtlpResource.Resource processResource, in Batch metrics) { - var metricsByLibrary = new Dictionary(); - var resourceMetrics = new OtlpMetrics.ResourceMetrics + var metricsByLibrary = new Dictionary(); + var resourceMetrics = new ResourceMetrics { Resource = processResource, }; @@ -77,19 +80,20 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque return; } - foreach (var scope in resourceMetrics.ScopeMetrics) + foreach (var scopeMetrics in resourceMetrics.ScopeMetrics) { - scope.Metrics.Clear(); - MetricListPool.Add(scope); + scopeMetrics.Metrics.Clear(); + scopeMetrics.Scope.Attributes.Clear(); + MetricListPool.Add(scopeMetrics); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable> meterTags) + internal static ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable> meterTags) { - if (!MetricListPool.TryTake(out var metrics)) + if (!MetricListPool.TryTake(out var scopeMetrics)) { - metrics = new OtlpMetrics.ScopeMetrics + scopeMetrics = new ScopeMetrics { Scope = new OtlpCommon.InstrumentationScope { @@ -100,21 +104,20 @@ internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, stri if (meterTags != null) { - AddAttributes(meterTags, metrics.Scope.Attributes); + AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes); } } else { - metrics.Scope.Name = name; - metrics.Scope.Version = version ?? string.Empty; + scopeMetrics.Scope.Name = name; + scopeMetrics.Scope.Version = version ?? string.Empty; if (meterTags != null) { - metrics.Scope.Attributes.Clear(); - AddAttributes(meterTags, metrics.Scope.Attributes); + AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes); } } - return metrics; + return scopeMetrics; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -150,7 +153,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.LongSum: case MetricType.LongSumNonMonotonic: { - var sum = new OtlpMetrics.Sum + var sum = new Sum { IsMonotonic = metric.MetricType == MetricType.LongSum, AggregationTemporality = temporality, @@ -158,7 +161,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.NumberDataPoint + var dataPoint = new NumberDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -177,7 +180,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.DoubleSum: case MetricType.DoubleSumNonMonotonic: { - var sum = new OtlpMetrics.Sum + var sum = new Sum { IsMonotonic = metric.MetricType == MetricType.DoubleSum, AggregationTemporality = temporality, @@ -185,7 +188,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.NumberDataPoint + var dataPoint = new NumberDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -203,10 +206,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.LongGauge: { - var gauge = new OtlpMetrics.Gauge(); + var gauge = new Gauge(); foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.NumberDataPoint + var dataPoint = new NumberDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -224,10 +227,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.DoubleGauge: { - var gauge = new OtlpMetrics.Gauge(); + var gauge = new Gauge(); foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.NumberDataPoint + var dataPoint = new NumberDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -245,14 +248,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.Histogram: { - var histogram = new OtlpMetrics.Histogram + var histogram = new Histogram { AggregationTemporality = temporality, }; foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.HistogramDataPoint + var dataPoint = new HistogramDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -320,14 +323,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.ExponentialHistogram: { - var histogram = new OtlpMetrics.ExponentialHistogram + var histogram = new ExponentialHistogram { AggregationTemporality = temporality, }; foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { - var dataPoint = new OtlpMetrics.ExponentialHistogramDataPoint + var dataPoint = new ExponentialHistogramDataPoint { StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(), TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(), @@ -347,7 +350,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) dataPoint.Scale = exponentialHistogramData.Scale; dataPoint.ZeroCount = (ulong)exponentialHistogramData.ZeroCount; - dataPoint.Positive = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets(); + dataPoint.Positive = new ExponentialHistogramDataPoint.Types.Buckets(); dataPoint.Positive.Offset = exponentialHistogramData.PositiveBuckets.Offset; foreach (var bucketCount in exponentialHistogramData.PositiveBuckets) { @@ -378,7 +381,7 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField> meterTags, RepeatedField attributes) + private static void AddScopeAttributes(IEnumerable> meterTags, RepeatedField attributes) { foreach (var tag in meterTags) {