Skip to content

Commit 2aae6e1

Browse files
authored
[tests] Clean up class structure in metrics tests (#5960)
1 parent 6044583 commit 2aae6e1

8 files changed

+17
-163
lines changed

test/OpenTelemetry.Extensions.Hosting.Tests/OpenTelemetry.Extensions.Hosting.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
<!-- Note: These are SDK tests which we link and run here using the
2323
IMetricsBuilder/IMetricsListener API added at the host level in .NET 8
2424
instead of the direct lower-level MeterListener API added in .NET 6. -->
25-
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\AggregatorTestsBase.cs" Link="Includes\Metrics\AggregatorTestsBase.cs" />
25+
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\AggregatorTests.cs" Link="Includes\Metrics\AggregatorTests.cs" />
2626
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\KnownHistogramBuckets.cs" Link="Includes\Metrics\KnownHistogramBuckets.cs" />
27-
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricApiTestsBase.cs" Link="Includes\Metrics\MetricApiTestsBase.cs" />
27+
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricApiTests.cs" Link="Includes\Metrics\MetricApiTests.cs" />
2828
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricExemplarTests.cs" Link="Includes\Metrics\MetricExemplarTests.cs" />
2929
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricTestData.cs" Link="Includes\Metrics\MetricTestData.cs" />
3030
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricTestsBase.cs" Link="Includes\Metrics\MetricTestsBase.cs" />

test/OpenTelemetry.Tests/Metrics/AggregatorTestsBase.cs renamed to test/OpenTelemetry.Tests/Metrics/AggregatorTests.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
namespace OpenTelemetry.Metrics.Tests;
99

10-
#pragma warning disable SA1402
11-
12-
public abstract class AggregatorTestsBase
10+
public class AggregatorTests
1311
{
1412
private static readonly Meter Meter = new("testMeter");
1513
private static readonly Instrument Instrument = Meter.CreateHistogram<long>("testInstrument");
@@ -18,7 +16,7 @@ public abstract class AggregatorTestsBase
1816

1917
private readonly AggregatorStore aggregatorStore;
2018

21-
protected AggregatorTestsBase()
19+
public AggregatorTests()
2220
{
2321
this.aggregatorStore = new(MetricStreamIdentity, AggregationType.HistogramWithBuckets, AggregationTemporality.Cumulative, 1024);
2422
}
@@ -510,11 +508,3 @@ public ThreadArguments(MetricPoint histogramPoint, ManualResetEvent mreToEnsureA
510508
}
511509
}
512510
}
513-
514-
public class AggregatorTests : AggregatorTestsBase
515-
{
516-
public AggregatorTests()
517-
: base()
518-
{
519-
}
520-
}

test/OpenTelemetry.Tests/Metrics/MetricApiTestsBase.cs renamed to test/OpenTelemetry.Tests/Metrics/MetricApiTests.cs

+2-22
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.Metrics;
6-
using Microsoft.Extensions.Configuration;
76
using OpenTelemetry.Exporter;
87
using OpenTelemetry.Internal;
98
using OpenTelemetry.Tests;
@@ -12,9 +11,7 @@
1211

1312
namespace OpenTelemetry.Metrics.Tests;
1413

15-
#pragma warning disable SA1402
16-
17-
public abstract class MetricApiTestsBase : MetricTestsBase
14+
public class MetricApiTests : MetricTestsBase
1815
{
1916
private const int MaxTimeToAllowForFlush = 10000;
2017
private static readonly int NumberOfThreads = Environment.ProcessorCount;
@@ -23,8 +20,7 @@ public abstract class MetricApiTestsBase : MetricTestsBase
2320
private static readonly int NumberOfMetricUpdateByEachThread = 100000;
2421
private readonly ITestOutputHelper output;
2522

26-
protected MetricApiTestsBase(ITestOutputHelper output)
27-
: base(BuildConfiguration())
23+
public MetricApiTests(ITestOutputHelper output)
2824
{
2925
this.output = output;
3026
}
@@ -1703,14 +1699,6 @@ public void GaugeHandlesNoNewMeasurementsCorrectlyWithTemporality(MetricReaderTe
17031699
}
17041700
}
17051701

