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

SDK: Forward SetParentProvider to children of CompositeProcessor #3368

Merged
Show file tree
Hide file tree
Changes from all commits
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
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
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