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

[sdk-logs] Introduce ConfigureOpenTelemetry ILoggingBuilder extension (proposal 1) #4496

Closed
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 @@ -18,6 +18,7 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;

Expand All @@ -28,6 +29,24 @@ namespace OpenTelemetry.Logs;
/// </summary>
internal static class LoggerProviderBuilderExtensions
{
/// <summary>
/// Registers a configuration action for the <see
/// cref="OpenTelemetryLoggerOptions"/> used by <see cref="ILogger"/>
/// integration (<see cref="OpenTelemetryLoggerProvider"/>).
/// </summary>
/// <param name="loggerProviderBuilder"><see cref="LoggerProviderBuilder"/>.</param>
/// <param name="configure">Configuration action.</param>
/// <returns>Returns <see cref="LoggerProviderBuilder"/> for chaining.</returns>
public static LoggerProviderBuilder ConfigureLoggerOptions(
this LoggerProviderBuilder loggerProviderBuilder,
Action<OpenTelemetryLoggerOptions> configure)
{
Guard.ThrowIfNull(configure);

return loggerProviderBuilder.ConfigureServices(
services => services.Configure(configure));
}

/// <summary>
/// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
/// this provider is built from. Overwrites currently set ResourceBuilder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class OpenTelemetryLoggerOptions
/// </summary>
/// <param name="processor">Log processor to add.</param>
/// <returns>Returns <see cref="OpenTelemetryLoggerOptions"/> for chaining.</returns>
// todo: [Obsolete("Call ConfigureOpenTelemetry to configure the LoggerProvider and manage processors. The AddProcessor method on OpenTelemetryLoggerOptions will be removed in a future version.")]
public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor<LogRecord> processor)
{
Guard.ThrowIfNull(processor);
Expand All @@ -104,6 +105,7 @@ public OpenTelemetryLoggerOptions AddProcessor(BaseProcessor<LogRecord> processo
/// </summary>
/// <param name="resourceBuilder"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
/// <returns>Returns <see cref="OpenTelemetryLoggerOptions"/> for chaining.</returns>
// todo: [Obsolete("Call ConfigureOpenTelemetry to configure the LoggerProvider and manage resources. The SetResourceBuilder method on OpenTelemetryLoggerOptions will be removed in a future version.")]
public OpenTelemetryLoggerOptions SetResourceBuilder(ResourceBuilder resourceBuilder)
{
Guard.ThrowIfNull(resourceBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static ILoggingBuilder AddOpenTelemetry(
// Note: This will bind logger options element (eg "Logging:OpenTelemetry") to OpenTelemetryLoggerOptions
LoggerProviderOptions.RegisterProviderOptions<OpenTelemetryLoggerOptions, OpenTelemetryLoggerProvider>(builder.Services);

// Note: This code is to support legacy AddProcessor & SetResourceBuilder APIs on OpenTelemetryLoggerOptions.
new LoggerProviderServiceCollectionBuilder(builder.Services).ConfigureBuilder(
(sp, logging) =>
{
Expand All @@ -71,6 +72,8 @@ public static ILoggingBuilder AddOpenTelemetry(
options.Processors.Clear();
});

builder.Services.AddOpenTelemetrySharedProviderBuilderServices();

builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<ILoggerProvider, OpenTelemetryLoggerProvider>(
sp => new OpenTelemetryLoggerProvider(
Expand Down Expand Up @@ -99,4 +102,28 @@ public static ILoggingBuilder AddOpenTelemetry(

return AddOpenTelemetry(builder);
}

/// <summary>
/// Configures the <see cref="LoggerProviderBuilder" /> which will be used
/// by the OpenTelemetry logger named 'OpenTelemetry' added to the <see
/// cref="ILoggerFactory"/>.
/// </summary>
/// Note: This is safe to be called multiple times and by library authors.
/// Only a single <see cref="OpenTelemetryLoggerProvider"/> and <see
/// cref="LoggerProvider"/> will be created for a given <see
/// cref="IServiceCollection"/>.
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="configure">Configuration action.</param>
/// <returns>The supplied <see cref="ILoggingBuilder"/> for call chaining.</returns>
internal static ILoggingBuilder ConfigureOpenTelemetry(
this ILoggingBuilder builder,
Action<LoggerProviderBuilder> configure)
{
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(configure);

configure(new LoggerProviderServiceCollectionBuilder(builder.Services));

return builder;
}
}