public void TracerProvideSdkCreatesActivitySourceWhenNoProcessor() { TestInstrumentation testInstrumentation = null; using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddDiagnosticSourceInstrumentation((adapter) => { testInstrumentation = new TestInstrumentation(adapter); return(testInstrumentation); }) .Build(); var adapter = testInstrumentation.Adapter; Activity activity = new Activity("test"); activity.Start(); adapter.Start(activity, ActivityKind.Internal, new ActivitySource("test", "1.0.0")); adapter.Stop(activity); activity.Stop(); // No asserts here. Validates that no exception // gets thrown when processors are not added, // TODO: Refactor to have more proper unit test // to target each individual classes. }
public void AddOpenTelemetryTracerProvider_ServiceProviderArgument_ServicesRegistered() { var testInstrumentation = new TestInstrumentation(); var services = new ServiceCollection(); services.AddSingleton(testInstrumentation); services.AddOpenTelemetryTracing(builder => { builder.Configure( (sp, b) => b.AddInstrumentation(() => sp.GetRequiredService <TestInstrumentation>())); }); var serviceProvider = services.BuildServiceProvider(); var tracerFactory = serviceProvider.GetRequiredService <TracerProvider>(); Assert.NotNull(tracerFactory); Assert.False(testInstrumentation.Disposed); serviceProvider.Dispose(); Assert.True(testInstrumentation.Disposed); }
public void AddOpenTelemetryTracerProvider_Idempotent() { var testInstrumentation1 = new TestInstrumentation(); var testInstrumentation2 = new TestInstrumentation(); var services = new ServiceCollection(); services.AddSingleton(testInstrumentation1); services.AddOpenTelemetryTracing(builder => { builder.AddInstrumentation(() => testInstrumentation1); }); services.AddOpenTelemetryTracing(builder => { builder.AddInstrumentation(() => testInstrumentation2); }); var serviceProvider = services.BuildServiceProvider(); var tracerFactory = serviceProvider.GetRequiredService <TracerProvider>(); Assert.NotNull(tracerFactory); Assert.False(testInstrumentation1.Disposed); Assert.False(testInstrumentation2.Disposed); serviceProvider.Dispose(); Assert.True(testInstrumentation1.Disposed); Assert.True(testInstrumentation2.Disposed); }
public ActivitySourceAdapterBenchmark() { this.tracerProvider = Sdk.CreateTracerProviderBuilder() .AddInstrumentation((adapter) => { this.testInstrumentation = new TestInstrumentation(adapter); return(this.testInstrumentation); }) .Build(); }
public void GlobalSetup() { this.tracerProvider = Sdk.CreateTracerProviderBuilder() .AddDiagnosticSourceInstrumentation((adapter) => { this.testInstrumentation = new TestInstrumentation(adapter); return(this.testInstrumentation); }) .Build(); }
public void TracerProvideSdkCreatesAndDiposesInstrumentation() { TestInstrumentation testInstrumentation = null; var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddDiagnosticSourceInstrumentation((adapter) => { testInstrumentation = new TestInstrumentation(adapter); return(testInstrumentation); }) .Build(); Assert.NotNull(testInstrumentation); var adapter = testInstrumentation.Adapter; Assert.NotNull(adapter); Assert.False(testInstrumentation.IsDisposed); tracerProvider.Dispose(); Assert.True(testInstrumentation.IsDisposed); }
public async Task AddOpenTelemetryTracerProviderInstrumentationCreationAndDisposal() { var testInstrumentation = new TestInstrumentation(); var callbackRun = false; var builder = new HostBuilder().ConfigureServices(services => { services.AddOpenTelemetryTracerProvider(builder => { builder.AddInstrumentation((activitySource) => { Assert.NotNull(activitySource); callbackRun = true; return(testInstrumentation); }); }); }); var host = builder.Build(); Assert.False(callbackRun); Assert.False(testInstrumentation.Disposed); await host.StartAsync(); Assert.True(callbackRun); Assert.False(testInstrumentation.Disposed); await host.StopAsync(); Assert.True(callbackRun); Assert.False(testInstrumentation.Disposed); host.Dispose(); Assert.True(callbackRun); Assert.True(testInstrumentation.Disposed); }
public async Task AddOpenTelemetry_RegisterInstrumentation_InstrumentationCreatedAndDisposed() { var testInstrumentation = new TestInstrumentation(); var callbackRun = false; var builder = new HostBuilder().ConfigureServices(services => { services.AddOpenTelemetry(telemetry => { telemetry.AddInstrumentation(t => { callbackRun = true; return(testInstrumentation); }); }); }); var host = builder.Build(); Assert.False(callbackRun); Assert.False(testInstrumentation.Disposed); await host.StartAsync(); Assert.True(callbackRun); Assert.False(testInstrumentation.Disposed); await host.StopAsync(); Assert.True(callbackRun); Assert.False(testInstrumentation.Disposed); host.Dispose(); Assert.True(callbackRun); Assert.True(testInstrumentation.Disposed); }
public void AddOpenTelemetry_ServiceProviderArgument_ServicesRegistered() { var testInstrumentation = new TestInstrumentation(); var services = new ServiceCollection(); services.AddSingleton(testInstrumentation); services.AddOpenTelemetry((provider, builder) => { builder.AddInstrumentation <TestInstrumentation>((activitySource) => provider.GetRequiredService <TestInstrumentation>()); }); var serviceProvider = services.BuildServiceProvider(); var tracerFactory = serviceProvider.GetRequiredService <OpenTelemetrySdk>(); Assert.NotNull(tracerFactory); Assert.False(testInstrumentation.Disposed); serviceProvider.Dispose(); Assert.True(testInstrumentation.Disposed); }
public void CreateFactory_BuilderWithArgs() { var exporterCalledCount = 0; var testExporter = new TestSpanExporter(spans => { exporterCalledCount++; Assert.Single(spans); Assert.IsType <SpanData>(spans.Single()); }); TestInstrumentation instrumentation1 = null; TestInstrumentation instrumentation2 = null; TestProcessor processor = null; var tracerFactory = TracerFactory.Create(b => b .AddProcessorPipeline(p => p .SetExporter(testExporter) .SetExportingProcessor(e => { processor = new TestProcessor(e); return(processor); })) .AddInstrumentation(t => { instrumentation1 = new TestInstrumentation(t); return(instrumentation1); }) .AddInstrumentation(t => { instrumentation2 = new TestInstrumentation(t); return(instrumentation2); })); var tracer = tracerFactory.GetTracer("my-app"); var span = tracer.StartSpan("foo"); span.End(); // default sampler is always sample Assert.True(span.IsRecording); Assert.Equal(1, exporterCalledCount); Assert.Single(((SpanSdk)span).LibraryResource.Attributes); Assert.Single(((SpanSdk)span).LibraryResource.Attributes.Where(kvp => kvp.Key == "name" && kvp.Value.ToString() == "my-app")); Assert.NotNull(instrumentation1); Assert.NotNull(instrumentation2); Assert.NotNull(processor); var span1 = instrumentation1.Collect(); var span2 = instrumentation1.Collect(); Assert.Equal(3, exporterCalledCount); Assert.Equal(2, span1.LibraryResource.Attributes.Count()); Assert.Equal(2, span2.LibraryResource.Attributes.Count()); Assert.Single(span1.LibraryResource.Attributes.Where(kvp => kvp.Key == "name" && kvp.Value is string sv && sv == "TestInstrumentation")); Assert.Single(span2.LibraryResource.Attributes.Where(kvp => kvp.Key == "name" && kvp.Value is string sv && sv == "TestInstrumentation")); Assert.Single(span1.LibraryResource.Attributes.Where(kvp => kvp.Key == "version" && kvp.Value is string sv && sv == "semver:1.0.0.0")); Assert.Single(span2.LibraryResource.Attributes.Where(kvp => kvp.Key == "version" && kvp.Value is string sv && sv == "semver:1.0.0.0")); tracerFactory.Dispose(); Assert.True(instrumentation1.IsDisposed); Assert.True(instrumentation2.IsDisposed); Assert.True(processor.IsDisposed); }
public NotifyPropertyChangedTests() { engineInstrumentation = new TestInstrumentation(); engine = new DependencyEngine(); engine.AddInstrumentation(engineInstrumentation); }
public void TracerProvideSdkCreatesActivitySource() { using TestActivityProcessor testActivityProcessor = new TestActivityProcessor(); bool startCalled = false; bool endCalled = false; testActivityProcessor.StartAction = (a) => { startCalled = true; }; testActivityProcessor.EndAction = (a) => { endCalled = true; }; TestInstrumentation testInstrumentation = null; using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddProcessor(testActivityProcessor) .AddDiagnosticSourceInstrumentation((adapter) => { testInstrumentation = new TestInstrumentation(adapter); return(testInstrumentation); }) .Build(); var adapter = testInstrumentation.Adapter; Activity activity = new Activity("test"); activity.Start(); adapter.Start(activity, ActivityKind.Internal, new ActivitySource("test", "1.0.0")); adapter.Stop(activity); activity.Stop(); Assert.True(startCalled); Assert.True(endCalled); // As Processors can be added anytime after Provider construction, // the following validates that updated processors are reflected // in ActivitySourceAdapter. TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor(); bool startCalledNew = false; bool endCalledNew = false; testActivityProcessorNew.StartAction = (a) => { startCalledNew = true; }; testActivityProcessorNew.EndAction = (a) => { endCalledNew = true; }; tracerProvider.AddProcessor(testActivityProcessorNew); Activity activityNew = new Activity("test"); activityNew.Start(); adapter.Start(activityNew, ActivityKind.Internal, new ActivitySource("test", "1.0.0")); adapter.Stop(activityNew); activityNew.Stop(); Assert.True(startCalledNew); Assert.True(endCalledNew); }
public void Instrumentation() { /* * A * / \ * B Throws * | | * C Skipped * / \ / * E D */ var instrumentation = new TestInstrumentation(); engine.AddInstrumentation(instrumentation); var a = new SinglePropertyType(); var b = new SinglePropertyType(); var c = new SinglePropertyType(); var d = new SinglePropertyType(); var e = new SinglePropertyType(); var throws = new SinglePropertyType(); var skipped = new SinglePropertyType(); engine.Assign(() => b.Value).From(() => a.Value, ex => { }); engine.Assign(() => c.Value).From(() => b.Value, ex => { }); engine.Assign(() => throws.Value).From(() => ThrowsInvalidOperationException(a.Value), ex => { }); engine.Assign(() => skipped.Value).From(() => throws.Value, ex => { }); engine.Assign(() => d.Value).From(() => c.Value + skipped.Value, ex => { }); engine.Assign(() => e.Value).From(() => c.Value, ex => { }); a.Value = 2; engine.ValueHasChanged(a, "Value"); var dotFormat = engine.ToDotFormat("Title"); Console.WriteLine(dotFormat); instrumentation.WalkIndexStart.ShouldBe(1); instrumentation.WalkIndexEnd.ShouldBe(1); instrumentation.NodeEvaluations.Count.ShouldBe(8); }
public void TracerProvideSdkCreatesActivitySource() { TestActivityProcessor testActivityProcessor = new TestActivityProcessor(); bool startCalled = false; bool endCalled = false; testActivityProcessor.StartAction = (a) => { startCalled = true; }; testActivityProcessor.EndAction = (a) => { endCalled = true; }; TestInstrumentation testInstrumentation = null; using var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddProcessor(testActivityProcessor) .AddInstrumentation((adapter) => { testInstrumentation = new TestInstrumentation(adapter); return(testInstrumentation); }) .Build(); var adapter = testInstrumentation.Adapter; Activity activity = new Activity("test"); activity.Start(); adapter.Start(activity); adapter.Stop(activity); activity.Stop(); Assert.True(startCalled); Assert.True(endCalled); /* * Uncomment when issue 1075 is fixed. * TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor(); * * bool startCalledNew = false; * bool endCalledNew = false; * * testActivityProcessorNew.StartAction = * (a) => * { * startCalledNew = true; * }; * * testActivityProcessorNew.EndAction = * (a) => * { * endCalledNew = true; * }; * * tracerProvider.AddProcessor(testActivityProcessorNew); * Activity activityNew = new Activity("test"); * activityNew.Start(); * adapter.Start(activityNew); * adapter.Stop(activityNew); * activityNew.Stop(); * * Assert.True(startCalledNew); * Assert.True(endCalledNew); */ }