diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/Experimental/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/Experimental/PublicAPI.Unshipped.txt index f4cb24cb080..e6bd747c9de 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/Experimental/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/.publicApi/Experimental/PublicAPI.Unshipped.txt @@ -1,5 +1,5 @@ static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder) -> OpenTelemetry.Logs.LoggerProviderBuilder! -static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, string? name, System.Action! configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder! +static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, string? name, System.Action? configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder! static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, string? name, System.Action? configureExporter) -> OpenTelemetry.Logs.LoggerProviderBuilder! static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, System.Action! configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder! static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, System.Action! configureExporter) -> OpenTelemetry.Logs.LoggerProviderBuilder! diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs index 565f33f8b41..9bde5bb44aa 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs @@ -153,7 +153,7 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder) internal #endif static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action configureExporter) - => AddOtlpExporter(builder, name: null, configureExporter: configureExporter); + => AddOtlpExporter(builder, name: null, configureExporter); #if EXPOSE_EXPERIMENTAL_FEATURES /// @@ -173,7 +173,7 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, internal #endif static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action configureExporterAndProcessor) - => AddOtlpExporter(builder, name: null, configureExporterAndProcessor: configureExporterAndProcessor); + => AddOtlpExporter(builder, name: null, configureExporterAndProcessor); #if EXPOSE_EXPERIMENTAL_FEATURES /// @@ -181,8 +181,8 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, /// /// WARNING: This is an experimental API which might change or be removed in the future. Use at your own risk. /// builder to use. - /// Name which is used when retrieving options. - /// Callback action for configuring . + /// Optional name which is used when retrieving options. + /// Optional callback action for configuring . /// The instance of to chain the calls. #if NET8_0_OR_GREATER [Experimental(DiagnosticDefinitions.LoggerProviderExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)] @@ -193,8 +193,8 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, /// Adds OpenTelemetry Protocol (OTLP) exporter to the LoggerProvider. /// /// builder to use. - /// Name which is used when retrieving options. - /// Callback action for configuring . + /// Optional name which is used when retrieving options. + /// Optional callback action for configuring . /// The instance of to chain the calls. internal #endif @@ -214,8 +214,7 @@ static LoggerProviderBuilder AddOtlpExporter( services.Configure(finalOptionsName, configureExporter); } - OtlpExporterOptions.RegisterOtlpExporterOptionsFactory(services); - services.RegisterOptionsFactory(configuration => new SdkLimitOptions(configuration)); + RegisterOptions(services); }); return builder.AddProcessor(sp => @@ -245,12 +244,14 @@ static LoggerProviderBuilder AddOtlpExporter( // There should only be one provider for a given service // collection so SdkLimitOptions is treated as a single default // instance. - var sdkOptionsManager = sp.GetRequiredService>().CurrentValue; + var sdkLimitOptions = sp.GetRequiredService>().CurrentValue; - return BuildOtlpLogExporterProcessor( + return BuildOtlpLogExporter( + sp, exporterOptions, sp.GetRequiredService>().Get(finalOptionsName), - sdkOptionsManager); + sdkLimitOptions, + sp.GetRequiredService>().Get(finalOptionsName)); }); } @@ -260,8 +261,8 @@ static LoggerProviderBuilder AddOtlpExporter( /// /// WARNING: This is an experimental API which might change or be removed in the future. Use at your own risk. /// builder to use. - /// Name which is used when retrieving options. - /// Callback action for + /// Optional name which is used when retrieving options. + /// Optional callback action for /// configuring and . /// The instance of to chain the calls. @@ -274,8 +275,8 @@ static LoggerProviderBuilder AddOtlpExporter( /// Adds an OTLP exporter to the LoggerProvider. /// /// builder to use. - /// Name which is used when retrieving options. - /// Callback action for + /// Optional name which is used when retrieving options. + /// Optional callback action for /// configuring and . /// The instance of to chain the calls. @@ -284,20 +285,15 @@ static LoggerProviderBuilder AddOtlpExporter( static LoggerProviderBuilder AddOtlpExporter( this LoggerProviderBuilder builder, string? name, - Action configureExporterAndProcessor) + Action? configureExporterAndProcessor) { var finalOptionsName = name ?? Options.DefaultName; - builder.ConfigureServices(services => - { - OtlpExporterOptions.RegisterOtlpExporterOptionsFactory(services); - services.RegisterOptionsFactory(configuration => new SdkLimitOptions(configuration)); - }); + builder.ConfigureServices(RegisterOptions); return builder.AddProcessor(sp => { OtlpExporterOptions exporterOptions; - LogRecordExportProcessorOptions processorOptions; if (name == null) { @@ -307,29 +303,31 @@ static LoggerProviderBuilder AddOtlpExporter( // name, delegates for all signals will mix together. See: // https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043 exporterOptions = sp.GetRequiredService>().Create(finalOptionsName); - processorOptions = sp.GetRequiredService>().Create(finalOptionsName); - - // Configuration delegate is executed inline on the fresh instance. - configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions); } else { // When using named options we can properly utilize Options // API to create or reuse an instance. exporterOptions = sp.GetRequiredService>().Get(finalOptionsName); - processorOptions = sp.GetRequiredService>().Get(finalOptionsName); } + var processorOptions = sp.GetRequiredService>().Get(finalOptionsName); + + // Configuration delegate is executed inline. + configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions); + // Note: Not using finalOptionsName 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>().CurrentValue; + var sdkLimitOptions = sp.GetRequiredService>().CurrentValue; - return BuildOtlpLogExporterProcessor( + return BuildOtlpLogExporter( + sp, exporterOptions, processorOptions, - sdkOptionsManager); + sdkLimitOptions, + sp.GetRequiredService>().Get(finalOptionsName)); }); } @@ -340,43 +338,31 @@ internal static BaseProcessor BuildOtlpLogExporter( Func, BaseExporter>? configureExporterInstance = null) { Debug.Assert(sp != null, "sp was null"); - Debug.Assert(exporterOptions != null, "exporterOptions was null"); - Debug.Assert(processorOptions != null, "processorOptions was null"); var config = sp!.GetRequiredService(); var sdkLimitOptions = new SdkLimitOptions(config); var experimentalOptions = new ExperimentalOptions(config); - BaseExporter otlpExporter = new OtlpLogExporter( - exporterOptions!, - sdkLimitOptions, - experimentalOptions); - - if (configureExporterInstance != null) - { - otlpExporter = configureExporterInstance(otlpExporter); - } - - if (processorOptions!.ExportProcessorType == ExportProcessorType.Simple) - { - return new SimpleLogRecordExportProcessor(otlpExporter); - } - else - { - var batchOptions = processorOptions.BatchExportProcessorOptions; - - return new BatchLogRecordExportProcessor( - otlpExporter, - batchOptions.MaxQueueSize, - batchOptions.ScheduledDelayMilliseconds, - batchOptions.ExporterTimeoutMilliseconds, - batchOptions.MaxExportBatchSize); - } + return BuildOtlpLogExporter(sp!, exporterOptions, processorOptions, sdkLimitOptions, experimentalOptions, configureExporterInstance); } - internal static BaseProcessor BuildOtlpLogExporterProcessor(OtlpExporterOptions exporterOptions, LogRecordExportProcessorOptions processorOptions, SdkLimitOptions sdkLimitOptions) + private static BaseProcessor BuildOtlpLogExporter( + IServiceProvider sp, + OtlpExporterOptions exporterOptions, + LogRecordExportProcessorOptions processorOptions, + SdkLimitOptions sdkLimitOptions, + ExperimentalOptions experimentalOptions, + Func, BaseExporter>? configureExporterInstance = null) { + // Note: sp is not currently used by this method but it should be used + // at some point for IHttpClientFactory integration. + Debug.Assert(sp != null, "sp was null"); + Debug.Assert(exporterOptions != null, "exporterOptions was null"); + Debug.Assert(processorOptions != null, "processorOptions was null"); + Debug.Assert(sdkLimitOptions != null, "sdkLimitOptions was null"); + Debug.Assert(experimentalOptions != null, "experimentalOptions was null"); + /* * Note: * @@ -393,23 +379,40 @@ internal static BaseProcessor BuildOtlpLogExporterProcessor(OtlpExpor * "OtlpLogExporter"); */ - BaseExporter otlpExporter = new OtlpLogExporter(exporterOptions, sdkLimitOptions, experimentalOptions: new()); + BaseExporter otlpExporter = new OtlpLogExporter( + exporterOptions!, + sdkLimitOptions!, + experimentalOptions!); - if (processorOptions.ExportProcessorType == ExportProcessorType.Simple) + if (configureExporterInstance != null) + { + otlpExporter = configureExporterInstance(otlpExporter); + } + + if (processorOptions!.ExportProcessorType == ExportProcessorType.Simple) { return new SimpleLogRecordExportProcessor(otlpExporter); } else { + var batchOptions = processorOptions.BatchExportProcessorOptions; + return new BatchLogRecordExportProcessor( otlpExporter, - processorOptions.BatchExportProcessorOptions.MaxQueueSize, - processorOptions.BatchExportProcessorOptions.ScheduledDelayMilliseconds, - processorOptions.BatchExportProcessorOptions.ExporterTimeoutMilliseconds, - processorOptions.BatchExportProcessorOptions.MaxExportBatchSize); + batchOptions.MaxQueueSize, + batchOptions.ScheduledDelayMilliseconds, + batchOptions.ExporterTimeoutMilliseconds, + batchOptions.MaxExportBatchSize); } } + private static void RegisterOptions(IServiceCollection services) + { + OtlpExporterOptions.RegisterOtlpExporterOptionsFactory(services); + services.RegisterOptionsFactory(configuration => new SdkLimitOptions(configuration)); + services.RegisterOptionsFactory(configuration => new ExperimentalOptions(configuration)); + } + private static OtlpExporterOptions GetOtlpExporterOptions(IServiceProvider sp, string? name, string finalName) { // Note: If OtlpExporter has been registered for tracing and/or metrics