1706-
internal static IConfiguration BuildConfiguration()
1707-
{
1708-
var configurationData = new Dictionary<string, string?>();
1709-
return new ConfigurationBuilder()
1710-
.AddInMemoryCollection(configurationData)
1711-
.Build();
1712-
}
1713-
17141702
private static void CounterUpdateThread<T>(object? obj)
17151703
where T : struct, IComparable
17161704
{
@@ -1878,11 +1866,3 @@ public UpdateThreadArguments(ManualResetEvent mreToBlockUpdateThread, ManualRese
18781866
}
18791867
}
18801868
}
1881-
1882-
public class MetricApiTest : MetricApiTestsBase
1883-
{
1884-
public MetricApiTest(ITestOutputHelper output)
1885-
: base(output)
1886-
{
1887-
}
1888-
}

test/OpenTelemetry.Tests/Metrics/MetricOverflowAttributeTestsBase.cs renamed to test/OpenTelemetry.Tests/Metrics/MetricOverflowAttributeTests.cs

+1-34
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics.Metrics;
5-
using Microsoft.Extensions.Configuration;
6-
using Microsoft.Extensions.DependencyInjection;
75
using OpenTelemetry.Tests;
86
using Xunit;
97

108
namespace OpenTelemetry.Metrics.Tests;
119

12-
#pragma warning disable SA1402
13-
14-
public abstract class MetricOverflowAttributeTestsBase
10+
public class MetricOverflowAttributeTests
1511
{
16-
private readonly Dictionary<string, string?> configurationData = new()
17-
{
18-
};
19-
20-
private readonly IConfiguration configuration;
21-
22-
public MetricOverflowAttributeTestsBase()
23-
{
24-
this.configuration = new ConfigurationBuilder()
25-
.AddInMemoryCollection(this.configurationData)
26-
.Build();
27-
}
28-
2912
[Theory]
3013
[InlineData(MetricReaderTemporalityPreference.Delta)]
3114
[InlineData(MetricReaderTemporalityPreference.Cumulative)]
@@ -37,10 +20,6 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem
3720
var counter = meter.CreateCounter<long>("TestCounter");
3821

3922
using var meterProvider = Sdk.CreateMeterProviderBuilder()
40-
.ConfigureServices(services =>
41-
{
42-
services.AddSingleton(this.configuration);
43-
})
4423
.AddMeter(meter.Name)
4524
.AddInMemoryExporter(exportedItems, metricReaderOptions => metricReaderOptions.TemporalityPreference = temporalityPreference)
4625
.Build();
@@ -181,10 +160,6 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
181160
var histogram = meter.CreateHistogram<long>("TestHistogram");
182161

183162
using var meterProvider = Sdk.CreateMeterProviderBuilder()
184-
.ConfigureServices(services =>
185-
{
186-
services.AddSingleton(this.configuration);
187-
})
188163
.AddMeter(meter.Name)
189164
.AddInMemoryExporter(exportedItems, metricReaderOptions => metricReaderOptions.TemporalityPreference = temporalityPreference)
190165
.Build();
@@ -318,11 +293,3 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
318293
}
319294
}
320295
}
321-
322-
public class MetricOverflowAttributeTests : MetricOverflowAttributeTestsBase
323-
{
324-
public MetricOverflowAttributeTests()
325-
: base()
326-
{
327-
}
328-
}

test/OpenTelemetry.Tests/Metrics/MetricPointReclaimTestsBase.cs renamed to test/OpenTelemetry.Tests/Metrics/MetricPointReclaimTests.cs

+1-34
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics.Metrics;
5-
using Microsoft.Extensions.Configuration;
6-
using Microsoft.Extensions.DependencyInjection;
75
using OpenTelemetry.Tests;
86
using Xunit;
97

108
namespace OpenTelemetry.Metrics.Tests;
119

