Skip to content

Commit

Permalink
[tests] Clean up class structure in metrics tests (open-telemetry#5960)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Nov 9, 2024
1 parent 6044583 commit 2aae6e1
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
<!-- Note: These are SDK tests which we link and run here using the
IMetricsBuilder/IMetricsListener API added at the host level in .NET 8
instead of the direct lower-level MeterListener API added in .NET 6. -->
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\AggregatorTestsBase.cs" Link="Includes\Metrics\AggregatorTestsBase.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\AggregatorTests.cs" Link="Includes\Metrics\AggregatorTests.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\KnownHistogramBuckets.cs" Link="Includes\Metrics\KnownHistogramBuckets.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricApiTestsBase.cs" Link="Includes\Metrics\MetricApiTestsBase.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricApiTests.cs" Link="Includes\Metrics\MetricApiTests.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricExemplarTests.cs" Link="Includes\Metrics\MetricExemplarTests.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricTestData.cs" Link="Includes\Metrics\MetricTestData.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Metrics\MetricTestsBase.cs" Link="Includes\Metrics\MetricTestsBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

namespace OpenTelemetry.Metrics.Tests;

#pragma warning disable SA1402

public abstract class AggregatorTestsBase
public class AggregatorTests
{
private static readonly Meter Meter = new("testMeter");
private static readonly Instrument Instrument = Meter.CreateHistogram<long>("testInstrument");
Expand All @@ -18,7 +16,7 @@ public abstract class AggregatorTestsBase

private readonly AggregatorStore aggregatorStore;

protected AggregatorTestsBase()
public AggregatorTests()
{
this.aggregatorStore = new(MetricStreamIdentity, AggregationType.HistogramWithBuckets, AggregationTemporality.Cumulative, 1024);
}
Expand Down Expand Up @@ -510,11 +508,3 @@ public ThreadArguments(MetricPoint histogramPoint, ManualResetEvent mreToEnsureA
}
}
}

public class AggregatorTests : AggregatorTestsBase
{
public AggregatorTests()
: base()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Exporter;
using OpenTelemetry.Internal;
using OpenTelemetry.Tests;
Expand All @@ -12,9 +11,7 @@

namespace OpenTelemetry.Metrics.Tests;

#pragma warning disable SA1402

public abstract class MetricApiTestsBase : MetricTestsBase
public class MetricApiTests : MetricTestsBase
{
private const int MaxTimeToAllowForFlush = 10000;
private static readonly int NumberOfThreads = Environment.ProcessorCount;
Expand All @@ -23,8 +20,7 @@ public abstract class MetricApiTestsBase : MetricTestsBase
private static readonly int NumberOfMetricUpdateByEachThread = 100000;
private readonly ITestOutputHelper output;

protected MetricApiTestsBase(ITestOutputHelper output)
: base(BuildConfiguration())
public MetricApiTests(ITestOutputHelper output)
{
this.output = output;
}
Expand Down Expand Up @@ -1703,14 +1699,6 @@ public void GaugeHandlesNoNewMeasurementsCorrectlyWithTemporality(MetricReaderTe
}
}

internal static IConfiguration BuildConfiguration()
{
var configurationData = new Dictionary<string, string?>();
return new ConfigurationBuilder()
.AddInMemoryCollection(configurationData)
.Build();
}

private static void CounterUpdateThread<T>(object? obj)
where T : struct, IComparable
{
Expand Down Expand Up @@ -1878,11 +1866,3 @@ public UpdateThreadArguments(ManualResetEvent mreToBlockUpdateThread, ManualRese
}
}
}

public class MetricApiTest : MetricApiTestsBase
{
public MetricApiTest(ITestOutputHelper output)
: base(output)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics.Metrics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Tests;
using Xunit;

namespace OpenTelemetry.Metrics.Tests;

#pragma warning disable SA1402

public abstract class MetricOverflowAttributeTestsBase
public class MetricOverflowAttributeTests
{
private readonly Dictionary<string, string?> configurationData = new()
{
};

private readonly IConfiguration configuration;

public MetricOverflowAttributeTestsBase()
{
this.configuration = new ConfigurationBuilder()
.AddInMemoryCollection(this.configurationData)
.Build();
}

[Theory]
[InlineData(MetricReaderTemporalityPreference.Delta)]
[InlineData(MetricReaderTemporalityPreference.Cumulative)]
Expand All @@ -37,10 +20,6 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForCounter(MetricReaderTem
var counter = meter.CreateCounter<long>("TestCounter");

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedItems, metricReaderOptions => metricReaderOptions.TemporalityPreference = temporalityPreference)
.Build();
Expand Down Expand Up @@ -181,10 +160,6 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
var histogram = meter.CreateHistogram<long>("TestHistogram");

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedItems, metricReaderOptions => metricReaderOptions.TemporalityPreference = temporalityPreference)
.Build();
Expand Down Expand Up @@ -318,11 +293,3 @@ public void MetricOverflowAttributeIsRecordedCorrectlyForHistogram(MetricReaderT
}
}
}

