diff --git a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt index c9041c2b48c..955d808767a 100644 --- a/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net462/PublicAPI.Unshipped.txt @@ -23,3 +23,4 @@ static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.SetExemplarFilter(th ~override OpenTelemetry.Metrics.AlwaysOnExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(double value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! \ No newline at end of file diff --git a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt index c9041c2b48c..b334cfe1664 100644 --- a/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net6.0/PublicAPI.Unshipped.txt @@ -23,3 +23,4 @@ static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.SetExemplarFilter(th ~override OpenTelemetry.Metrics.AlwaysOnExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(double value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index c9041c2b48c..b334cfe1664 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -23,3 +23,4 @@ static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.SetExemplarFilter(th ~override OpenTelemetry.Metrics.AlwaysOnExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(double value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt index c9041c2b48c..b334cfe1664 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.1/PublicAPI.Unshipped.txt @@ -23,3 +23,4 @@ static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.SetExemplarFilter(th ~override OpenTelemetry.Metrics.AlwaysOnExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(double value, System.ReadOnlySpan> tags) -> bool ~override OpenTelemetry.Metrics.TraceBasedExemplarFilter.ShouldSample(long value, System.ReadOnlySpan> tags) -> bool +OpenTelemetry.Resources.ResourceBuilder.AddDetector(System.Func! resourceDetectorFactory) -> OpenTelemetry.Resources.ResourceBuilder! diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index a7535102bb7..4036a76749a 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -5,6 +5,9 @@ * Added Exemplar support. ([#4217](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4217)) +* Added `AddDetector` factory overload on `ResourceBuilder`. + ([#4261](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4261)) + ## 1.4.0 Released 2023-Feb-24 diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs index b2e6e2311e7..e46e2851c95 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs @@ -80,6 +80,8 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServic Guard.ThrowIfNull(options); + this.ServiceProvider = serviceProvider; + this.IncludeScopes = options.IncludeScopes; this.IncludeFormattedMessage = options.IncludeFormattedMessage; this.ParseStateValues = options.ParseStateValues; @@ -96,6 +98,8 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServic OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("OpenTelemetryLoggerProvider built successfully."); } + internal IServiceProvider? ServiceProvider { get; } + internal IExternalScopeProvider? ScopeProvider { get; private set; } internal ILogRecordPool LogRecordPool => this.threadStaticPool ?? LogRecordSharedPool.Current; diff --git a/src/OpenTelemetry/ProviderExtensions.cs b/src/OpenTelemetry/ProviderExtensions.cs index c83127fdc51..b25753e71ef 100644 --- a/src/OpenTelemetry/ProviderExtensions.cs +++ b/src/OpenTelemetry/ProviderExtensions.cs @@ -73,6 +73,10 @@ public static Resource GetDefaultResource(this BaseProvider baseProvider) { return meterProviderSdk.ServiceProvider; } + else if (baseProvider is OpenTelemetryLoggerProvider openTelemetryLoggerProvider) + { + return openTelemetryLoggerProvider.ServiceProvider; + } return null; } diff --git a/src/OpenTelemetry/Resources/ResourceBuilder.cs b/src/OpenTelemetry/Resources/ResourceBuilder.cs index 44657001c9d..c9de4fa77e7 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilder.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilder.cs @@ -132,18 +132,24 @@ public ResourceBuilder AddDetector(IResourceDetector resourceDetector) /// /// Add a to the builder which will be resolved using the application . /// - /// - /// Note: The supplied may be - /// called with a - /// for detached instances. Factories - /// should either throw if a cannot be handled, - /// or return a default when is not available. - /// /// Resource detector factory. /// Supplied for call chaining. - // Note: This API may be made public if there is a need for it. - internal ResourceBuilder AddDetector(Func resourceDetectorFactory) + public ResourceBuilder AddDetector(Func resourceDetectorFactory) + { + Guard.ThrowIfNull(resourceDetectorFactory); + + return this.AddDetectorInternal(sp => + { + if (sp == null) + { + throw new NotSupportedException("IResourceDetector factory pattern is not supported when calling ResourceBuilder.Build() directly."); + } + + return resourceDetectorFactory(sp); + }); + } + + internal ResourceBuilder AddDetectorInternal(Func resourceDetectorFactory) { Guard.ThrowIfNull(resourceDetectorFactory); diff --git a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs index 3fcaf4b2b64..8353a6c465b 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs @@ -122,8 +122,8 @@ public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilde Lazy configuration = new Lazy(() => new ConfigurationBuilder().AddEnvironmentVariables().Build()); return resourceBuilder - .AddDetector(sp => new OtelEnvResourceDetector(sp?.GetService() ?? configuration.Value)) - .AddDetector(sp => new OtelServiceNameEnvVarDetector(sp?.GetService() ?? configuration.Value)); + .AddDetectorInternal(sp => new OtelEnvResourceDetector(sp?.GetService() ?? configuration.Value)) + .AddDetectorInternal(sp => new OtelServiceNameEnvVarDetector(sp?.GetService() ?? configuration.Value)); } private static string GetFileVersion() diff --git a/test/OpenTelemetry.Tests/Metrics/MeterProviderBuilderExtensionsTests.cs b/test/OpenTelemetry.Tests/Metrics/MeterProviderBuilderExtensionsTests.cs index f4012b52d11..e3e3857dfd6 100644 --- a/test/OpenTelemetry.Tests/Metrics/MeterProviderBuilderExtensionsTests.cs +++ b/test/OpenTelemetry.Tests/Metrics/MeterProviderBuilderExtensionsTests.cs @@ -108,7 +108,7 @@ public void SetAndConfigureResourceTest() Assert.Empty(builder.ResourceDetectors); - builder.AddDetector(sp => + builder.AddDetectorInternal(sp => { serviceProviderTestExecuted = true; Assert.NotNull(sp); diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index 419f78281a8..c1ab60fe0c6 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -498,13 +498,39 @@ public void GetResource_WithServiceNameSetWithTwoEnvVarsAndCode() } [Fact] - public void ResourceBuilder_ServiceProvider_Available() + public void ResourceBuilder_AddDetector_Test() + { + bool factoryExecuted = false; + + var builder = ResourceBuilder.CreateDefault(); + + builder.AddDetector(sp => + { + factoryExecuted = true; + return new NoopResourceDetector(); + }); + + Assert.Throws(() => builder.Build()); + Assert.False(factoryExecuted); + + var serviceCollection = new ServiceCollection(); + using var serviceProvider = serviceCollection.BuildServiceProvider(); + + builder.ServiceProvider = serviceProvider; + + var resource = builder.Build(); + + Assert.True(factoryExecuted); + } + + [Fact] + public void ResourceBuilder_AddDetectorInternal_Test() { var builder = ResourceBuilder.CreateDefault(); bool nullTestRun = false; - builder.AddDetector(sp => + builder.AddDetectorInternal(sp => { nullTestRun = true; Assert.Null(sp); @@ -524,7 +550,7 @@ public void ResourceBuilder_ServiceProvider_Available() builder.ServiceProvider = serviceProvider; - builder.AddDetector(sp => + builder.AddDetectorInternal(sp => { validTestRun = true; Assert.NotNull(sp); diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs index 8b042d488ae..252f749f4bb 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderBuilderExtensionsTest.cs @@ -196,7 +196,7 @@ public void SetAndConfigureResourceTest() Assert.Empty(builder.ResourceDetectors); - builder.AddDetector(sp => + builder.AddDetectorInternal(sp => { serviceProviderTestExecuted = true; Assert.NotNull(sp);