12-
#pragma warning disable SA1402
13-
14-
public abstract class MetricPointReclaimTestsBase
10+
public class MetricPointReclaimTests
1511
{
16-
private readonly Dictionary<string, string?> configurationData = new()
17-
{
18-
};
19-
20-
private readonly IConfiguration configuration;
21-
22-
protected MetricPointReclaimTestsBase()
23-
{
24-
this.configuration = new ConfigurationBuilder()
25-
.AddInMemoryCollection(this.configurationData)
26-
.Build();
27-
}
28-
2912
[Theory]
3013
[InlineData(false)]
3114
[InlineData(true)]
@@ -44,10 +27,6 @@ public void MeasurementsAreNotDropped(bool emitMetricWithNoDimensions)
4427
};
4528

4629
using var meterProvider = Sdk.CreateMeterProviderBuilder()
47-
.ConfigureServices(services =>
48-
{
49-
services.AddSingleton(this.configuration);
50-
})
5130
.AddMeter(Utils.GetCurrentMethodName())
5231
.AddReader(metricReader)
5332
.Build();
@@ -137,10 +116,6 @@ public void MeasurementsAreAggregatedEvenAfterTheyAreDropped(bool emitMetricWith
137116
};
138117

139118
using var meterProvider = Sdk.CreateMeterProviderBuilder()
140-
.ConfigureServices(services =>
141-
{
142-
services.AddSingleton(this.configuration);
143-
})
144119
.AddMeter(Utils.GetCurrentMethodName())
145120
.SetMaxMetricPointsPerMetricStream(10) // Set max MetricPoints limit to 10
146121
.AddReader(metricReader)
@@ -262,11 +237,3 @@ public override ExportResult Export(in Batch<Metric> batch)
262237
}
263238
}
264239
}
265-
266-
public class MetricPointReclaimTests : MetricPointReclaimTestsBase
267-
{
268-
public MetricPointReclaimTests()
269-
: base()
270-
{
271-
}
272-
}

test/OpenTelemetry.Tests/Metrics/MetricSnapshotTestsBase.cs renamed to test/OpenTelemetry.Tests/Metrics/MetricSnapshotTests.cs

+5-36
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics.Metrics;
5-
using Microsoft.Extensions.Configuration;
6-
using Microsoft.Extensions.DependencyInjection;
75
using OpenTelemetry.Tests;
86

97
using Xunit;
108

119
namespace OpenTelemetry.Metrics.Tests;
1210

13-
#pragma warning disable SA1402
14-
15-
public abstract class MetricSnapshotTestsBase
11+
public class MetricSnapshotTests
1612
{
17-
private readonly IConfiguration configuration;
18-
19-
protected MetricSnapshotTestsBase()
20-
{
21-
this.configuration = MetricApiTestsBase.BuildConfiguration();
22-
}
23-
2413
[Fact]
2514
public void VerifySnapshot_Counter()
2615
{
@@ -30,10 +19,6 @@ public void VerifySnapshot_Counter()
3019
using var meter = new Meter(Utils.GetCurrentMethodName());
3120
var counter = meter.CreateCounter<long>("meter");
3221
using var meterProvider = Sdk.CreateMeterProviderBuilder()
33-
.ConfigureServices(services =>
34-
{
35-
services.AddSingleton(this.configuration);
36-
})
3722
.AddMeter(meter.Name)
3823
.AddInMemoryExporter(exportedMetrics)
3924
.AddInMemoryExporter(exportedSnapshots)
@@ -103,10 +88,6 @@ public void VerifySnapshot_Histogram()
10388
using var meter = new Meter(Utils.GetCurrentMethodName());
10489
var histogram = meter.CreateHistogram<int>("histogram");
10590
using var meterProvider = Sdk.CreateMeterProviderBuilder()
106-
.ConfigureServices(services =>
107-
{
108-
services.AddSingleton(this.configuration);
109-
})
11091
.AddMeter(meter.Name)
11192
.AddInMemoryExporter(exportedMetrics)
11293
.AddInMemoryExporter(exportedSnapshots)
@@ -199,10 +180,6 @@ public void VerifySnapshot_ExponentialHistogram()
199180
using var meter = new Meter(Utils.GetCurrentMethodName());
200181
var histogram = meter.CreateHistogram<int>("histogram");
201182
using var meterProvider = Sdk.CreateMeterProviderBuilder()
202-
.ConfigureServices(services =>
203-
{
204-
services.AddSingleton(this.configuration);
205-
})
206183
.AddMeter(meter.Name)
207184
.AddView("histogram", new Base2ExponentialBucketHistogramConfiguration())
208185
.AddInMemoryExporter(exportedMetrics)
@@ -225,7 +202,7 @@ public void VerifySnapshot_ExponentialHistogram()
225202
metricPoint1.TryGetHistogramMinMaxValues(out var min, out var max);
226203
Assert.Equal(10, min);
227204
Assert.Equal(10, max);
228-
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint1.GetExponentialHistogramData());
205+
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint1.GetExponentialHistogramData());
229206

