Skip to content

Commit

Permalink
Make MetricPoint access from MetricPointAccessor readonly (#2736)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Dec 14, 2021
1 parent 1e45e09 commit 33f3596
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override ExportResult Export(in Batch<Metric> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/MetricPointsAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal Enumerator(MetricPoint[] metricsPoints, int[] metricPointsToProcess, lo
/// <summary>
/// Gets the <see cref="MetricPoint"/> at the current position of the enumerator.
/// </summary>
public ref MetricPoint Current
public ref readonly MetricPoint Current
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion test/Benchmarks/Metrics/MetricCollectBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void ProcessExport(Batch<Metric> 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();
}
Expand Down
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Tests/Metrics/InMemoryExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions test/OpenTelemetry.Tests/Metrics/MetricAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void ObserverCallbackTest()
var metric = exportedItems[0];
Assert.Equal("myGauge", metric.Name);
List<MetricPoint> metricPoints = new List<MetricPoint>();
foreach (ref var mp in metric.GetMetricPoints())
foreach (ref readonly var mp in metric.GetMetricPoints())
{
metricPoints.Add(mp);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public void ObserverCallbackExceptionTest()
var metric = exportedItems[0];
Assert.Equal("myGauge", metric.Name);
List<MetricPoint> metricPoints = new List<MetricPoint>();
foreach (ref var mp in metric.GetMetricPoints())
foreach (ref readonly var mp in metric.GetMetricPoints())
{
metricPoints.Add(mp);
}
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -696,7 +696,7 @@ private static long GetLongSum(List<Metric> 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())
{
Expand All @@ -717,7 +717,7 @@ private static double GetDoubleSum(List<Metric> 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())
{
Expand Down
10 changes: 5 additions & 5 deletions test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public void ViewToProduceCustomHistogramBound()
Assert.Equal("MyHistogram", metricCustom.Name);

List<MetricPoint> metricPointsDefault = new List<MetricPoint>();
foreach (ref var mp in metricDefault.GetMetricPoints())
foreach (ref readonly var mp in metricDefault.GetMetricPoints())
{
metricPointsDefault.Add(mp);
}
Expand All @@ -407,7 +407,7 @@ public void ViewToProduceCustomHistogramBound()
Assert.Equal(Metric.DefaultHistogramBounds.Length + 1, actualCount);

List<MetricPoint> metricPointsCustom = new List<MetricPoint>();
foreach (ref var mp in metricCustom.GetMetricPoints())
foreach (ref readonly var mp in metricCustom.GetMetricPoints())
{
metricPointsCustom.Add(mp);
}
Expand Down Expand Up @@ -465,7 +465,7 @@ public void ViewToSelectTagKeys()
var metric = exportedItems[0];
Assert.Equal("NameOnly", metric.Name);
List<MetricPoint> metricPoints = new List<MetricPoint>();
foreach (ref var mp in metric.GetMetricPoints())
foreach (ref readonly var mp in metric.GetMetricPoints())
{
metricPoints.Add(mp);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Tests/Metrics/MultipleReadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 33f3596

Please sign in to comment.