一键导入
dotnet-otel-instrumentation
// Pattern for adding OpenTelemetry tracing to .NET projects with clean library/host separation
// Pattern for adding OpenTelemetry tracing to .NET projects with clean library/host separation
| name | dotnet-otel-instrumentation |
| description | Pattern for adding OpenTelemetry tracing to .NET projects with clean library/host separation |
| domain | observability |
| confidence | low |
| source | earned |
When instrumenting .NET applications with OpenTelemetry, the standard pattern separates instrumentation (library code) from collection/export (host code). This keeps libraries lightweight and lets the host decide where telemetry goes.
Library projects use System.Diagnostics.ActivitySource and Activity for instrumentation. No OpenTelemetry NuGet packages in libraries. ActivitySource is declared as internal static readonly on the class that owns the operations:
public class MyService
{
internal static readonly ActivitySource ActivitySource = new("MyApp.MySubsystem");
public void DoWork()
{
using var activity = ActivitySource.StartActivity("mysubsystem.do_work");
activity?.SetTag("work.param", value);
// actual work
}
}
Only the composition root (CLI, web host, AppHost) references OpenTelemetry packages and registers sources by name:
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyApp"))
.AddSource("MyApp.MySubsystem")
.AddOtlpExporter()
.Build();
Use {subsystem}.{attribute} naming for span tags: midi.channel, note.name, sequence.title.
ActivitySource.StartActivity() returns null when no listener is registered. Always use activity?.SetTag() — never assume the activity exists.
Dispose TracerProvider before exit to flush pending spans. In CLI apps, do this explicitly. In hosted apps, use OpenTelemetry.Extensions.Hosting.
OpenTelemetry.* packages. Use System.Diagnostics only.AddSource() in the host.TracerProvider isn't disposed, the last batch of spans may be lost..SetTag() without null-conditional. Activity is null when no collector is listening.