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

[Logs] Support dependency injection in ResourceBuilder and load envvars from IConfiguration #3889

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
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
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