diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 80ac8d6aa6b..f9a7f3013e4 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +* Added dependency injection support in the `ResourceBuilder` class and added + support for loading environment variables from `IConfiguration` for the + `AddEnvironmentVariableDetector` extension (Logs) + ([#3889](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3889)) + ## 1.4.0-beta.3 Released 2022-Nov-07 @@ -17,7 +22,7 @@ Released 2022-Nov-07 * Added dependency injection support in the `ResourceBuilder` class and added support for loading environment variables from `IConfiguration` for the - `AddEnvironmentVariableDetector` extension + `AddEnvironmentVariableDetector` extension (Traces & Metrics) ([#3782](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3782), [#3798](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3798)) diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs index 2d52d20399f..af48d1ab528 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs @@ -20,6 +20,7 @@ using System.Collections; using System.Text; using System.Threading; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using OpenTelemetry.Internal; @@ -58,6 +59,13 @@ public OpenTelemetryLoggerProvider(IOptionsMonitor o { } + internal OpenTelemetryLoggerProvider(IServiceProvider serviceProvider) + : this( + serviceProvider: serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)), + options: serviceProvider?.GetRequiredService>().CurrentValue!) + { + } + internal OpenTelemetryLoggerProvider() : this(new OpenTelemetryLoggerOptions()) { @@ -68,7 +76,7 @@ internal OpenTelemetryLoggerProvider(Action configur { } - internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options) + internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServiceProvider? serviceProvider = null) { OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("Building OpenTelemetryLoggerProvider."); @@ -78,7 +86,9 @@ internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options) this.IncludeFormattedMessage = options.IncludeFormattedMessage; this.ParseStateValues = options.ParseStateValues; - this.Resource = options.ResourceBuilder.Build(); + var resourceBuilder = options.ResourceBuilder; + resourceBuilder.ServiceProvider = serviceProvider; + this.Resource = resourceBuilder.Build(); foreach (var processor in options.Processors) { diff --git a/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs b/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs index 9a2f8607749..844a744dd2d 100644 --- a/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs +++ b/src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs @@ -57,7 +57,9 @@ public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Act builder.AddConfiguration(); - builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); + builder.Services.TryAddEnumerable( + ServiceDescriptor.Singleton( + sp => new OpenTelemetryLoggerProvider(sp))); // Note: This will bind logger options element (eg "Logging:OpenTelemetry") to OpenTelemetryLoggerOptions LoggerProviderOptions.RegisterProviderOptions(builder.Services); diff --git a/test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerProviderTests.cs b/test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerProviderTests.cs index 6ddd5d7caa4..c42729af674 100644 --- a/test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerProviderTests.cs +++ b/test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerProviderTests.cs @@ -15,6 +15,8 @@ // using System.Collections.Generic; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OpenTelemetry.Exporter; using OpenTelemetry.Resources; @@ -38,6 +40,23 @@ public void DefaultCtorTests() Assert.NotNull(provider.Resource); } + [Fact] + public void ResourceDetectionUsingIConfigurationTest() + { + var services = new ServiceCollection(); + + services.AddOptions(); + + services.AddSingleton( + new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["OTEL_SERVICE_NAME"] = "TestServiceName" }).Build()); + + using var serviceProvider = services.BuildServiceProvider(); + + using var loggerProvider = new OpenTelemetryLoggerProvider(serviceProvider); + + Assert.Contains(loggerProvider.Resource.Attributes, kvp => kvp.Key == "service.name" && (string)kvp.Value == "TestServiceName"); + } + [Fact] public void ConfigureCtorTests() {