From 1b9290e0e7a8d090d2d2cfed26da6f4e4aa6d9fb Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Thu, 27 Apr 2023 13:28:21 -0700 Subject: [PATCH] [sdk-logs] Add LoggerProviderBuilderExtensions to SDK (#4450) --- .../LoggerProviderBuilderExtensions.cs | 161 ++++++++++++++++++ .../Logs/Builder/LoggerProviderBuilderSdk.cs | 4 +- 2 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderExtensions.cs diff --git a/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderExtensions.cs b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderExtensions.cs new file mode 100644 index 00000000000..78364924e03 --- /dev/null +++ b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderExtensions.cs @@ -0,0 +1,161 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#nullable enable + +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using OpenTelemetry.Internal; +using OpenTelemetry.Resources; + +namespace OpenTelemetry.Logs; + +/// +/// Contains extension methods for the class. +/// +internal static class LoggerProviderBuilderExtensions +{ + /// + /// Sets the from which the Resource associated with + /// this provider is built from. Overwrites currently set ResourceBuilder. + /// You should usually use instead + /// (call if desired). + /// + /// . + /// from which Resource will be built. + /// Returns for chaining. + public static LoggerProviderBuilder SetResourceBuilder(this LoggerProviderBuilder loggerProviderBuilder, ResourceBuilder resourceBuilder) + { + Guard.ThrowIfNull(resourceBuilder); + + loggerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is LoggerProviderBuilderSdk loggerProviderBuilderSdk) + { + loggerProviderBuilderSdk.SetResourceBuilder(resourceBuilder); + } + }); + + return loggerProviderBuilder; + } + + /// + /// Modify the from which the Resource associated with + /// this provider is built from in-place. + /// + /// . + /// An action which modifies the provided in-place. + /// Returns for chaining. + public static LoggerProviderBuilder ConfigureResource(this LoggerProviderBuilder loggerProviderBuilder, Action configure) + { + Guard.ThrowIfNull(configure); + + loggerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is LoggerProviderBuilderSdk loggerProviderBuilderSdk) + { + loggerProviderBuilderSdk.ConfigureResource(configure); + } + }); + + return loggerProviderBuilder; + } + + /// + /// Adds a processor to the provider. + /// + /// . + /// LogRecord processor to add. + /// Returns for chaining. + public static LoggerProviderBuilder AddProcessor(this LoggerProviderBuilder loggerProviderBuilder, BaseProcessor processor) + { + Guard.ThrowIfNull(processor); + + loggerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is LoggerProviderBuilderSdk loggerProviderBuilderSdk) + { + loggerProviderBuilderSdk.AddProcessor(processor); + } + }); + + return loggerProviderBuilder; + } + + /// + /// Adds a processor to the provider which will be retrieved using dependency injection. + /// + /// + /// Note: The type specified by will be + /// registered as a singleton service into application services. + /// + /// Processor type. + /// . + /// The supplied for chaining. + public static LoggerProviderBuilder AddProcessor(this LoggerProviderBuilder loggerProviderBuilder) + where T : BaseProcessor + { + loggerProviderBuilder.ConfigureServices(services => services.TryAddSingleton()); + + loggerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is LoggerProviderBuilderSdk loggerProviderBuilderSdk) + { + loggerProviderBuilderSdk.AddProcessor(sp.GetRequiredService()); + } + }); + + return loggerProviderBuilder; + } + + /// + /// Adds a processor to the provider which will be retrieved using dependency injection. + /// + /// . + /// The factory that creates the service. + /// The supplied for chaining. + public static LoggerProviderBuilder AddProcessor( + this LoggerProviderBuilder loggerProviderBuilder, + Func> implementationFactory) + { + Guard.ThrowIfNull(implementationFactory); + + loggerProviderBuilder.ConfigureBuilder((sp, builder) => + { + if (builder is LoggerProviderBuilderSdk loggerProviderBuilderSdk) + { + loggerProviderBuilderSdk.AddProcessor(implementationFactory(sp)); + } + }); + + return loggerProviderBuilder; + } + + /// + /// Run the given actions to initialize the . + /// + /// . + /// . + public static LoggerProvider Build(this LoggerProviderBuilder loggerProviderBuilder) + { + if (loggerProviderBuilder is LoggerProviderServiceCollectionBuilder loggerProviderServiceCollectionBuilder) + { + return loggerProviderServiceCollectionBuilder.Build(); + } + + return new LoggerProvider(); + } +} diff --git a/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs index c55469d00a3..d9aea4c262c 100644 --- a/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs +++ b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs @@ -43,7 +43,7 @@ public LoggerProviderBuilderSdk(IServiceProvider serviceProvider) public LoggerProvider? Provider => this.loggerProvider; - public List> Processors { get; } = new(); + public List> Processors { get; } = new(); public void RegisterProvider(LoggerProviderSdk loggerProvider) { @@ -91,7 +91,7 @@ public LoggerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) return this; } - public LoggerProviderBuilder AddProcessor(BaseProcessor processor) + public LoggerProviderBuilder AddProcessor(BaseProcessor processor) { Debug.Assert(processor != null, "processor was null");