Skip to content

OpenTelemetry

https://opentelemetry.io/

OpenTelemetry is a collection of tools, APIs, and SDKs. Use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help you analyze your software’s performance and behavior.

Integration

You can find it here about how to use OpenTelemetry in console applications or ASP.NET Core, at here we mainly describe how to tracing CAP data to OpenTelemetry.

Configuration

Install the CAP OpenTelemetry package into the project.

dotnet add package DotNetCore.Cap.OpenTelemetry

The OpenTelemetry data comes from diagnostics, add the instrumentation of CAP to the configuration of OpenTelemetry.

services.AddOpenTelemetryTracing((builder) => builder
    .AddAspNetCoreInstrumentation()
    .AddCapInstrumentation()    // <-- Add this line
    .AddZipkinExporter()
);

If you don't use a framework that does this automatically for you (like aspnetcore), make sure you enable a listener, for example:

ActivitySource.AddActivityListener(new ActivityListener()
{
    ShouldListenTo = _ => true,
    Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
    ActivityStarted = activity => Console.WriteLine($"{activity.ParentId}:{activity.Id} - Start"),
    ActivityStopped = activity => Console.WriteLine($"{activity.ParentId}:{activity.Id} - Stop")
});
Here is a diagram of CAP's tracking data in Zipkin:

Context Propagation

CAP supports Context Propagation by injecting traceparent and baggage headers when sending messages and restoring the context from those headers when receiving messages.

CAP uses the configured Propagators.DefaultTextMapPropagator propagator, which is usually set to both TraceContextPropagator and BaggagePropagator by the dotnet OpenTelemetry SDK but can be set in your your client program. For example, to opt out of the Baggage propagation, you can call:

OpenTelemetry.Sdk.SetDefaultTextMapPropagator(
    new TraceContextPropagator());

See the dotnet OpenTelemetry.Api readme for more details.