diff --git a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs index c8ddae0b99a..2325bf3bec4 100644 --- a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs +++ b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs @@ -33,8 +33,7 @@ public static IServiceCollection AddOpenTelemetryLoggerProviderBuilderServices(t { Debug.Assert(services != null, "services was null"); - // TODO: - // services!.TryAddSingleton(); + services!.TryAddSingleton(); services!.RegisterOptionsFactory(configuration => new BatchExportLogRecordProcessorOptions(configuration)); return services!; diff --git a/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs new file mode 100644 index 00000000000..c55469d00a3 --- /dev/null +++ b/src/OpenTelemetry/Logs/Builder/LoggerProviderBuilderSdk.cs @@ -0,0 +1,133 @@ +// +// 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 System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using OpenTelemetry.Resources; + +namespace OpenTelemetry.Logs; + +/// +/// Stores state used to build a . +/// +internal sealed class LoggerProviderBuilderSdk : LoggerProviderBuilder, ILoggerProviderBuilder +{ + private const string DefaultInstrumentationVersion = "1.0.0.0"; + + private readonly IServiceProvider serviceProvider; + private LoggerProviderSdk? loggerProvider; + + public LoggerProviderBuilderSdk(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + + public List Instrumentation { get; } = new(); + + public ResourceBuilder? ResourceBuilder { get; private set; } + + public LoggerProvider? Provider => this.loggerProvider; + + public List> Processors { get; } = new(); + + public void RegisterProvider(LoggerProviderSdk loggerProvider) + { + Debug.Assert(loggerProvider != null, "loggerProvider was null"); + + if (this.loggerProvider != null) + { + throw new NotSupportedException("LoggerProvider cannot be accessed while build is executing."); + } + + this.loggerProvider = loggerProvider; + } + + public override LoggerProviderBuilder AddInstrumentation( + Func instrumentationFactory) + { + Debug.Assert(instrumentationFactory != null, "instrumentationFactory was null"); + + this.Instrumentation.Add( + new InstrumentationRegistration( + typeof(TInstrumentation).Name, + typeof(TInstrumentation).Assembly.GetName().Version?.ToString() ?? DefaultInstrumentationVersion, + instrumentationFactory!()!)); + + return this; + } + + public LoggerProviderBuilder ConfigureResource(Action configure) + { + Debug.Assert(configure != null, "configure was null"); + + var resourceBuilder = this.ResourceBuilder ??= ResourceBuilder.CreateDefault(); + + configure!(resourceBuilder); + + return this; + } + + public LoggerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder) + { + Debug.Assert(resourceBuilder != null, "resourceBuilder was null"); + + this.ResourceBuilder = resourceBuilder; + + return this; + } + + public LoggerProviderBuilder AddProcessor(BaseProcessor processor) + { + Debug.Assert(processor != null, "processor was null"); + + this.Processors.Add(processor!); + + return this; + } + + public LoggerProviderBuilder ConfigureBuilder(Action configure) + { + Debug.Assert(configure != null, "configure was null"); + + configure!(this.serviceProvider, this); + + return this; + } + + public LoggerProviderBuilder ConfigureServices(Action configure) + { + throw new NotSupportedException("Services cannot be configured after ServiceProvider has been created."); + } + + LoggerProviderBuilder IDeferredLoggerProviderBuilder.Configure(Action configure) + => this.ConfigureBuilder(configure); + + internal readonly struct InstrumentationRegistration + { + public readonly string Name; + public readonly string Version; + public readonly object Instance; + + internal InstrumentationRegistration(string name, string version, object instance) + { + this.Name = name; + this.Version = version; + this.Instance = instance; + } + } +}