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-branch] Support loading OtlpLogExporter envvars from IConfiguration #3812

Merged
merged 2 commits into from
Oct 24, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
management
([#3707](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3707))

* `OtlpExporterOptions` can now be bound to `IConfiguation` when using the
`OtlpLogExporterHelperExtensions.AddOtlpExporter` extension
([#3812](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3812))

## Unreleased

## 1.4.0-beta.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Logs
Expand Down Expand Up @@ -102,16 +103,27 @@ public static LoggerProviderBuilder AddOtlpExporter(

name ??= Options.DefaultName;

if (configure != null)
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services => services.Configure(name, configure));
}
if (configure != null)
{
services.Configure(name, configure);
}

services.RegisterOptionsFactory(configuration => new SdkLimitOptions(configuration));
services.RegisterOptionsFactory(configuration => new OtlpExporterOptions(configuration));
});

builder.ConfigureBuilder((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(name);
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(name);

AddOtlpExporter(builder, options, sp);
// Note: Not using name here for SdkLimitOptions. There should
// only be one provider for a given service collection so
// SdkLimitOptions is treated as a single default instance.
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;

AddOtlpExporter(builder, exporterOptions, sdkLimitOptions, sp);
});

return builder;
Expand All @@ -120,10 +132,12 @@ public static LoggerProviderBuilder AddOtlpExporter(
private static void AddOtlpExporter(
LoggerProviderBuilder builder,
OtlpExporterOptions exporterOptions,
SdkLimitOptions sdkLimitOptions,
IServiceProvider serviceProvider)
{
exporterOptions.TryEnableIHttpClientFactoryIntegration(serviceProvider, "OtlpLogExporter");
var otlpExporter = new OtlpLogExporter(exporterOptions);

var otlpExporter = new OtlpLogExporter(exporterOptions, sdkLimitOptions);

if (exporterOptions.ExportProcessorType == ExportProcessorType.Simple)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public static TracerProviderBuilder AddOtlpExporter(
// Note: Not using name here for SdkLimitOptions. There should
// only be one provider for a given service collection so
// SdkLimitOptions is treated as a single default instance.
var sdkOptionsManager = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;

AddOtlpExporter(builder, exporterOptions, sdkOptionsManager, sp);
AddOtlpExporter(builder, exporterOptions, sdkLimitOptions, sp);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ namespace Microsoft.Extensions.DependencyInjection;

internal static class ProviderBuilderServiceCollectionExtensions
{
public static IServiceCollection AddOpenTelemetryLoggerProviderBuilderServices(this IServiceCollection services)
{
services.AddOpenTelemetryProviderBuilderServices();

return services;
}

public static IServiceCollection AddOpenTelemetryMeterProviderBuilderServices(this IServiceCollection services)
{
services.AddOpenTelemetryProviderBuilderServices();
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public LoggerProviderBuilderSdk(IServiceCollection services)
{
Debug.Assert(services != null, "services was null");

services.AddOptions();
services!.AddOpenTelemetryLoggerProviderBuilderServices();
services!.TryAddSingleton<LoggerProvider>(sp => new LoggerProviderSdk(sp, ownsServiceProvider: false));

this.services = services;
Expand All @@ -66,7 +66,7 @@ public LoggerProviderBuilderSdk()
{
var services = new ServiceCollection();

services.AddOptions();
services.AddOpenTelemetryLoggerProviderBuilderServices();

this.services = services;
this.ownsServices = true;
Expand Down