Skip to content

Commit

Permalink
Added ServiceProvider option to WithTracing and WithMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
megla-tlanghorst committed Feb 24, 2023
1 parent 3ca478b commit 4652db2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ OpenTelemetry.OpenTelemetryBuilder.ConfigureResource(System.Action<OpenTelemetry
OpenTelemetry.OpenTelemetryBuilder.Services.get -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
OpenTelemetry.OpenTelemetryBuilder.WithMetrics() -> OpenTelemetry.OpenTelemetryBuilder!
OpenTelemetry.OpenTelemetryBuilder.WithMetrics(System.Action<OpenTelemetry.Metrics.MeterProviderBuilder!>! configure) -> OpenTelemetry.OpenTelemetryBuilder!
OpenTelemetry.OpenTelemetryBuilder.WithMetrics(System.Action<OpenTelemetry.Metrics.MeterProviderBuilder!, System.IServiceProvider!>! configure) -> OpenTelemetry.OpenTelemetryBuilder!
OpenTelemetry.OpenTelemetryBuilder.WithTracing() -> OpenTelemetry.OpenTelemetryBuilder!
OpenTelemetry.OpenTelemetryBuilder.WithTracing(System.Action<OpenTelemetry.Trace.TracerProviderBuilder!>! configure) -> OpenTelemetry.OpenTelemetryBuilder!
OpenTelemetry.OpenTelemetryBuilder.WithTracing(System.Action<OpenTelemetry.Trace.TracerProviderBuilder!, System.IServiceProvider!>! configure) -> OpenTelemetry.OpenTelemetryBuilder!
static Microsoft.Extensions.DependencyInjection.OpenTelemetryServicesExtensions.AddOpenTelemetry(this Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> OpenTelemetry.OpenTelemetryBuilder!

42 changes: 40 additions & 2 deletions src/OpenTelemetry.Extensions.Hosting/OpenTelemetryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,26 @@ public OpenTelemetryBuilder WithMetrics(Action<MeterProviderBuilder> configure)

var builder = new MeterProviderBuilderBase(this.Services);

configure(builder);
builder.Configure(configure);

return this;
}

/// <summary>
/// Adds metric services into the builder.
/// </summary>
/// <remarks><inheritdoc cref="WithMetrics()" path="/remarks"/></remarks>
/// <param name="configure"><see cref="MeterProviderBuilder"/>
/// configuration callback.</param>
/// <returns>The supplied <see cref="OpenTelemetryBuilder"/> for chaining
/// calls.</returns>
public OpenTelemetryBuilder WithMetrics(Action<MeterProviderBuilder, IServiceProvider> configure)
{
Guard.ThrowIfNull(configure);

var builder = new MeterProviderBuilderBase(this.Services);

builder.Configure(configure);

return this;
}
Expand Down Expand Up @@ -150,7 +169,26 @@ public OpenTelemetryBuilder WithTracing(Action<TracerProviderBuilder> configure)

var builder = new TracerProviderBuilderBase(this.Services);

configure(builder);
builder.Configure(configure);

return this;
}

/// <summary>
/// Adds tracing services into the builder.
/// </summary>
/// <remarks><inheritdoc cref="WithTracing()" path="/remarks"/></remarks>
/// <param name="configure"><see cref="TracerProviderBuilder"/>
/// configuration callback.</param>
/// <returns>The supplied <see cref="OpenTelemetryBuilder"/> for chaining
/// calls.</returns>
public OpenTelemetryBuilder WithTracing(Action<TracerProviderBuilder, IServiceProvider> configure)
{
Guard.ThrowIfNull(configure);

var builder = new TracerProviderBuilderBase(this.Services);

builder.Configure(configure);

return this;
}
Expand Down
10 changes: 10 additions & 0 deletions src/OpenTelemetry/Metrics/Builder/MeterProviderBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ MeterProviderBuilder IDeferredMeterProviderBuilder.Configure(Action<IServiceProv
internal MeterProvider InvokeBuild()
=> this.Build();

internal void Configure(Action<MeterProviderBuilder> configure)
{
this.ConfigureBuilderInternal((_, builder) => configure(builder));
}

internal void Configure(Action<MeterProviderBuilder, IServiceProvider> configure)
{
this.ConfigureBuilderInternal((sp, builder) => configure(builder, sp));
}

/// <summary>
/// Run the configured actions to initialize the <see cref="MeterProvider"/>.
/// </summary>
Expand Down
22 changes: 18 additions & 4 deletions src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public TracerProviderBuilderBase()
.AddOpenTelemetrySharedProviderBuilderServices()
.AddOpenTelemetryTracerProviderBuilderServices()
.TryAddSingleton<TracerProvider>(
sp => throw new NotSupportedException("Self-contained TracerProvider cannot be accessed using the application IServiceProvider call Build instead."));
sp => throw new NotSupportedException(
"Self-contained TracerProvider cannot be accessed using the application IServiceProvider call Build instead."));

services.ConfigureOpenTelemetryTracerProvider((sp, builder) => this.services = null);

Expand Down Expand Up @@ -69,7 +70,8 @@ internal TracerProviderBuilderBase(IServiceCollection services)
TracerProvider? ITracerProviderBuilder.Provider => null;

/// <inheritdoc />
public override TracerProviderBuilder AddInstrumentation<TInstrumentation>(Func<TInstrumentation> instrumentationFactory)
public override TracerProviderBuilder AddInstrumentation<TInstrumentation>(
Func<TInstrumentation> instrumentationFactory)
{
Guard.ThrowIfNull(instrumentationFactory);

Expand Down Expand Up @@ -112,12 +114,23 @@ TracerProviderBuilder ITracerProviderBuilder.ConfigureServices(Action<IServiceCo
=> this.ConfigureServicesInternal(configure);

/// <inheritdoc />
TracerProviderBuilder IDeferredTracerProviderBuilder.Configure(Action<IServiceProvider, TracerProviderBuilder> configure)
TracerProviderBuilder IDeferredTracerProviderBuilder.Configure(
Action<IServiceProvider, TracerProviderBuilder> configure)
=> this.ConfigureBuilderInternal(configure);

internal TracerProvider InvokeBuild()
=> this.Build();

internal void Configure(Action<TracerProviderBuilder> configure)
{
this.ConfigureBuilderInternal((_, builder) => configure(builder));
}

internal void Configure(Action<TracerProviderBuilder, IServiceProvider> configure)
{
this.ConfigureBuilderInternal((sp, builder) => configure(builder, sp));
}

/// <summary>
/// Adds instrumentation to the provider.
/// </summary>d
Expand Down Expand Up @@ -154,7 +167,8 @@ protected TracerProvider Build()
{
if (!this.allowBuild)
{
throw new NotSupportedException("A TracerProviderBuilder bound to external service cannot be built directly. Access the TracerProvider using the application IServiceProvider instead.");
throw new NotSupportedException(
"A TracerProviderBuilder bound to external service cannot be built directly. Access the TracerProvider using the application IServiceProvider instead.");
}

var services = this.services;
Expand Down

0 comments on commit 4652db2

Please sign in to comment.