-
Notifications
You must be signed in to change notification settings - Fork 775
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
[otlp] UseOtlpExporter cross-cutting extension #5400
Merged
CodeBlanch
merged 61 commits into
open-telemetry:main
from
CodeBlanch:otlp-crosscutting-extension
Mar 14, 2024
Merged
Changes from 10 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
b060f51
OtlpExporter cross-cutting registration extension rough draft.
CodeBlanch c729fd7
Tweaks and evolving design.
CodeBlanch fbd97d9
Merge remote-tracking branch 'upstream/main' into otlp-crosscutting-e…
CodeBlanch ab87eb0
First compiling version.
CodeBlanch 0e9709e
Tweaks.
CodeBlanch a19a81f
Add concept of weight to BaseProcessor.
CodeBlanch 7e1cffb
Merge remote-tracking branch 'upstream/main' into otlp-crosscutting-e…
CodeBlanch 77e550c
Updates based on discussion.
CodeBlanch ef6652b
Tweak processor pipeline weight design.
CodeBlanch ea8d6a1
Revert OtlpExporterOptionsBase refactor to shrink the diff of changes.
CodeBlanch 5512bc7
Merge branch 'main' into otlp-crosscutting-extension
CodeBlanch b3d9db4
Reduce changes shown on diff.
CodeBlanch 75111d2
Tweaks.
CodeBlanch c2db100
Code review.
CodeBlanch cb83c0e
Merge remote-tracking branch 'upstream/main' into otlp-crosscutting-e…
CodeBlanch 1506a34
Merge remote-tracking branch 'upstream/main' into otlp-crosscutting-e…
CodeBlanch 8c896a3
Merge from main.
CodeBlanch d55e488
Merge from main.
CodeBlanch 2a1046a
Merge from main.
CodeBlanch 15e0272
Merge from main.
CodeBlanch 24cbc81
Merge fixes.
CodeBlanch 2e7811d
Tweaks and fixes.
CodeBlanch 7bdf936
Merge from main.
CodeBlanch 71c56e6
Revert order changes.
CodeBlanch aa84a40
Tweak.
CodeBlanch 4ba37bb
Tweak.
CodeBlanch 7f3c3cb
API files.
CodeBlanch ded0687
Bug fix.
CodeBlanch bc27254
Make processor pipeline weight a constant.
CodeBlanch 57cbc99
Tweak.
CodeBlanch 356628e
Bug fix.
CodeBlanch 3493219
Add tests.
CodeBlanch 15ac47b
CHANGELOG update.
CodeBlanch f275ea2
Merge branch 'main' into otlp-crosscutting-extension
CodeBlanch c80c86f
Lint.
CodeBlanch 74f24fe
Warning cleanup.
CodeBlanch ef38a9d
Warning cleanup.
CodeBlanch e767069
Docfx fix.
CodeBlanch f515cc0
Integration test fixes.
CodeBlanch b145007
Code review.
CodeBlanch 16e0741
JSON config comment update.
CodeBlanch 6f2c8e0
Code review.
CodeBlanch b75f139
Code review.
CodeBlanch 0faac1a
CHANGELOG tweak.
CodeBlanch 4bef0ef
Ctor tweak.
CodeBlanch 956f537
Test tweaks.
CodeBlanch 2c1cc37
Tweaked exposed overloads and refactored code.
CodeBlanch e577dc3
Test fixes.
CodeBlanch c4fe57c
Assert fix.
CodeBlanch 084188a
Add XML docs to capture current naming behaviors.
CodeBlanch c0d8ba3
Tweak namespace.
CodeBlanch cc33b5a
Merge branch 'main' into otlp-crosscutting-extension
CodeBlanch c11e4a2
Warning cleanup.
CodeBlanch 4f06ab8
Code review.
CodeBlanch cd7ca4e
Remove UseOtlpExporter overload which only changes protocol.
CodeBlanch 01dba40
Code review.
CodeBlanch 5dac98b
Merge from main.
CodeBlanch 1c3258c
Cleanup and refactoring.
CodeBlanch ed09c06
Code review.
CodeBlanch 31f7759
Refactor spec env var code and add test coverage for UseOtlpExporter …
CodeBlanch c3e0e37
Add a test collection for tests which modify envvars.
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
220 changes: 220 additions & 0 deletions
220
...etry.Exporter.OpenTelemetryProtocol/Builder/OpenTelemetryBuilderOtlpExporterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#nullable enable | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; | ||
using OpenTelemetry.Internal; | ||
using OpenTelemetry.Logs; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Exporter; | ||
|
||
/// <summary> | ||
/// Contains extension methods to facilitate registration of the OpenTelemetry | ||
/// Protocol (OTLP) exporter into an <see cref="IOpenTelemetryBuilder"/> | ||
/// instance. | ||
/// </summary> | ||
public static class OpenTelemetryBuilderOtlpExporterExtensions | ||
{ | ||
/// <summary> | ||
/// Uses OpenTelemetry Protocol (OTLP) exporter for all signals. | ||
/// </summary> | ||
/// <remarks> | ||
/// Notes: | ||
/// <list type="bullet"> | ||
/// <item>Calling this method automatically enables logging, metrics, and | ||
/// tracing.</item> | ||
/// <item>The exporter registered by this method will be added as the last | ||
/// processor in the pipeline established for logging and tracing.</item> | ||
/// <item>This method can only be called once. Subsequent calls will results | ||
/// in a <see cref="NotSupportedException"/> being thrown.</item> | ||
/// <item>This method cannot be called in addition to signal-specific | ||
/// <c>AddOtlpExporter</c> methods. If this method is called signal-specific | ||
/// <c>AddOtlpExporter</c> calls will result in a <see | ||
/// cref="NotSupportedException"/> being thrown.</item> | ||
/// </list> | ||
/// </remarks> | ||
/// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> | ||
/// <returns>Supplied <see cref="IOpenTelemetryBuilder"/> for chaining calls.</returns> | ||
public static IOpenTelemetryBuilder UseOtlpExporter( | ||
this IOpenTelemetryBuilder builder) | ||
=> UseOtlpExporter(builder, name: null); | ||
|
||
/// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> | ||
/// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> | ||
/// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> | ||
/// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> | ||
/// <param name="baseEndpoint">The base endpoint to use. A signal-specific | ||
/// path will be appended to the base endpoint for each signal | ||
/// automatically.</param> | ||
public static IOpenTelemetryBuilder UseOtlpExporter( | ||
this IOpenTelemetryBuilder builder, | ||
Uri baseEndpoint) | ||
=> UseOtlpExporter(builder, OtlpExportProtocol.Grpc, baseEndpoint); | ||
|
||
/// <summary><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)"/></summary> | ||
/// <remarks><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/remarks"/></remarks> | ||
/// <returns><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder)" path="/returns"/></returns> | ||
/// <param name="builder"><see cref="IOpenTelemetryBuilder"/>.</param> | ||
/// <param name="protocol"><see cref="OtlpExportProtocol"/>.</param> | ||
/// <param name="baseEndpoint"><inheritdoc cref="UseOtlpExporter(IOpenTelemetryBuilder, Uri)" path="/param[@name='baseEndpoint']"/></param> | ||
public static IOpenTelemetryBuilder UseOtlpExporter( | ||
this IOpenTelemetryBuilder builder, | ||
OtlpExportProtocol protocol, | ||
Uri baseEndpoint) | ||
{ | ||
Guard.ThrowIfNull(baseEndpoint); | ||
|
||
return UseOtlpExporter(builder, name: null, configure: otlpBuilder => | ||
{ | ||
otlpBuilder.ConfigureDefaultExporterOptions(o => | ||
{ | ||
o.Protocol = protocol; | ||
if (baseEndpoint != null) | ||
CodeBlanch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
o.Endpoint = baseEndpoint; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
internal static IOpenTelemetryBuilder AddOtlpExporter( | ||
this IOpenTelemetryBuilder builder, | ||
Action<OtlpExporterBuilder> configure) | ||
{ | ||
Guard.ThrowIfNull(configure); | ||
|
||
return UseOtlpExporter(builder, name: null, configure: configure); | ||
} | ||
|
||
internal static IOpenTelemetryBuilder UseOtlpExporter( | ||
this IOpenTelemetryBuilder builder, | ||
IConfiguration configuration) | ||
{ | ||
Guard.ThrowIfNull(configuration); | ||
|
||
return UseOtlpExporter(builder, name: null, configuration: configuration); | ||
} | ||
|
||
internal static IOpenTelemetryBuilder UseOtlpExporter( | ||
this IOpenTelemetryBuilder builder, | ||
string? name = null, | ||
IConfiguration? configuration = null, | ||
Action<OtlpExporterBuilder>? configure = null, | ||
ProcessorPipelineWeight processorPipelineWeight = ProcessorPipelineWeight.PipelineExporter) | ||
{ | ||
Guard.ThrowIfNull(builder); | ||
|
||
if (configuration == null && string.IsNullOrEmpty(name)) | ||
{ | ||
name = "otlp"; | ||
} | ||
|
||
var otlpBuilder = new OtlpExporterBuilder(builder.Services, name, configuration); | ||
|
||
configure?.Invoke(otlpBuilder); | ||
|
||
UseOtlpExporterInternal(builder, name, processorPipelineWeight); | ||
|
||
return builder; | ||
} | ||
|
||
private static void UseOtlpExporterInternal(IOpenTelemetryBuilder builder, string? name, ProcessorPipelineWeight processorPipelineWeight) | ||
{ | ||
builder | ||
.WithLogging() | ||
.WithMetrics() | ||
.WithTracing(); | ||
|
||
var services = builder.Services; | ||
|
||
services.AddSingleton<UseOtlpExporterRegistration>(); | ||
|
||
services.RegisterOptionsFactory(configuration => new SdkLimitOptions(configuration)); | ||
services.RegisterOptionsFactory(configuration => new ExperimentalOptions(configuration)); | ||
services.RegisterOptionsFactory((sp, configuration, name) => new OtlpExporterBuilderOptions( | ||
configuration, | ||
sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue, | ||
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().CurrentValue, | ||
sp.GetService<IOptionsMonitor<LogRecordExportProcessorOptions>>()?.Get(name), | ||
sp.GetService<IOptionsMonitor<MetricReaderOptions>>()?.Get(name), | ||
sp.GetService<IOptionsMonitor<ActivityExportProcessorOptions>>()?.Get(name))); | ||
|
||
name ??= Options.DefaultName; | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
services.ConfigureOpenTelemetryLoggerProvider( | ||
(sp, logging) => | ||
{ | ||
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name); | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!builderOptions.Signals.HasFlag(OtlpExporterSignals.Logs)) | ||
{ | ||
return; | ||
} | ||
|
||
var processor = OtlpLogExporterHelperExtensions.BuildOtlpLogExporter( | ||
sp, | ||
builderOptions.LoggingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance), | ||
builderOptions.LogRecordExportProcessorOptions ?? throw new NotSupportedException(), | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builderOptions.SdkLimitOptions, | ||
builderOptions.ExperimentalOptions); | ||
|
||
processor.PipelineWeight = processorPipelineWeight; | ||
|
||
logging.AddProcessor(processor); | ||
}); | ||
|
||
services.ConfigureOpenTelemetryMeterProvider( | ||
(sp, metrics) => | ||
{ | ||
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name); | ||
|
||
if (!builderOptions.Signals.HasFlag(OtlpExporterSignals.Metrics)) | ||
{ | ||
return; | ||
} | ||
|
||
metrics.AddReader( | ||
OtlpMetricExporterExtensions.BuildOtlpExporterMetricReader( | ||
builderOptions.MetricsOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance), | ||
builderOptions.MetricReaderOptions ?? throw new NotSupportedException(), | ||
sp)); | ||
}); | ||
|
||
services.ConfigureOpenTelemetryTracerProvider( | ||
(sp, tracing) => | ||
{ | ||
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name); | ||
|
||
if (!builderOptions.Signals.HasFlag(OtlpExporterSignals.Traces)) | ||
{ | ||
return; | ||
} | ||
|
||
var processorOptions = builderOptions.ActivityExportProcessorOptions ?? throw new NotSupportedException(); | ||
|
||
var processor = OtlpTraceExporterHelperExtensions.BuildOtlpExporterProcessor( | ||
builderOptions.TracingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance), | ||
builderOptions.SdkLimitOptions, | ||
processorOptions.ExportProcessorType, | ||
processorOptions.BatchExportProcessorOptions, | ||
sp); | ||
|
||
processor.PipelineWeight = processorPipelineWeight; | ||
|
||
tracing.AddProcessor(processor); | ||
}); | ||
|
||
static OtlpExporterBuilderOptions GetBuilderOptionsAndValidateRegistrations(IServiceProvider sp, string name) | ||
{ | ||
sp.EnsureSingleUseOtlpExporterRegistration(); | ||
|
||
return sp.GetRequiredService<IOptionsMonitor<OtlpExporterBuilderOptions>>().Get(name); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...y.Exporter.OpenTelemetryProtocol/Builder/OpenTelemetryBuilderServiceProviderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Exporter; | ||
|
||
namespace System; | ||
CodeBlanch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
internal static class OpenTelemetryBuilderServiceProviderExtensions | ||
{ | ||
public static void EnsureSingleUseOtlpExporterRegistration(this IServiceProvider serviceProvider) | ||
{ | ||
var registrations = serviceProvider.GetServices<UseOtlpExporterRegistration>(); | ||
if (registrations.Count() > 1) | ||
{ | ||
throw new NotSupportedException("Multiple calls to UseOtlpExporter on the same IServiceCollection are not supported."); | ||
} | ||
} | ||
|
||
public static void EnsureNoUseOtlpExporterRegistrations(this IServiceProvider serviceProvider) | ||
{ | ||
var registrations = serviceProvider.GetServices<UseOtlpExporterRegistration>(); | ||
if (registrations.Any()) | ||
{ | ||
throw new NotSupportedException("Signal-specific AddOtlpExporter methods and the cross-cutting UseOtlpExporter method being invoked on the same IServiceCollection is not supported."); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like grpc protocol specific method to me. Do we need this as a separate method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not following. What do you mean by grpc-specific? At the moment there are 3 overloads exposed:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for this particular case
Are we expecting many users specifying the endpoint here and changing the protocol to http via environment variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vishweshbankwar
I chatted with @alanwest a bit. We decided to tweak the overloads. They are now:
How you feel about that? TBH we weren't sure exactly what concern you were raising but these seemed more useful in the end 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chatted offline with @vishweshbankwar. We decided to drop the second overload. What we have now is: