Skip to content

Commit 0e22cd4

Browse files
EvangelinkCopilot
andcommitted
Own enabled SDK state in OpenTelemetry provider tests
Use the existing environment scope and matching writer resource lock for the provider/exporter assertions that require an enabled OpenTelemetry SDK. Preserve the exporter/disposal assertions, existing writer locks, existing DoNotParallelize declarations, and method-level assembly scheduling. The original lifecycle case executed and failed at its exporter assertion with inherited OTEL_SDK_DISABLED=true (one failed, exit 2). The same case passes with the repair (one passed, exit 0). All 43 same-class cases execute and pass with inherited true and with the variable absent, using 32 method-level workers. This proves inherited-state handling; the precise CI mid-build interleaving was not captured. No production disposal or dependency version is changed. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 500f4f0 commit 0e22cd4

1 file changed

Lines changed: 119 additions & 96 deletions

File tree

‎test/UnitTests/Microsoft.Testing.Extensions.UnitTests/OpenTelemetryProviderExtensionsTests.cs‎

Lines changed: 119 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ namespace Microsoft.Testing.Extensions.UnitTests;
3737
/// <see cref="ResourceLockAttribute"/> on <see cref="WellKnownResources.EnvironmentVariables"/> (the same pattern
3838
/// used by <c>AzureFoundryChatClientProviderTests</c> and <c>TestingPlatformResourceDetectorTests</c> in this
3939
/// project): they still serialize against every other test in the assembly that mutates environment variables, but
40-
/// can run in parallel with tests that never touch environment variables at all. The two raw-listener methods carry
40+
/// can run in parallel with tests that never touch environment variables at all. SDK-building checks that require
41+
/// enabled providers arrange <c>OTEL_SDK_DISABLED=false</c> inside that scope so inherited configuration or another
42+
/// test cannot replace either provider with a no-op provider. The two raw-listener methods carry
4143
/// a method-level write <see cref="ResourceLockAttribute"/> on
4244
/// <see cref="OpenTelemetryPlatformService.ActivitySourceName"/> instead:
4345
/// <see cref="ActivitySource.AddActivityListener(ActivityListener)"/> registers an unfiltered listener against the
@@ -251,59 +253,73 @@ public void AddOpenTelemetryProviderFromEnvironment_WithNullBuilder_Throws()
251253

252254
[TestMethod]
253255
[DoNotParallelize]
256+
[ResourceLock(WellKnownResources.EnvironmentVariables)]
254257
public void AddTestingPlatformInstrumentation_ConfiguresDurationHistogramBoundaries()
255-
{
256-
CapturingMetricExporter exporter = new();
257-
using BaseExportingMetricReader reader = new(exporter);
258-
259-
using (var provider = new OpenTelemetryProvider(
260-
withMetrics: metrics => metrics
261-
.AddTestingPlatformInstrumentation()
262-
.AddReader(reader)))
263-
using (var service = new OpenTelemetryPlatformService())
264-
{
265-
service.CreateHistogram<double>(TestingPlatformSemanticConventions.Metrics.TestCaseDuration).Record(0.25);
266-
service.CreateHistogram<double>(TestingPlatformSemanticConventions.Metrics.TestRunDuration).Record(60);
267-
}
258+
=> WithEnvironment(
259+
new()
260+
{
261+
["OTEL_SDK_DISABLED"] = "false",
262+
},
263+
() =>
264+
{
265+
CapturingMetricExporter exporter = new();
266+
using BaseExportingMetricReader reader = new(exporter);
267+
268+
using (var provider = new OpenTelemetryProvider(
269+
withMetrics: metrics => metrics
270+
.AddTestingPlatformInstrumentation()
271+
.AddReader(reader)))
272+
using (var service = new OpenTelemetryPlatformService())
273+
{
274+
service.CreateHistogram<double>(TestingPlatformSemanticConventions.Metrics.TestCaseDuration).Record(0.25);
275+
service.CreateHistogram<double>(TestingPlatformSemanticConventions.Metrics.TestRunDuration).Record(60);
276+
}
268277

269-
AssertHistogramBounds(
270-
exporter,
271-
TestingPlatformSemanticConventions.Metrics.TestCaseDuration,
272-
[0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 300]);
273-
AssertHistogramBounds(
274-
exporter,
275-
TestingPlatformSemanticConventions.Metrics.TestRunDuration,
276-
[1, 5, 10, 30, 60, 120, 300, 600, 1800, 3600]);
277-
}
278+
AssertHistogramBounds(
279+
exporter,
280+
TestingPlatformSemanticConventions.Metrics.TestCaseDuration,
281+
[0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 300]);
282+
AssertHistogramBounds(
283+
exporter,
284+
TestingPlatformSemanticConventions.Metrics.TestRunDuration,
285+
[1, 5, 10, 30, 60, 120, 300, 600, 1800, 3600]);
286+
});
278287

