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] ConcurrencyMode and export processor registration APIs #5758

Closed
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
18 changes: 0 additions & 18 deletions docs/logs/extending-the-sdk/LoggerExtensions.cs

This file was deleted.

39 changes: 39 additions & 0 deletions docs/logs/extending-the-sdk/MyExporterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.Logs;

internal static class MyExporterExtensions
{
public static LoggerProviderBuilder AddMyExporter(
this LoggerProviderBuilder builder)
=> AddMyExporter(builder, name: null);

public static LoggerProviderBuilder AddMyExporter(
this LoggerProviderBuilder builder,
string? name)
{
return builder.AddBatchExportProcessor(
name,
new MyExporter());
}

public static OpenTelemetryLoggerOptions AddMyExporter(
this OpenTelemetryLoggerOptions options)
=> AddMyExporter(options, name: null);

public static OpenTelemetryLoggerOptions AddMyExporter(
this OpenTelemetryLoggerOptions options,
string? name)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

((IDeferredLoggerProviderBuilder)options).Configure(
(sp, builder) => builder.AddBatchExportProcessor(name, new MyExporter()));

return options;
}
}
11 changes: 9 additions & 2 deletions docs/logs/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ A demo exporter which simply writes log records to the console is shown
[here](./MyExporter.cs).

Apart from the exporter itself, you should also provide extension methods as
shown [here](./LoggerExtensions.cs). This allows users to add the Exporter to
the `OpenTelemetryLoggerOptions` as shown in the example [here](./Program.cs).
shown [here](./MyExporterExtensions.cs). This allows users to add the exporter
to the `OpenTelemetryLoggerOptions` (as shown in the example
[here](./Program.cs)) or to a `LoggerProviderBuilder` using the `WithLogging`
extension in `OpenTelemetry.Extensions.Hosting`.

## Processor

Expand All @@ -64,6 +66,11 @@ OpenTelemetry .NET SDK has provided the following built-in processors:
* [CompositeProcessor<T>](../../../src/OpenTelemetry/CompositeProcessor.cs)
* [SimpleExportProcessor<T>](../../../src/OpenTelemetry/SimpleExportProcessor.cs)

> [!NOTE]
> As of `1.10.0` it is recommended to use the `LoggerProviderBuilder`
> `AddBatchExportProcessor` or `AddSimpleExportProcessor` helper extension
> methods to create batch and/or simple processors.

Custom processors can be implemented to cover more scenarios:

