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

[tests] Clean up class structure in metrics tests #5960

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Clean up class structure in metrics tests introduced to support delta…
… metricpoint reclaim feature.
  • Loading branch information
CodeBlanch committed Nov 8, 2024
commit 4ffca3e0df5e2f7542f1b8974a7f1dac4605f58d
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,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
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Tests/Metrics/MetricViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public void ViewToProduceExponentialHistogram()
var count = metricPoint.GetHistogramCount();
var sum = metricPoint.GetHistogramSum();

AggregatorTestsBase.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData());
AggregatorTests.AssertExponentialBucketsAreCorrect(expectedHistogram, metricPoint.GetExponentialHistogramData());
Assert.Equal(50, sum);
Assert.Equal(6, count);
}
Expand Down
Loading