dotnet add package OpenTelemetry.Extensions.HostingThe following example registers tracing using the ZipkinExporter and binds
options to the "Zipkin" configuration section:
services.AddOpenTelemetryTracing((builder) => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddZipkinExporter());
services.Configure<ZipkinExporterOptions>(this.Configuration.GetSection("Zipkin"));The following example registers a processor of the type "MyProcessor" which has
been registered as a singleton with the IServiceCollection:
services.AddSingleton<MyProcessor>();
services.AddOpenTelemetryTracing((builder) => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessor<MyProcessor>());Similar methods exist for registering instrumentation (AddInstrumentation<T>)
and setting a sampler (SetSampler<T>).
You can also access the application IServiceProvider directly and accomplish
the same registration using the Configure extension like this:
services.AddSingleton<MyProcessor>();
services.AddOpenTelemetryTracing(hostingBuilder => hostingBuilder
.Configure((sp, builder) => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessor(sp.GetRequiredService<MyProcessor>())));Note: Configure is called after the IServiceProvider has been built
from the application IServiceCollection so any services registered in the
Configure callback will be ignored.
Library authors may want to configure the OpenTelemetry TracerProvider and
register application services to provide more complex features. This can be
accomplished concisely by using the TracerProviderBuilder.GetServices
extension method inside of a more general TracerProviderBuilder configuration
extension like this:
public static class MyLibraryExtensions
{
public static TracerProviderBuilder AddMyFeature(this TracerProviderBuilder tracerProviderBuilder)
{
(tracerProviderBuilder.GetServices()
?? throw new NotSupportedException(
"MyFeature requires a hosting TracerProviderBuilder instance."))
.AddHostedService<MyHostedService>()
.AddSingleton<MyService>()
.AddSingleton<MyProcessor>()
.AddSingleton<MySampler>();
return tracerProviderBuilder
.AddProcessor<MyProcessor>()
.SetSampler<MySampler>();
}
}Such an extension method can be consumed like this:
services.AddOpenTelemetryTracing((builder) => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddMyFeature()
.AddZipkinExporter());