* Processors should inherit from `OpenTelemetry.BaseProcessor<LogRecord>` (which
Expand Down
32 changes: 23 additions & 9 deletions docs/trace/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,32 @@ writing custom processors.

#### Exporter Configuration

The snippet below shows how to add export processors to the provider before it
The snippets below shows how to add export processors to the provider before it
is built.

```csharp
using OpenTelemetry;
using OpenTelemetry.Trace;
* Using 1.10.0 or newer

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(new BatchActivityExportProcessor(new MyExporter1()))
.AddProcessor(new SimpleActivityExportProcessor(new MyExporter2()))
.Build();
```
```csharp
using OpenTelemetry;
using OpenTelemetry.Trace;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddBatchExportProcessor(new MyExporter1())
.AddSimpleExportProcessor(new MyExporter2())
.Build();
```

* Using 1.9.0 or older

```csharp
using OpenTelemetry;
using OpenTelemetry.Trace;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(new BatchActivityExportProcessor(new MyExporter1()))
.AddProcessor(new SimpleActivityExportProcessor(new MyExporter2()))
.Build();
```

It is also common for exporters to provide their own extensions to simplify
registration. The snippet below shows how to add the
Expand Down
18 changes: 10 additions & 8 deletions docs/trace/extending-the-sdk/MyExporterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry;
using OpenTelemetry.Trace;

internal static class MyExporterExtensions
{
public static TracerProviderBuilder AddMyExporter(this TracerProviderBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
public static TracerProviderBuilder AddMyExporter(
this TracerProviderBuilder builder)
=> AddMyExporter(builder, name: null);

return builder.AddProcessor(new BatchActivityExportProcessor(new MyExporter()));
public static TracerProviderBuilder AddMyExporter(
this TracerProviderBuilder builder,
string? name)
{
return builder.AddBatchExportProcessor(
name,
new MyExporter());
}
}
5 changes: 5 additions & 0 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ OpenTelemetry .NET SDK has provided the following built-in processors:
* [CompositeProcessor&lt;T&gt;](../../../src/OpenTelemetry/CompositeProcessor.cs)
* [SimpleExportProcessor&lt;T&gt;](../../../src/OpenTelemetry/SimpleExportProcessor.cs)

> [!NOTE]
> As of `1.10.0` it is recommended to use the `TracerProviderBuilder`
> `AddBatchExportProcessor` or `AddSimpleExportProcessor` helper extension
> methods to create batch and/or simple processors.

Custom processors can be implemented to cover more scenarios:

* Processors should inherit from `OpenTelemetry.BaseProcessor<Activity>` (which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public static TracerProviderBuilder AddConsoleExporter(
builder.ConfigureServices(services => services.Configure(name, configure));
}

return builder.AddProcessor(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);

return new SimpleActivityExportProcessor(new ConsoleActivityExporter(options));
});
return builder.AddSimpleExportProcessor(
name,
static (sp, name) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);

return new ConsoleActivityExporter(options);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ public static LoggerProviderBuilder AddConsoleExporter(

if (configure != null)
{
loggerProviderBuilder.ConfigureServices(services => services.Configure(name, configure));
loggerProviderBuilder.ConfigureServices(
services => services.Configure(name, configure));
}

return loggerProviderBuilder.AddProcessor(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);
return loggerProviderBuilder.AddSimpleExportProcessor(
name,
static (sp, name) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ConsoleExporterOptions>>().Get(name);

return new SimpleLogRecordExportProcessor(new ConsoleLogRecordExporter(options));
});
return new ConsoleLogRecordExporter(options);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuild
Guard.ThrowIfNull(builder);
Guard.ThrowIfNull(exportedItems);

return builder.AddProcessor(new SimpleActivityExportProcessor(new InMemoryExporter<Activity>(exportedItems)));
return builder.AddSimpleExportProcessor(
new InMemoryExporter<Activity>(exportedItems));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public static OpenTelemetryLoggerOptions AddInMemoryExporter(

var logExporter = BuildExporter(exportedItems);

return loggerOptions.AddProcessor(
new SimpleLogRecordExportProcessor(logExporter));
((IDeferredLoggerProviderBuilder)loggerOptions).Configure(
(sp, builder) => builder.AddSimpleExportProcessor(logExporter));

return loggerOptions;
}

/// <summary>
Expand All @@ -43,8 +45,7 @@ public static LoggerProviderBuilder AddInMemoryExporter(

var logExporter = BuildExporter(exportedItems);

return loggerProviderBuilder.AddProcessor(
new SimpleLogRecordExportProcessor(logExporter));
return loggerProviderBuilder.AddSimpleExportProcessor(logExporter);
}

private static InMemoryExporter<LogRecord> BuildExporter(ICollection<LogRecord> exportedItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,19 @@ there but that may change in the future so it is handled
{
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name!);

var processor = OtlpLogExporterHelperExtensions.BuildOtlpLogExporter(
var processorOptions = builderOptions.LogRecordExportProcessorOptions
?? throw new InvalidOperationException("LogRecordExportProcessorOptions were missing with logging enabled");

OtlpLogExporterHelperExtensions.AddOtlpLogExporter(
name,
sp,
logging,
builderOptions.LoggingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance),
builderOptions.LogRecordExportProcessorOptions ?? throw new InvalidOperationException("LogRecordExportProcessorOptions were missing with logging enabled"),
builderOptions.SdkLimitOptions,
builderOptions.ExperimentalOptions,
processorOptions.ExportProcessorType,
processorPipelineWeight: DefaultProcessorPipelineWeight,
skipUseOtlpExporterRegistrationCheck: true);

processor.PipelineWeight = DefaultProcessorPipelineWeight;

logging.AddProcessor(processor);
});

