forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricSnapshotTests.cs
More file actions
185 lines (160 loc) · 7.79 KB
/
MetricSnapshotTests.cs
File metadata and controls
185 lines (160 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// <copyright file="MetricSnapshotTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Diagnostics.Metrics;
using OpenTelemetry.Tests;
using Xunit;
namespace OpenTelemetry.Metrics.Tests
{
public class MetricSnapshotTests
{
[Fact]
public void VerifySnapshot_Counter()
{
var exportedMetrics = new List<Metric>();
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();
// FIRST EXPORT
counter.Add(10);
meterProvider.ForceFlush();
// Verify Metric 1
Assert.Single(exportedMetrics);
var metric1 = exportedMetrics[0];
var metricPoints1Enumerator = metric1.GetMetricPoints().GetEnumerator();
Assert.True(metricPoints1Enumerator.MoveNext());
ref readonly var metricPoint1 = ref metricPoints1Enumerator.Current;
Assert.Equal(10, metricPoint1.GetSumLong());
// Verify Snapshot 1
Assert.Single(exportedSnapshots);
var snapshot1 = exportedSnapshots[0];
Assert.Single(snapshot1.MetricPoints);
Assert.Equal(10, snapshot1.MetricPoints[0].GetSumLong());
// Verify Metric == Snapshot
Assert.Equal(metric1.Name, snapshot1.Name);
Assert.Equal(metric1.Description, snapshot1.Description);
Assert.Equal(metric1.Unit, snapshot1.Unit);
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]
public void VerifySnapshot_Histogram()
{
var exportedMetrics = new List<Metric>();
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();
// FIRST EXPORT
histogram.Record(10);
meterProvider.ForceFlush();
// Verify Metric 1
Assert.Single(exportedMetrics);
var metric1 = exportedMetrics[0];
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());
metricPoint1.TryGetHistogramMinMaxValues(out var min, out var max);
Assert.Equal(10, min);
Assert.Equal(10, max);
// Verify Snapshot 1
Assert.Single(exportedSnapshots);
var snapshot1 = exportedSnapshots[0];
Assert.Single(snapshot1.MetricPoints);
Assert.Equal(1, snapshot1.MetricPoints[0].GetHistogramCount());
Assert.Equal(10, snapshot1.MetricPoints[0].GetHistogramSum());
snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(10, min);
Assert.Equal(10, max);
// Verify Metric == Snapshot
Assert.Equal(metric1.Name, snapshot1.Name);
Assert.Equal(metric1.Description, snapshot1.Description);
Assert.Equal(metric1.Unit, snapshot1.Unit);
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());
metricPoint1.TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(5, min);
Assert.Equal(10, max);
// 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());
metricPoint2.TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(5, min);
Assert.Equal(10, max);
// 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());
snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(10, min);
Assert.Equal(10, max);
// 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());
snapshot2.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(5, min);
Assert.Equal(10, max);
}
}
}