Skip to content

Commit 6053c03

Browse files
author
Timothy Mothra
authored
improve test coverage: InMemoryExporter SnapshotMetric (#3344)
1 parent fd24811 commit 6053c03

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// <copyright file="MetricSnapshotTests.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System.Collections.Generic;
18+
using System.Diagnostics.Metrics;
19+
20+
using OpenTelemetry.Tests;
21+
22+
using Xunit;
23+
24+
namespace OpenTelemetry.Metrics.Tests
25+
{
26+
public class MetricSnapshotTests
27+
{
28+
[Fact]
29+
public void VerifySnapshot_Counter()
30+
{
31+
var exportedMetrics = new List<Metric>();
32+
var exportedSnapshots = new List<MetricSnapshot>();
33+
34+
using var meter = new Meter(Utils.GetCurrentMethodName());
35+
var counter = meter.CreateCounter<long>("meter");
36+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
37+
.AddMeter(meter.Name)
38+
.AddInMemoryExporter(exportedMetrics)
39+
.AddInMemoryExporter(exportedSnapshots)
40+
.Build();
41+
42+
// FIRST EXPORT
43+
counter.Add(10);
44+
meterProvider.ForceFlush();
45+
46+
// Verify Metric 1
47+
Assert.Single(exportedMetrics);
48+
var metric1 = exportedMetrics[0];
49+
var metricPoints1Enumerator = metric1.GetMetricPoints().GetEnumerator();
50+
Assert.True(metricPoints1Enumerator.MoveNext());
51+
ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current;
52+
Assert.Equal(10, metricPoint1.GetSumLong());
53+
54+
// Verify Snapshot 1
55+
Assert.Single(exportedSnapshots);
56+
var snapshot1 = exportedSnapshots[0];
57+
Assert.Single(snapshot1.MetricPoints);
58+
Assert.Equal(10, snapshot1.MetricPoints[0].GetSumLong());
59+
60+
// Verify Metric == Snapshot
61+
Assert.Equal(metric1.Name, snapshot1.Name);
62+
Assert.Equal(metric1.Description, snapshot1.Description);
63+
Assert.Equal(metric1.Unit, snapshot1.Unit);
64+
Assert.Equal(metric1.MeterName, snapshot1.MeterName);
65+
Assert.Equal(metric1.MetricType, snapshot1.MetricType);
66+
Assert.Equal(metric1.MeterVersion, snapshot1.MeterVersion);
67+
68+
// SECOND EXPORT
69+
counter.Add(5);
70+
meterProvider.ForceFlush();
71+
72+
// Verify Metric 1, after second export
73+
// This value is expected to be updated.
74+
Assert.Equal(15, metricPoint1.GetSumLong());
75+
76+
// Verify Metric 2
77+
Assert.Equal(2, exportedMetrics.Count);
78+
var metric2 = exportedMetrics[1];
79+
var metricPoints2Enumerator = metric2.GetMetricPoints().GetEnumerator();
80+
Assert.True(metricPoints2Enumerator.MoveNext());
81+
ref readonly var metricPoint2 = ref metricPoints2Enumerator.Current;
82+
Assert.Equal(15, metricPoint2.GetSumLong());
83+
84+
// Verify Snapshot 1, after second export
85+
// This value is expected to be unchanged.
86+
Assert.Equal(10, snapshot1.MetricPoints[0].GetSumLong());
87+
88+
// Verify Snapshot 2
89+
Assert.Equal(2, exportedSnapshots.Count);
90+
var snapshot2 = exportedSnapshots[1];
91+
Assert.Single(snapshot2.MetricPoints);
92+
Assert.Equal(15, snapshot2.MetricPoints[0].GetSumLong());
93+
}
94+
95+
[Fact]
96+
public void VerifySnapshot_Histogram()
97+
{
98+
var exportedMetrics = new List<Metric>();
99+
var exportedSnapshots = new List<MetricSnapshot>();
100+
101+
using var meter = new Meter(Utils.GetCurrentMethodName());
102+
var histogram = meter.CreateHistogram<int>("histogram");
103+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
104+
.AddMeter(meter.Name)
105+
.AddInMemoryExporter(exportedMetrics)
106+
.AddInMemoryExporter(exportedSnapshots)
107+
.Build();
108+
109+
// FIRST EXPORT
110+
histogram.Record(10);
111+
meterProvider.ForceFlush();
112+
113+
// Verify Metric 1
114+
Assert.Single(exportedMetrics);
115+
var metric1 = exportedMetrics[0];
116+
var metricPoints1Enumerator = metric1.GetMetricPoints().GetEnumerator();
117+
Assert.True(metricPoints1Enumerator.MoveNext());
118+
ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current;
119+
Assert.Equal(1, metricPoint1.GetHistogramCount());
120+
Assert.Equal(10, metricPoint1.GetHistogramSum());
121+
122+
// Verify Snapshot 1
123+
Assert.Single(exportedSnapshots);
124+
var snapshot1 = exportedSnapshots[0];
125+
Assert.Single(snapshot1.MetricPoints);
126+
Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount());
127+
Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum());
128+
129+
// Verify Metric == Snapshot
130+
Assert.Equal(metric1.Name, snapshot1.Name);
131+
Assert.Equal(metric1.Description, snapshot1.Description);
132+
Assert.Equal(metric1.Unit, snapshot1.Unit);
133+
Assert.Equal(metric1.MeterName, snapshot1.MeterName);
134+
Assert.Equal(metric1.MetricType, snapshot1.MetricType);
135+
Assert.Equal(metric1.MeterVersion, snapshot1.MeterVersion);
136+
137+
// SECOND EXPORT
138+
histogram.Record(5);
139+
meterProvider.ForceFlush();
140+
141+
// Verify Metric 1 after second export
142+
// This value is expected to be updated.
143+
Assert.Equal(2, metricPoint1.GetHistogramCount());
144+
Assert.Equal(15, metricPoint1.GetHistogramSum());
145+
146+
// Verify Metric 2
147+
Assert.Equal(2, exportedMetrics.Count);
148+
var metric2 = exportedMetrics[1];
149+
var metricPoints2Enumerator = metric2.GetMetricPoints().GetEnumerator();
150+
Assert.True(metricPoints2Enumerator.MoveNext());
151+
ref readonly var metricPoint2 = ref metricPoints2Enumerator.Current;
152+
Assert.Equal(2, metricPoint2.GetHistogramCount());
153+
Assert.Equal(15, metricPoint2.GetHistogramSum());
154+
155+
// Verify Snapshot 1 after second export
156+
// This value is expected to be unchanged.
157+
Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount());
158+
Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum());
159+
160+
// Verify Snapshot 2
161+
Assert.Equal(2, exportedSnapshots.Count);
162+
var snapshot2 = exportedSnapshots[1];
163+
Assert.Single(snapshot2.MetricPoints);
164+
Assert.Equal(2, snapshot2.MetricPoints[0].GetHistogramCount());
165+
Assert.Equal(15, snapshot2.MetricPoints[0].GetHistogramSum());
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)