public class MetricOverflowAttributeTests : MetricOverflowAttributeTestsBase
{
public MetricOverflowAttributeTests()
: base()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics.Metrics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Tests;
using Xunit;

namespace OpenTelemetry.Metrics.Tests;

#pragma warning disable SA1402

public abstract class MetricPointReclaimTestsBase
public class MetricPointReclaimTests
{
private readonly Dictionary<string, string?> configurationData = new()
{
};

private readonly IConfiguration configuration;

protected MetricPointReclaimTestsBase()
{
this.configuration = new ConfigurationBuilder()
.AddInMemoryCollection(this.configurationData)
.Build();
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand All @@ -44,10 +27,6 @@ public void MeasurementsAreNotDropped(bool emitMetricWithNoDimensions)
};

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(Utils.GetCurrentMethodName())
.AddReader(metricReader)
.Build();
Expand Down Expand Up @@ -137,10 +116,6 @@ public void MeasurementsAreAggregatedEvenAfterTheyAreDropped(bool emitMetricWith
};

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(Utils.GetCurrentMethodName())
.SetMaxMetricPointsPerMetricStream(10) // Set max MetricPoints limit to 10
.AddReader(metricReader)
Expand Down Expand Up @@ -262,11 +237,3 @@ public override ExportResult Export(in Batch<Metric> batch)
}
}
}

public class MetricPointReclaimTests : MetricPointReclaimTestsBase
{
public MetricPointReclaimTests()
: base()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics.Metrics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Tests;

using Xunit;

namespace OpenTelemetry.Metrics.Tests;

#pragma warning disable SA1402

public abstract class MetricSnapshotTestsBase
public class MetricSnapshotTests
{
private readonly IConfiguration configuration;

protected MetricSnapshotTestsBase()
{
this.configuration = MetricApiTestsBase.BuildConfiguration();
}

[Fact]
public void VerifySnapshot_Counter()
{
Expand All @@ -30,10 +19,6 @@ public void VerifySnapshot_Counter()
using var meter = new Meter(Utils.GetCurrentMethodName());
var counter = meter.CreateCounter<long>("meter");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedMetrics)
.AddInMemoryExporter(exportedSnapshots)
Expand Down Expand Up @@ -103,10 +88,6 @@ public void VerifySnapshot_Histogram()
using var meter = new Meter(Utils.GetCurrentMethodName());
var histogram = meter.CreateHistogram<int>("histogram");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedMetrics)
.AddInMemoryExporter(exportedSnapshots)
Expand Down Expand Up @@ -199,10 +180,6 @@ public void VerifySnapshot_ExponentialHistogram()
using var meter = new Meter(Utils.GetCurrentMethodName());
var histogram = meter.CreateHistogram<int>("histogram");
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureServices(services =>
{
services.AddSingleton(this.configuration);
})
.AddMeter(meter.Name)
.AddView("histogram", new Base2ExponentialBucketHistogramConfiguration())
.AddInMemoryExporter(exportedMetrics)
Expand All @@ -225,7 +202,7 @@ public void VerifySnapshot_ExponentialHistogram()
metricPoint1.TryGetHistogramMinMaxValues(out var min, out var max);
Assert.Equal(10, min);
Assert.Equal(10, max);
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint1.GetExponentialHistogramData());
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint1.GetExponentialHistogramData());

// Verify Snapshot 1
Assert.Single(exportedSnapshots);
Expand All @@ -236,7 +213,7 @@ public void VerifySnapshot_ExponentialHistogram()
snapshot1.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(10, min);
Assert.Equal(10, max);
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot1.MetricPoints[0].GetExponentialHistogramData());
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot1.MetricPoints[0].GetExponentialHistogramData());

// Verify Metric == Snapshot
Assert.Equal(metric1.Name, snapshot1.Name);
Expand Down Expand Up @@ -270,7 +247,7 @@ public void VerifySnapshot_ExponentialHistogram()
metricPoint1.TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(5, min);
Assert.Equal(10, max);
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint2.GetExponentialHistogramData());
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint2.GetExponentialHistogramData());

// Verify Snapshot 1 after second export
// This value is expected to be unchanged.
Expand All @@ -289,14 +266,6 @@ public void VerifySnapshot_ExponentialHistogram()
snapshot2.MetricPoints[0].TryGetHistogramMinMaxValues(out min, out max);
Assert.Equal(5, min);
Assert.Equal(10, max);
AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot2.MetricPoints[0].GetExponentialHistogramData());
}
}

public class MetricSnapshotTests : MetricSnapshotTestsBase
{
public MetricSnapshotTests()
: base()
{
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, snapshot2.MetricPoints[0].GetExponentialHistogramData());
}
}
25 changes: 3 additions & 22 deletions test/OpenTelemetry.Tests/Metrics/MetricTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,22 @@
#if BUILDING_HOSTING_TESTS
using System.Diagnostics;
#endif
#if BUILDING_HOSTING_TESTS
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
#if BUILDING_HOSTING_TESTS
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.Hosting;
#endif
using Xunit;

namespace OpenTelemetry.Metrics.Tests;

public class MetricTestsBase
public abstract class MetricTestsBase
{
protected readonly IConfiguration? configuration;

protected MetricTestsBase()
{
}

protected MetricTestsBase(IConfiguration configuration)
{
this.configuration = configuration;
}

#if BUILDING_HOSTING_TESTS
public static IHost BuildHost(
bool useWithMetricsStyle,
Expand Down Expand Up @@ -204,26 +197,14 @@ public IDisposable BuildMeterProvider(
#if BUILDING_HOSTING_TESTS
var host = BuildHost(
useWithMetricsStyle: false,
configureMeterProviderBuilder: configure,
configureServices: services =>
{
if (this.configuration != null)
{
services.AddSingleton(this.configuration);
}
});
configureMeterProviderBuilder: configure);

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

return host;
#else
var builder = Sdk.CreateMeterProviderBuilder();

if (this.configuration != null)
{
builder.ConfigureServices(services => services.AddSingleton(this.configuration));
}

configure(builder);

return meterProvider = builder.Build();
Expand Down
Loading

0 comments on commit 2aae6e1

Please sign in to comment.