Skip to content

Commit

Permalink
Tweaks and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Dec 13, 2023
1 parent fa4d229 commit 04a036c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -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<OpenTelemetry.Exporter.OtlpExporterOptions!, OpenTelemetry.Logs.LogRecordExportProcessorOptions!>! configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions!, OpenTelemetry.Logs.LogRecordExportProcessorOptions!>? configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions!>? configureExporter) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions!, OpenTelemetry.Logs.LogRecordExportProcessorOptions!>! configureExporterAndProcessor) -> OpenTelemetry.Logs.LoggerProviderBuilder!
static OpenTelemetry.Logs.OtlpLogExporterHelperExtensions.AddOtlpExporter(this OpenTelemetry.Logs.LoggerProviderBuilder! builder, System.Action<OpenTelemetry.Exporter.OtlpExporterOptions!>! configureExporter) -> OpenTelemetry.Logs.LoggerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder)
internal
#endif
static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action<OtlpExporterOptions> configureExporter)
=> AddOtlpExporter(builder, name: null, configureExporter: configureExporter);
=> AddOtlpExporter(builder, name: null, configureExporter);

#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
Expand All @@ -173,16 +173,16 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder,
internal
#endif
static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action<OtlpExporterOptions, LogRecordExportProcessorOptions> configureExporterAndProcessor)
=> AddOtlpExporter(builder, name: null, configureExporterAndProcessor: configureExporterAndProcessor);
=> AddOtlpExporter(builder, name: null, configureExporterAndProcessor);

#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
/// Adds OpenTelemetry Protocol (OTLP) exporter to the LoggerProvider.
/// </summary>
/// <remarks><b>WARNING</b>: This is an experimental API which might change or be removed in the future. Use at your own risk.</remarks>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureExporter">Callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporter">Optional callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
#if NET8_0_OR_GREATER
[Experimental(DiagnosticDefinitions.LoggerProviderExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
Expand All @@ -193,8 +193,8 @@ static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder,
/// Adds OpenTelemetry Protocol (OTLP) exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureExporter">Callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporter">Optional callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
internal
#endif
Expand All @@ -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 =>
Expand Down Expand Up @@ -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<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;

return BuildOtlpLogExporterProcessor(
return BuildOtlpLogExporter(
sp,
exporterOptions,
sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName),
sdkOptionsManager);
sdkLimitOptions,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(finalOptionsName));
});
}

Expand All @@ -260,8 +261,8 @@ static LoggerProviderBuilder AddOtlpExporter(
/// </summary>
/// <remarks><b>WARNING</b>: This is an experimental API which might change or be removed in the future. Use at your own risk.</remarks>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Callback action for
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Optional callback action for
/// configuring <see cref="OtlpExporterOptions"/> and <see
/// cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
Expand All @@ -274,8 +275,8 @@ static LoggerProviderBuilder AddOtlpExporter(
/// Adds an OTLP exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Callback action for
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Optional callback action for
/// configuring <see cref="OtlpExporterOptions"/> and <see
/// cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
Expand All @@ -284,20 +285,15 @@ static LoggerProviderBuilder AddOtlpExporter(
static LoggerProviderBuilder AddOtlpExporter(
this LoggerProviderBuilder builder,
string? name,
Action<OtlpExporterOptions, LogRecordExportProcessorOptions> configureExporterAndProcessor)
Action<OtlpExporterOptions, LogRecordExportProcessorOptions>? 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)
{
Expand All @@ -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<IOptionsFactory<OtlpExporterOptions>>().Create(finalOptionsName);
processorOptions = sp.GetRequiredService<IOptionsFactory<LogRecordExportProcessorOptions>>().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<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
processorOptions = sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName);
}

var processorOptions = sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().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<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;

return BuildOtlpLogExporterProcessor(
return BuildOtlpLogExporter(
sp,
exporterOptions,
processorOptions,
sdkOptionsManager);
sdkLimitOptions,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(finalOptionsName));
});
}

Expand All @@ -340,43 +338,31 @@ internal static BaseProcessor<LogRecord> BuildOtlpLogExporter(
Func<BaseExporter<LogRecord>, BaseExporter<LogRecord>>? 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<IConfiguration>();

var sdkLimitOptions = new SdkLimitOptions(config);
var experimentalOptions = new ExperimentalOptions(config);

BaseExporter<LogRecord> 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<LogRecord> BuildOtlpLogExporterProcessor(OtlpExporterOptions exporterOptions, LogRecordExportProcessorOptions processorOptions, SdkLimitOptions sdkLimitOptions)
private static BaseProcessor<LogRecord> BuildOtlpLogExporter(
IServiceProvider sp,
OtlpExporterOptions exporterOptions,
LogRecordExportProcessorOptions processorOptions,
SdkLimitOptions sdkLimitOptions,
ExperimentalOptions experimentalOptions,
Func<BaseExporter<LogRecord>, BaseExporter<LogRecord>>? 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:
*
Expand All @@ -393,23 +379,40 @@ internal static BaseProcessor<LogRecord> BuildOtlpLogExporterProcessor(OtlpExpor
* "OtlpLogExporter");
*/

BaseExporter<LogRecord> otlpExporter = new OtlpLogExporter(exporterOptions, sdkLimitOptions, experimentalOptions: new());
BaseExporter<LogRecord> 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
Expand Down

0 comments on commit 04a036c

Please sign in to comment.