diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index cd045f089d9..938cde89d6c 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -75,7 +75,7 @@ public override ExportResult Export(in Batch batch) Console.WriteLine(msg.ToString()); - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { string valueDisplay = string.Empty; StringBuilder tagsBuilder = new StringBuilder(); diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs index 5ec44df3c16..eafacb2f979 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs @@ -148,7 +148,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) sum.IsMonotonic = true; sum.AggregationTemporality = temporality; - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var dataPoint = new OtlpMetrics.NumberDataPoint { @@ -172,7 +172,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) sum.IsMonotonic = true; sum.AggregationTemporality = temporality; - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var dataPoint = new OtlpMetrics.NumberDataPoint { @@ -193,7 +193,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.LongGauge: { var gauge = new OtlpMetrics.Gauge(); - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var dataPoint = new OtlpMetrics.NumberDataPoint { @@ -214,7 +214,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) case MetricType.DoubleGauge: { var gauge = new OtlpMetrics.Gauge(); - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var dataPoint = new OtlpMetrics.NumberDataPoint { @@ -237,7 +237,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric) var histogram = new OtlpMetrics.Histogram(); histogram.AggregationTemporality = temporality; - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var dataPoint = new OtlpMetrics.HistogramDataPoint { diff --git a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs index 226c45ad559..dc98b92221c 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/Implementation/PrometheusSerializerExt.cs @@ -40,7 +40,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) if (!metric.MetricType.IsHistogram()) { - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var tags = metricPoint.Tags; var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); @@ -103,7 +103,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric) } else { - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { var tags = metricPoint.Tags; var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds(); diff --git a/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs b/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs index 5bd2f1a211a..1fd3cac7cd2 100644 --- a/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs +++ b/src/OpenTelemetry/Metrics/MetricPointsAccessor.cs @@ -76,7 +76,7 @@ internal Enumerator(MetricPoint[] metricsPoints, int[] metricPointsToProcess, lo /// /// Gets the at the current position of the enumerator. /// - public ref MetricPoint Current + public ref readonly MetricPoint Current { get { diff --git a/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs b/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs index ba87cb3adfa..11cbb0fdf0c 100644 --- a/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs +++ b/test/Benchmarks/Metrics/MetricCollectBenchmarks.cs @@ -69,7 +69,7 @@ void ProcessExport(Batch batch) if (this.UseWithRef) { // The performant way of iterating. - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { sum += metricPoint.GetSumDouble(); } diff --git a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs index 995db8daac1..5f5914be28e 100644 --- a/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs @@ -48,7 +48,7 @@ public void InMemoryExporterShouldDeepCopyMetricPoints() var metric = exportedItems[0]; // Only one Metric object is added to the collection at this point var metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric - ref var metricPointForFirstExport = ref metricPointsEnumerator.Current; + ref readonly var metricPointForFirstExport = ref metricPointsEnumerator.Current; Assert.Equal(10, metricPointForFirstExport.GetSumLong()); // Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1") diff --git a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs index d157fbeedd8..edf65d9d75c 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs @@ -58,7 +58,7 @@ public void ObserverCallbackTest() var metric = exportedItems[0]; Assert.Equal("myGauge", metric.Name); List metricPoints = new List(); - foreach (ref var mp in metric.GetMetricPoints()) + foreach (ref readonly var mp in metric.GetMetricPoints()) { metricPoints.Add(mp); } @@ -88,7 +88,7 @@ public void ObserverCallbackExceptionTest() var metric = exportedItems[0]; Assert.Equal("myGauge", metric.Name); List metricPoints = new List(); - foreach (ref var mp in metric.GetMetricPoints()) + foreach (ref readonly var mp in metric.GetMetricPoints()) { metricPoints.Add(mp); } @@ -450,7 +450,7 @@ int MetricPointCount() foreach (var metric in exportedItems) { - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { count++; } @@ -696,7 +696,7 @@ private static long GetLongSum(List metrics) long sum = 0; foreach (var metric in metrics) { - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { if (metric.MetricType.IsSum()) { @@ -717,7 +717,7 @@ private static double GetDoubleSum(List metrics) double sum = 0; foreach (var metric in metrics) { - foreach (ref var metricPoint in metric.GetMetricPoints()) + foreach (ref readonly var metricPoint in metric.GetMetricPoints()) { if (metric.MetricType.IsSum()) { diff --git a/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs b/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs index d3e356072bc..2f76de70efc 100644 --- a/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs @@ -380,7 +380,7 @@ public void ViewToProduceCustomHistogramBound() Assert.Equal("MyHistogram", metricCustom.Name); List metricPointsDefault = new List(); - foreach (ref var mp in metricDefault.GetMetricPoints()) + foreach (ref readonly var mp in metricDefault.GetMetricPoints()) { metricPointsDefault.Add(mp); } @@ -407,7 +407,7 @@ public void ViewToProduceCustomHistogramBound() Assert.Equal(Metric.DefaultHistogramBounds.Length + 1, actualCount); List metricPointsCustom = new List(); - foreach (ref var mp in metricCustom.GetMetricPoints()) + foreach (ref readonly var mp in metricCustom.GetMetricPoints()) { metricPointsCustom.Add(mp); } @@ -465,7 +465,7 @@ public void ViewToSelectTagKeys() var metric = exportedItems[0]; Assert.Equal("NameOnly", metric.Name); List metricPoints = new List(); - foreach (ref var mp in metric.GetMetricPoints()) + foreach (ref readonly var mp in metric.GetMetricPoints()) { metricPoints.Add(mp); } @@ -476,7 +476,7 @@ public void ViewToSelectTagKeys() metric = exportedItems[1]; Assert.Equal("SizeOnly", metric.Name); metricPoints.Clear(); - foreach (ref var mp in metric.GetMetricPoints()) + foreach (ref readonly var mp in metric.GetMetricPoints()) { metricPoints.Add(mp); } @@ -487,7 +487,7 @@ public void ViewToSelectTagKeys() metric = exportedItems[2]; Assert.Equal("NoTags", metric.Name); metricPoints.Clear(); - foreach (ref var mp in metric.GetMetricPoints()) + foreach (ref readonly var mp in metric.GetMetricPoints()) { metricPoints.Add(mp); } diff --git a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs index dc428dc838d..ba08b0644a5 100644 --- a/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs @@ -111,7 +111,7 @@ private void AssertLongSumValueForMetric(Metric metric, long value) var metricPoints = metric.GetMetricPoints(); var metricPointsEnumerator = metricPoints.GetEnumerator(); Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric - ref var metricPointForFirstExport = ref metricPointsEnumerator.Current; + ref readonly var metricPointForFirstExport = ref metricPointsEnumerator.Current; if (metric.MetricType.IsSum()) { Assert.Equal(value, metricPointForFirstExport.GetSumLong());