services!.ConfigureOpenTelemetryMeterProvider(
Expand All @@ -232,20 +234,19 @@ there but that may change in the future so it is handled
{
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name!);

var processorOptions = builderOptions.ActivityExportProcessorOptions ?? throw new InvalidOperationException("ActivityExportProcessorOptions were missing with tracing enabled");
var processorOptions = builderOptions.ActivityExportProcessorOptions
?? throw new InvalidOperationException("ActivityExportProcessorOptions were missing with tracing enabled");

var processor = OtlpTraceExporterHelperExtensions.BuildOtlpExporterProcessor(
OtlpTraceExporterHelperExtensions.AddOtlpTraceExporter(
name,
sp,
tracing,
builderOptions.TracingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance),
builderOptions.SdkLimitOptions,
builderOptions.ExperimentalOptions,
processorOptions.ExportProcessorType,
processorOptions.BatchExportProcessorOptions,
processorPipelineWeight: DefaultProcessorPipelineWeight,
skipUseOtlpExporterRegistrationCheck: true);

processor.PipelineWeight = DefaultProcessorPipelineWeight;

tracing.AddProcessor(processor);
});

static OtlpExporterBuilderOptions GetBuilderOptionsAndValidateRegistrations(IServiceProvider sp, string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ internal OtlpExporterBuilderOptions(
this.MetricReaderOptions = metricReaderOptions;
this.ActivityExportProcessorOptions = activityExportProcessorOptions;

var defaultBatchOptions = this.ActivityExportProcessorOptions!.BatchExportProcessorOptions;
var defaultOptions = this.ActivityExportProcessorOptions!;

this.DefaultOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Default, defaultBatchOptions);
this.DefaultOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Default, defaultOptions);

this.LoggingOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Logs, defaultBatchOptions);
this.LoggingOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Logs, defaultOptions);

this.MetricsOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Metrics, defaultBatchOptions);
this.MetricsOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Metrics, defaultOptions);

this.TracingOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Traces, defaultBatchOptions);
this.TracingOptionsInstance = new OtlpExporterOptions(configuration!, OtlpExporterOptionsConfigurationType.Traces, defaultOptions);
}

public IOtlpExporterOptions DefaultOptions => this.DefaultOptionsInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ internal OtlpExporterOptions(
: this(
configuration: new ConfigurationBuilder().AddEnvironmentVariables().Build(),
configurationType,
defaultBatchOptions: new())
defaultOptions: new())
{
}

internal OtlpExporterOptions(
IConfiguration configuration,
OtlpExporterOptionsConfigurationType configurationType,
BatchExportActivityProcessorOptions defaultBatchOptions)
ActivityExportProcessorOptions defaultOptions)
{
Debug.Assert(defaultBatchOptions != null, "defaultBatchOptions was null");
Debug.Assert(defaultOptions != null, "defaultOptions was null");

this.ApplyConfiguration(configuration, configurationType);

Expand All @@ -74,7 +74,8 @@ internal OtlpExporterOptions(
};
};

this.BatchExportProcessorOptions = defaultBatchOptions!;
this.ExportProcessorType = defaultOptions!.ExportProcessorType;
this.BatchExportProcessorOptions = defaultOptions.BatchExportProcessorOptions;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -165,7 +166,7 @@ internal static OtlpExporterOptions CreateOtlpExporterOptions(
=> new(
configuration,
OtlpExporterOptionsConfigurationType.Default,
serviceProvider.GetRequiredService<IOptionsMonitor<BatchExportActivityProcessorOptions>>().Get(name));
serviceProvider.GetRequiredService<IOptionsMonitor<ActivityExportProcessorOptions>>().Get(name));

internal void ApplyConfigurationUsingSpecificationEnvVars(
IConfiguration configuration,
Expand Down
Loading