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

[otlp] Workaround DI containers which create unregistered services #5808

Merged
merged 2 commits into from
Aug 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ private static void RegisterOtlpExporterServices(IServiceCollection services, st
services!.AddOtlpExporterTracingServices();

// Note: UseOtlpExporterRegistration is added to the service collection
// to detect repeated calls to "UseOtlpExporter" and to throw if
// "AddOtlpExporter" extensions are called
services!.AddSingleton<UseOtlpExporterRegistration>();
// for each invocation to detect repeated calls to "UseOtlpExporter" and
// to throw if "AddOtlpExporter" extensions are called
services!.AddSingleton(UseOtlpExporterRegistration.Instance);

services!.RegisterOptionsFactory((sp, configuration, name) => new OtlpExporterBuilderOptions(
configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ namespace OpenTelemetry.Exporter;
// calls to signal-specific AddOtlpExporter can throw.
internal sealed class UseOtlpExporterRegistration
{
public static readonly UseOtlpExporterRegistration Instance = new();

private UseOtlpExporterRegistration()
{
// Note: Some dependency injection containers (ex: Unity, Grace) will
// automatically create services if they have a public constructor even
// if the service was never registered into the IServiceCollection. The
// behavior of UseOtlpExporterRegistration requires that it should only
// exist if registered. This private constructor is intended to prevent
// automatic instantiation.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Notes](../../RELEASENOTES.md).
`CultureInfo.InvariantCulture`.
([#5700](https:/open-telemetry/opentelemetry-dotnet/pull/5700))

* Fixed an issue causing `NotSupportedException`s to be thrown on startup when
`AddOtlpExporter` registration extensions are called while using custom
dependency injection containers which automatically create services (Unity,
Grace, etc.).
([#5808](https:/open-telemetry/opentelemetry-dotnet/pull/5808))

## 1.9.0

Released 2024-Jun-14
Expand Down