Skip to content

Commit

Permalink
[otlp] Workaround DI containers which create unregistered services (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Aug 29, 2024
1 parent ba8a0e4 commit f3a22c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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.
}
}
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Notes](../../RELEASENOTES.md).
`CultureInfo.InvariantCulture`.
([#5700](https://github.com/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://github.com/open-telemetry/opentelemetry-dotnet/pull/5808))

## 1.9.0

Released 2024-Jun-14
Expand Down

0 comments on commit f3a22c3

Please sign in to comment.