Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve test coverage: InMemoryExporter SnapshotMetric #3344

Merged
merged 11 commits into from
Jun 17, 2022
Prev Previous commit
Next Next commit
add additional tests
  • Loading branch information
TimothyMothra committed Jun 13, 2022
commit 7fb697f032ff387d281ca17344657567a48ae820
90 changes: 73 additions & 17 deletions test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ public void VerifySnapshot_Counter()
var exportedSnapshots = new List<MetricSnapshot>();

using var meter = new Meter(Utils.GetCurrentMethodName());
var counter = meter.CreateCounter<long>("meter");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedMetrics)
.AddInMemoryExporter(exportedSnapshots)
.Build();

var counter = meter.CreateCounter<long>("meter");
// FIRST EXPORT
counter.Add(10);

meterProvider.ForceFlush();

// Verify Metric
// Verify Metric 1
Assert.Single(exportedMetrics);
var metric1 = exportedMetrics[0];
var metricPointsEnumerator = metric1.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext());
ref readonly var metricPointForFirstExport = ref metricPointsEnumerator.Current;
Assert.Equal(10, metricPointForFirstExport.GetSumLong());
var metricPoints1Enumerator = metric1.GetMetricPoints().GetEnumerator();
Assert.True(metricPoints1Enumerator.MoveNext());
ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current;
Assert.Equal(10, metricPoint1.GetSumLong());

// Verify Snapshot
// Verify Snapshot 1
Assert.Single(exportedSnapshots);
var snapshot1 = exportedSnapshots[0];
Assert.Single(snapshot1.MetricPoints);
Expand All @@ -64,6 +64,32 @@ public void VerifySnapshot_Counter()
Assert.Equal(metric1.MeterName, snapshot1.MeterName);
Assert.Equal(metric1.MetricType, snapshot1.MetricType);
Assert.Equal(metric1.MeterVersion, snapshot1.MeterVersion);

// SECOND EXPORT
counter.Add(5);
meterProvider.ForceFlush();

// Verify Metric 1, after second export
// This value is expected to be updated.
Assert.Equal(15, metricPoint1.GetSumLong());

// Verify Metric 2
Assert.Equal(2, exportedMetrics.Count);
var metric2 = exportedMetrics[1];
var metricPoints2Enumerator = metric2.GetMetricPoints().GetEnumerator();
Assert.True(metricPoints2Enumerator.MoveNext());
ref readonly var metricPoint2 = ref metricPoints2Enumerator.Current;
Assert.Equal(15, metricPoint2.GetSumLong());

// Verify Snapshot 1, after second export
// This value is expected to be unchanged.
Assert.Equal(10, snapshot1.MetricPoints[0].GetSumLong());

// Verify Snapshot 2
Assert.Equal(2, exportedSnapshots.Count);
var snapshot2 = exportedSnapshots[1];
Assert.Single(snapshot2.MetricPoints);
Assert.Equal(15, snapshot2.MetricPoints[0].GetSumLong());
}

[Fact]
Expand All @@ -73,27 +99,27 @@ public void VerifySnapshot_Histogram()
var exportedSnapshots = new List<MetricSnapshot>();

using var meter = new Meter(Utils.GetCurrentMethodName());
var histogram = meter.CreateHistogram<int>("histogram");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedMetrics)
.AddInMemoryExporter(exportedSnapshots)
.Build();

var histogram = meter.CreateHistogram<int>("histogram");
// FIRST EXPORT
histogram.Record(10);

meterProvider.ForceFlush();

// Verify Metric
// Verify Metric 1
Assert.Single(exportedMetrics);
var metric1 = exportedMetrics[0];
var metricPointsEnumerator = metric1.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext());
ref readonly var metricPointForFirstExport = ref metricPointsEnumerator.Current;
Assert.Equal(1, metricPointForFirstExport.GetHistogramCount());
Assert.Equal(10, metricPointForFirstExport.GetHistogramSum());
var metricPoints1Enumerator = metric1.GetMetricPoints().GetEnumerator();
Assert.True(metricPoints1Enumerator.MoveNext());
ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current;
Assert.Equal(1, metricPoint1.GetHistogramCount());
Assert.Equal(10, metricPoint1.GetHistogramSum());

// Verify Snapshot
// Verify Snapshot 1
Assert.Single(exportedSnapshots);
var snapshot1 = exportedSnapshots[0];
Assert.Single(snapshot1.MetricPoints);
Expand All @@ -107,6 +133,36 @@ public void VerifySnapshot_Histogram()
Assert.Equal(metric1.MeterName, snapshot1.MeterName);
Assert.Equal(metric1.MetricType, snapshot1.MetricType);
Assert.Equal(metric1.MeterVersion, snapshot1.MeterVersion);

// SECOND EXPORT
histogram.Record(5);
meterProvider.ForceFlush();

// Verify Metric 1 after second export
// This value is expected to be updated.
Assert.Equal(2, metricPoint1.GetHistogramCount());
Assert.Equal(15, metricPoint1.GetHistogramSum());

// Verify Metric 2
Assert.Equal(2, exportedMetrics.Count);
var metric2 = exportedMetrics[1];
var metricPoints2Enumerator = metric2.GetMetricPoints().GetEnumerator();
Assert.True(metricPoints2Enumerator.MoveNext());
ref readonly var metricPoint2 = ref metricPoints2Enumerator.Current;
Assert.Equal(2, metricPoint2.GetHistogramCount());
Assert.Equal(15, metricPoint2.GetHistogramSum());

// Verify Snapshot 1 after second export
// This value is expected to be unchanged.
Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount());
Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum());

// Verify Snapshot 2
Assert.Equal(2, exportedSnapshots.Count);
var snapshot2 = exportedSnapshots[1];
Assert.Single(snapshot2.MetricPoints);
Assert.Equal(2, snapshot2.MetricPoints[0].GetHistogramCount());
Assert.Equal(15, snapshot2.MetricPoints[0].GetHistogramSum());
}
}
}