Skip to content

Commit

Permalink
SDK: Forward SetParentProvider to children of CompositeProcessor (#3368)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Jun 16, 2022
1 parent b680ec6 commit a58c7a3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
null-valued tag.
([#3325](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3325))

* `CompositeProcessor` will now ensure `ParentProvider` is set on its children
([#3368](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3368))

## 1.3.0

Released 2022-Jun-03
Expand Down
10 changes: 10 additions & 0 deletions src/OpenTelemetry/CompositeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public override void OnStart(T data)
}
}

internal override void SetParentProvider(BaseProvider parentProvider)
{
base.SetParentProvider(parentProvider);

for (var cur = this.head; cur != null; cur = cur.Next)
{
cur.Value.SetParentProvider(parentProvider);
}
}

/// <inheritdoc/>
protected override bool OnForceFlush(int timeoutMilliseconds)
{
Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> proce
}
else
{
this.Processor = new CompositeProcessor<LogRecord>(new[]
var newCompositeProcessor = new CompositeProcessor<LogRecord>(new[]
{
this.Processor,
processor,
});
newCompositeProcessor.SetParentProvider(this);
newCompositeProcessor.AddProcessor(processor);
this.Processor = newCompositeProcessor;
}

return this;
Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry/Trace/TracerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,13 @@ internal TracerProviderSdk AddProcessor(BaseProcessor<Activity> processor)
}
else
{
this.processor = new CompositeProcessor<Activity>(new[]
var newCompositeProcessor = new CompositeProcessor<Activity>(new[]
{
this.processor,
processor,
});
newCompositeProcessor.SetParentProvider(this);
newCompositeProcessor.AddProcessor(processor);
this.processor = newCompositeProcessor;
}

return this;
Expand Down
25 changes: 25 additions & 0 deletions test/OpenTelemetry.Tests/Trace/CompositeActivityProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,30 @@ public void CompositeActivityProcessor_ForceFlush(int timeout)
Assert.True(p1.ForceFlushCalled);
Assert.True(p2.ForceFlushCalled);
}

[Fact]
public void CompositeActivityProcessor_ForwardsParentProvider()
{
using TracerProvider provider = new TestProvider();

using var p1 = new TestActivityProcessor(null, null);
using var p2 = new TestActivityProcessor(null, null);

using var processor = new CompositeProcessor<Activity>(new[] { p1, p2 });

Assert.Null(processor.ParentProvider);
Assert.Null(p1.ParentProvider);
Assert.Null(p2.ParentProvider);

processor.SetParentProvider(provider);

Assert.Equal(provider, processor.ParentProvider);
Assert.Equal(provider, p1.ParentProvider);
Assert.Equal(provider, p2.ParentProvider);
}

private sealed class TestProvider : TracerProvider
{
}
}
}
14 changes: 11 additions & 3 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,11 @@ public void SdkProcessesLegacyActivityEvenAfterAddingNewProcessor()

// AddLegacyOperationName chained to TracerProviderBuilder
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(testActivityProcessor)
.AddLegacySource(operationNameForLegacyActivity)
.Build();
.AddProcessor(testActivityProcessor)
.AddLegacySource(operationNameForLegacyActivity)
.Build();

Assert.Equal(tracerProvider, testActivityProcessor.ParentProvider);

Activity activity = new Activity(operationNameForLegacyActivity);
activity.Start();
Expand Down Expand Up @@ -736,6 +738,12 @@ public void SdkProcessesLegacyActivityEvenAfterAddingNewProcessor()

tracerProvider.AddProcessor(testActivityProcessorNew);

var sdkProvider = (TracerProviderSdk)tracerProvider;

Assert.True(sdkProvider.Processor is CompositeProcessor<Activity>);
Assert.Equal(tracerProvider, sdkProvider.Processor.ParentProvider);
Assert.Equal(tracerProvider, testActivityProcessorNew.ParentProvider);

Activity activityNew = new Activity(operationNameForLegacyActivity); // Create a new Activity with the same operation name
activityNew.Start();
activityNew.Stop();
Expand Down

0 comments on commit a58c7a3

Please sign in to comment.