Tracing

This page details how to perform distributed tracing with Ocelot.

opentracing-csharp Logo OpenTracing

Ocelot providers tracing functionality from the excellent OpenTracing API for .NET project. The code for the Ocelot integration can be found in Ocelot.Tracing.OpenTracing project.

The example below uses C# Client for Jaeger client to provide the tracer used in Ocelot. In order to add OpenTracing services we must call the AddOpenTracing() extension of the OcelotBuilder being returned by AddOcelot() [1] like below:

services.AddSingleton<ITracer>(sp =>
{
    var loggerFactory = sp.GetService<ILoggerFactory>();
    Configuration config = new Configuration(context.HostingEnvironment.ApplicationName, loggerFactory);

    var tracer = config.GetTracer();
    GlobalTracer.Register(tracer);
    return tracer;
});

services
    .AddOcelot()
    .AddOpenTracing();

Then in your ocelot.json add the following to the Route you want to trace:

"HttpHandlerOptions": {
  "UseTracing": true
}

Ocelot will now send tracing information to Jaeger when this Route is called.

OpenTracing Status

The OpenTracing project was archived on January 31, 2022 (see the article). The Ocelot team will decide on a migration to OpenTelemetry which is highly desired.

Butterfly

Ocelot providers tracing functionality from the excellent Butterfly project. The code for the Ocelot integration can be found in Ocelot.Tracing.Butterfly project.

In order to use the tracing please read the Butterfly documentation.

In Ocelot you need to add the NuGet package if you wish to trace a Route:

Install-Package Ocelot.Tracing.Butterfly

In your ConfigureServices method to add Butterfly services: we must call the AddButterfly() extension of the OcelotBuilder being returned by AddOcelot() [1] like below:

services
    .AddOcelot()
    // This comes from Ocelot.Tracing.Butterfly package
    .AddButterfly(option =>
    {
        // This is the URL that the Butterfly collector server is running on...
        option.CollectorUrl = "http://localhost:9618";
        option.Service = "Ocelot";
    });

Then in your ocelot.json add the following to the Route you want to trace:

"HttpHandlerOptions": {
  "UseTracing": true
}

Ocelot will now send tracing information to Butterfly when this Route is called.