Skip to content

Commit

Permalink
[di-tracing] Fix the TracerProviderBuilder.AddInstrumentation factory…
Browse files Browse the repository at this point in the history
… pattern extension (#4468)
  • Loading branch information
CodeBlanch authored May 8, 2023
1 parent b9908d1 commit 4376564
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Api.ProviderBuilderExtensions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Fixed a bug which prevented the
`TracerProviderBuilder.AddInstrumentation(IServiceProvider, TracerProvider)`
factory extension from being called during construction of the SDK
`TracerProvider`.
([#4468](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4468))

## 1.5.0-alpha.2

Released 2023-Mar-31
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static TracerProviderBuilder AddInstrumentation<T>(

tracerProviderBuilder.ConfigureBuilder((sp, builder) =>
{
if (tracerProviderBuilder is ITracerProviderBuilder iTracerProviderBuilder
if (builder is ITracerProviderBuilder iTracerProviderBuilder
&& iTracerProviderBuilder.Provider != null)
{
builder.AddInstrumentation(() => instrumentationFactory(sp, iTracerProviderBuilder.Provider));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ public void AddReaderUsingDependencyInjectionTest()
Assert.True(reader.Head.Next?.Value is MyReader);
}

[Fact]
public void AddInstrumentationTest()
{
List<object> instrumentation = null;

using (var provider = Sdk.CreateMeterProviderBuilder()
.AddInstrumentation<MyInstrumentation>()
.AddInstrumentation((sp, provider) => new MyInstrumentation() { Provider = provider })
.AddInstrumentation(new MyInstrumentation())
.Build() as MeterProviderSdk)
{
Assert.NotNull(provider);

Assert.Equal(3, provider.Instrumentations.Count);

Assert.Null(((MyInstrumentation)provider.Instrumentations[0]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[0]).Disposed);

Assert.NotNull(((MyInstrumentation)provider.Instrumentations[1]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[1]).Disposed);

Assert.Null(((MyInstrumentation)provider.Instrumentations[2]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[2]).Disposed);

instrumentation = new List<object>(provider.Instrumentations);
}

Assert.NotNull(instrumentation);
Assert.True(((MyInstrumentation)instrumentation[0]).Disposed);
Assert.True(((MyInstrumentation)instrumentation[1]).Disposed);
Assert.True(((MyInstrumentation)instrumentation[2]).Disposed);
}

[Fact]
public void SetAndConfigureResourceTest()
{
Expand Down Expand Up @@ -303,6 +336,7 @@ private static void RunBuilderServiceLifecycleTest(

private sealed class MyInstrumentation : IDisposable
{
internal MeterProvider Provider;
internal bool Disposed;

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,39 @@ public void AddProcessorUsingDependencyInjectionTest()
Assert.True(processor.Head.Next?.Value is MyProcessor);
}

[Fact]
public void AddInstrumentationTest()
{
List<object> instrumentation = null;

using (var provider = Sdk.CreateTracerProviderBuilder()
.AddInstrumentation<MyInstrumentation>()
.AddInstrumentation((sp, provider) => new MyInstrumentation() { Provider = provider })
.AddInstrumentation(new MyInstrumentation())
.Build() as TracerProviderSdk)
{
Assert.NotNull(provider);

Assert.Equal(3, provider.Instrumentations.Count);

Assert.Null(((MyInstrumentation)provider.Instrumentations[0]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[0]).Disposed);

Assert.NotNull(((MyInstrumentation)provider.Instrumentations[1]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[1]).Disposed);

Assert.Null(((MyInstrumentation)provider.Instrumentations[2]).Provider);
Assert.False(((MyInstrumentation)provider.Instrumentations[2]).Disposed);

instrumentation = new List<object>(provider.Instrumentations);
}

Assert.NotNull(instrumentation);
Assert.True(((MyInstrumentation)instrumentation[0]).Disposed);
Assert.True(((MyInstrumentation)instrumentation[1]).Disposed);
Assert.True(((MyInstrumentation)instrumentation[2]).Disposed);
}

[Fact]
public void SetAndConfigureResourceTest()
{
Expand Down Expand Up @@ -431,6 +464,7 @@ public override SamplingResult ShouldSample(in SamplingParameters samplingParame

private sealed class MyInstrumentation : IDisposable
{
internal TracerProvider Provider;
internal bool Disposed;

public void Dispose()
Expand Down

0 comments on commit 4376564

Please sign in to comment.