Skip to content

Commit

Permalink
[Logs] Support dependency injection in ResourceBuilder and load envva…
Browse files Browse the repository at this point in the history
…rs from IConfiguration (#3889)

* Fixed logging resource builder not being attached to IServiceProvider.

* CHANGELOG patch.

Co-authored-by: Alan West <[email protected]>
  • Loading branch information
CodeBlanch and alanwest authored Nov 11, 2022
1 parent b5e50b2 commit ddcff7d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand Down
14 changes: 12 additions & 2 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,13 @@ public OpenTelemetryLoggerProvider(IOptionsMonitor<OpenTelemetryLoggerOptions> o
{
}

internal OpenTelemetryLoggerProvider(IServiceProvider serviceProvider)
: this(
serviceProvider: serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)),
options: serviceProvider?.GetRequiredService<IOptionsMonitor<OpenTelemetryLoggerOptions>>().CurrentValue!)
{
}

internal OpenTelemetryLoggerProvider()
: this(new OpenTelemetryLoggerOptions())
{
Expand All @@ -68,7 +76,7 @@ internal OpenTelemetryLoggerProvider(Action<OpenTelemetryLoggerOptions> configur
{
}

internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options)
internal OpenTelemetryLoggerProvider(OpenTelemetryLoggerOptions options, IServiceProvider? serviceProvider = null)
{
OpenTelemetrySdkEventSource.Log.OpenTelemetryLoggerProviderEvent("Building OpenTelemetryLoggerProvider.");

Expand All @@ -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)
{
Expand Down
4 changes: 3 additions & 1 deletion src/OpenTelemetry/Logs/OpenTelemetryLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Act

builder.AddConfiguration();

builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, OpenTelemetryLoggerProvider>());
builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<ILoggerProvider, OpenTelemetryLoggerProvider>(
sp => new OpenTelemetryLoggerProvider(sp)));

// Note: This will bind logger options element (eg "Logging:OpenTelemetry") to OpenTelemetryLoggerOptions
LoggerProviderOptions.RegisterProviderOptions<OpenTelemetryLoggerOptions, OpenTelemetryLoggerProvider>(builder.Services);
Expand Down
19 changes: 19 additions & 0 deletions test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
Expand All @@ -38,6 +40,23 @@ public void DefaultCtorTests()
Assert.NotNull(provider.Resource);
}

[Fact]
public void ResourceDetectionUsingIConfigurationTest()
{
var services = new ServiceCollection();

services.AddOptions();

services.AddSingleton<IConfiguration>(
new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string> { ["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()
{
Expand Down

0 comments on commit ddcff7d

Please sign in to comment.