279288
[TestMethod]
289+
[ResourceLock(WellKnownResources.EnvironmentVariables)]
280290
public void OpenTelemetryProvider_InvokesConfigurationDelegatesAndDisposesBothProviders()
281-
{
282-
CapturingActivityExporter activityExporter = new([], namePrefix: string.Empty);
283-
CapturingMetricExporter metricExporter = new();
284-
bool tracingConfigured = false;
285-
bool metricsConfigured = false;
286-
using SimpleActivityExportProcessor activityProcessor = new(activityExporter);
287-
using BaseExportingMetricReader metricReader = new(metricExporter);
288-
var provider = new OpenTelemetryProvider(
289-
tracing =>
291+
=> WithEnvironment(
292+
new()
290293
{
291-
tracingConfigured = true;
292-
tracing.AddProcessor(activityProcessor);
294+
["OTEL_SDK_DISABLED"] = "false",
293295
},
294-
metrics =>
296+
() =>
295297
{
296-
metricsConfigured = true;
297-
metrics.AddReader(metricReader);
298-
});
298+
CapturingActivityExporter activityExporter = new([], namePrefix: string.Empty);
299+
CapturingMetricExporter metricExporter = new();
300+
bool tracingConfigured = false;
301+
bool metricsConfigured = false;
302+
using SimpleActivityExportProcessor activityProcessor = new(activityExporter);
303+
using BaseExportingMetricReader metricReader = new(metricExporter);
304+
var provider = new OpenTelemetryProvider(
305+
tracing =>
306+
{
307+
tracingConfigured = true;
308+
tracing.AddProcessor(activityProcessor);
309+
},
310+
metrics =>
311+
{
312+
metricsConfigured = true;
313+
metrics.AddReader(metricReader);
314+
});
299315

300-
provider.Dispose();
316+
provider.Dispose();
301317

302-
Assert.IsTrue(tracingConfigured);
303-
Assert.IsTrue(metricsConfigured);
304-
Assert.AreEqual(1, activityExporter.ShutdownCount);
305-
Assert.AreEqual(1, metricExporter.ShutdownCount);
306-
}
318+
Assert.IsTrue(tracingConfigured);
319+
Assert.IsTrue(metricsConfigured);
320+
Assert.AreEqual(1, activityExporter.ShutdownCount);
321+
Assert.AreEqual(1, metricExporter.ShutdownCount);
322+
});
307323

308324
[TestMethod]
309325
[ResourceLock(WellKnownResources.EnvironmentVariables)]
@@ -723,62 +739,69 @@ public void ResolveEnvironmentConfiguration_WithTracingDelegateButNoExporter_Con
723739