230207
// Verify Snapshot 1
231208
Assert.Single(exportedSnapshots);
@@ -236,7 +213,7 @@ public void VerifySnapshot_ExponentialHistogram()
236213
snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
237214
Assert.Equal(10, min);
238215
Assert.Equal(10, max);
239-
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot1.MetricPoints[0].GetExponentialHistogramData());
216+
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot1.MetricPoints[0].GetExponentialHistogramData());
240217

241218
// Verify Metric == Snapshot
242219
Assert.Equal(metric1.Name, snapshot1.Name);
@@ -270,7 +247,7 @@ public void VerifySnapshot_ExponentialHistogram()
270247
metricPoint1.TryGetHistogramMinMaxValues(out min, out max);
271248
Assert.Equal(5, min);
272249
Assert.Equal(10, max);
273-
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint2.GetExponentialHistogramData());
250+
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint2.GetExponentialHistogramData());
274251

275252
// Verify Snapshot 1 after second export
276253
// This value is expected to be unchanged.
@@ -289,14 +266,6 @@ public void VerifySnapshot_ExponentialHistogram()
289266
snapshot2.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
290267
Assert.Equal(5, min);
291268
Assert.Equal(10, max);
292-
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot2.MetricPoints[0].GetExponentialHistogramData());
293-
}
294-
}
295-
296-
public class MetricSnapshotTests : MetricSnapshotTestsBase
297-
{
298-
public MetricSnapshotTests()
299-
: base()
300-
{
269+
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot2.MetricPoints[0].GetExponentialHistogramData());
301270
}
302271
}

test/OpenTelemetry.Tests/Metrics/MetricTestsBase.cs

+3-22
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44
#if BUILDING_HOSTING_TESTS
55
using System.Diagnostics;
66
#endif
7+
#if BUILDING_HOSTING_TESTS
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
9-
#if BUILDING_HOSTING_TESTS
1010
using Microsoft.Extensions.Diagnostics.Metrics;
1111
using Microsoft.Extensions.Hosting;
1212
#endif
1313
using Xunit;
1414

1515
namespace OpenTelemetry.Metrics.Tests;
1616

17-
public class MetricTestsBase
17+
public abstract class MetricTestsBase
1818
{
19-
protected readonly IConfiguration? configuration;
20-
2119
protected MetricTestsBase()
2220
{
2321
}
2422

25-
protected MetricTestsBase(IConfiguration configuration)
26-
{
27-
this.configuration = configuration;
28-
}
29-
3023
#if BUILDING_HOSTING_TESTS
3124
public static IHost BuildHost(
3225
bool useWithMetricsStyle,
@@ -204,26 +197,14 @@ public IDisposable BuildMeterProvider(
204197
#if BUILDING_HOSTING_TESTS
205198
var host = BuildHost(
206199
useWithMetricsStyle: false,
207-
configureMeterProviderBuilder: configure,
208-
configureServices: services =>
209-
{
210-
if (this.configuration != null)
211-
{
212-
services.AddSingleton(this.configuration);
213-
}
214-
});
200+
configureMeterProviderBuilder: configure);
215201

216202
meterProvider = host.Services.GetRequiredService<MeterProvider>();
217203

218204
return host;
219205
#else
220206
var builder = Sdk.CreateMeterProviderBuilder();
221207

222-
if (this.configuration != null)
223-
{
224-
builder.ConfigureServices(services => services.AddSingleton(this.configuration));
225-
}
226-
227208
configure(builder);
228209

229210
return meterProvider = builder.Build();

0 commit comments

Comments
 (0)