724740
[TestMethod]
725741
[DoNotParallelize]
742+
[ResourceLock(WellKnownResources.EnvironmentVariables)]
726743
public void EndToEnd_InstrumentationAndResource_ExportSpanWithSemanticConventionTagsAndResource()
727-
{
728-
string namePrefix = $"e2e-{Guid.NewGuid():N}-";
729-
List<Activity> exported = [];
730-
CapturingActivityExporter exporter = new(exported, namePrefix);
731-
732-
// The built TracerProvider (disposed by the using below) takes ownership of the processor added through
733-
// AddProcessor and disposes it — and the exporter it wraps — on Dispose, so there is no undisposed local.
734-
using (TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
735-
.AddTestingPlatformInstrumentation()
736-
.ConfigureResource(resource => resource.AddTestingPlatformResource())
737-
.AddProcessor(new SimpleActivityExportProcessor(exporter))
738-
.Build())
739-
{
740-
using var service = new OpenTelemetryPlatformService();
741-
using var handler = new OpenTelemetryResultHandler(service);
742-
743-
var testNode = new TestNode
744+
=> WithEnvironment(
745+
new()
744746
{
745-
Uid = new TestNodeUid("MyNamespace.MyTests.MyTest"),
746-
DisplayName = namePrefix + "MyTest",
747-
Properties = new PropertyBag(
748-
new TestMethodIdentifierProperty(
749-
assemblyFullName: "MyAssembly",
750-
@namespace: "MyNamespace",
751-
typeName: "MyTests",
752-
methodName: "MyTest",
753-
methodArity: 0,
754-
parameterTypeFullNames: [],
755-
returnTypeFullName: "System.Void"),
756-
new TestFileLocationProperty("/repo/src/MyTests.cs", new LinePositionSpan(new LinePosition(10, 0), new LinePosition(12, 0))),
757-
new StandardOutputProperty("hello from the test")),
758-
};
759-
760-
handler.NotifyInProgress(testNode, parentUid: null);
761-
handler.NotifyPassed(testNode, PassedTestNodeStateProperty.CachedInstance);
762-
}
747+
["OTEL_SDK_DISABLED"] = "false",
748+
},
749+
() =>
750+
{
751+
string namePrefix = $"e2e-{Guid.NewGuid():N}-";
752+
List<Activity> exported = [];
753+
CapturingActivityExporter exporter = new(exported, namePrefix);
754+
755+
// The built TracerProvider (disposed by the using below) takes ownership of the processor added through
756+
// AddProcessor and disposes it — and the exporter it wraps — on Dispose, so there is no undisposed local.
757+
using (TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
758+
.AddTestingPlatformInstrumentation()
759+
.ConfigureResource(resource => resource.AddTestingPlatformResource())
760+
.AddProcessor(new SimpleActivityExportProcessor(exporter))
761+
.Build())
762+
{
763+
using var service = new OpenTelemetryPlatformService();
764+
using var handler = new OpenTelemetryResultHandler(service);
763765

764-
Activity activity = exported.Single();
765-
Assert.AreEqual(namePrefix + "MyTest", activity.GetTagItem("test.case.name"));
766-
Assert.AreEqual("MyNamespace.MyTests.MyTest", activity.GetTagItem("code.function.name"));
767-
Assert.AreEqual("MyTests", activity.GetTagItem("test.suite.name"));
768-
Assert.AreEqual("/repo/src/MyTests.cs", activity.GetTagItem("code.file.path"));
769-
Assert.AreEqual("pass", activity.GetTagItem("test.case.result.status"));
770-
Assert.AreEqual("hello from the test", activity.GetTagItem("test.output.stdout"));
771-
772-
Assert.IsNotNull(exporter.CapturedResource);
773-
Dictionary<string, object> resourceAttributes = [];
774-
foreach (KeyValuePair<string, object> attribute in exporter.CapturedResource.Attributes)
775-
{
776-
resourceAttributes[attribute.Key] = attribute.Value;
777-
}
766+
var testNode = new TestNode
767+
{
768+
Uid = new TestNodeUid("MyNamespace.MyTests.MyTest"),
769+
DisplayName = namePrefix + "MyTest",
770+
Properties = new PropertyBag(
771+
new TestMethodIdentifierProperty(
772+
assemblyFullName: "MyAssembly",
773+
@namespace: "MyNamespace",
774+
typeName: "MyTests",
775+
methodName: "MyTest",
776+
methodArity: 0,
777+
parameterTypeFullNames: [],
778+
returnTypeFullName: "System.Void"),
779+
new TestFileLocationProperty("/repo/src/MyTests.cs", new LinePositionSpan(new LinePosition(10, 0), new LinePosition(12, 0))),
780+
new StandardOutputProperty("hello from the test")),
781+
};
782+
783+
handler.NotifyInProgress(testNode, parentUid: null);
784+
handler.NotifyPassed(testNode, PassedTestNodeStateProperty.CachedInstance);
785+
}
778786

779-
Assert.IsTrue(resourceAttributes.ContainsKey("service.name"));
780-
Assert.AreEqual(Environment.MachineName, resourceAttributes["host.name"]);
781-
}
787+
Activity activity = exported.Single();
788+
Assert.AreEqual(namePrefix + "MyTest", activity.GetTagItem("test.case.name"));
789+
Assert.AreEqual("MyNamespace.MyTests.MyTest", activity.GetTagItem("code.function.name"));
790+
Assert.AreEqual("MyTests", activity.GetTagItem("test.suite.name"));
791+
Assert.AreEqual("/repo/src/MyTests.cs", activity.GetTagItem("code.file.path"));
792+
Assert.AreEqual("pass", activity.GetTagItem("test.case.result.status"));
793+
Assert.AreEqual("hello from the test", activity.GetTagItem("test.output.stdout"));
794+
795+
Assert.IsNotNull(exporter.CapturedResource);
796+
Dictionary<string, object> resourceAttributes = [];
797+
foreach (KeyValuePair<string, object> attribute in exporter.CapturedResource.Attributes)
798+
{
799+
resourceAttributes[attribute.Key] = attribute.Value;
800+
}
801+
802+
Assert.IsTrue(resourceAttributes.ContainsKey("service.name"));
803+
Assert.AreEqual(Environment.MachineName, resourceAttributes["host.name"]);
804+
});
782805

783806
private static Func<string, string?> Env(Dictionary<string, string?> values)
784807
=> name => values.TryGetValue(name, out string? value) ? value : null;

0 commit comments

Comments
 (0)