Search code examples
c#opentracing

Opentracing c# without ASP?


Im a bit puzzled here. But shouldn't you be able to start up a Opentracing client based on c# without involving ASP? Alot of the examples always do involve ASP in some form. Can this be solved without ASP?


Solution

  • ASP.NET Core isn't needed. The reason why almost all examples use ASP.NET Core, it's because that's what OpenTelemetry (and its predecessors, OpenTracing and OpenCensus) are for - to provide distributed tracing, logging and metrics for (micro)services. In the vast majority of cases those services use HTTP and gRPC, even if it's only to expose a control endpoint.

    In OpenTelemetry, applications/services publish logs, traces and metrics. Those can be published through exporters to Jaeger, Zipkin, Elastic, Prometheus etc, or, preferably, collected by a Collector instance which forwards them, again through exporters, to the monitoring services.

    The OpenTelemetry .NET repo explains how OpenTelemetry is used in .NET Core through the native Activity and ActivitySource classes and listeners. It contains a folder with Console examples.

    Logging

    The Logging Getting Started page doesn't mention ASP.NET Core at all, it shows a Console appliction. The code is just :

    using Microsoft.Extensions.Logging;
    using OpenTelemetry.Logs;
    
    public class Program
    {
        public static void Main()
        {
            using var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddOpenTelemetry(options => options
                    .AddConsoleExporter());
            });
    
            var logger = loggerFactory.CreateLogger<Program>();
            logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
        }
    }
    

    In .NET 6 this would come down to :

    using Microsoft.Extensions.Logging;
    using OpenTelemetry.Logs;
    
    using var loggerFactory = LoggerFactory.Create(builder =>
    {
    builder.AddOpenTelemetry(options => options
                    .AddConsoleExporter());
    });
    
    var logger = loggerFactory.CreateLogger<Program>();
    logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
    

    For logging, OpenTelemetry works through the .NET Core logging interfaces. If you use ILogger, you can already use OpenTelemetry.

    Tracing

    The Tracing Getting Started page also uses a Console app and uses the built-in ActivitySource and Activity classes.

    • ActivitySource corresponds to an OpenTelemetry Tracer
    • Activity corresponds to Span

    The example is a very short Console app:

    using System.Diagnostics;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    
    public class Program
    {
        private static readonly ActivitySource MyActivitySource = new ActivitySource(
            "MyCompany.MyProduct.MyLibrary");
    
        public static void Main()
        {
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                .SetSampler(new AlwaysOnSampler())
                .AddSource("MyCompany.MyProduct.MyLibrary")
                .AddConsoleExporter()
                .Build();
    
            using (var activity = MyActivitySource.StartActivity("SayHello"))
            {
                activity?.SetTag("foo", 1);
                activity?.SetTag("bar", "Hello, World!");
                activity?.SetTag("baz", new int[] { 1, 2, 3 });
            }
        }
    }
    

    Once again, the only thing that's specific to OpenTelemetry is the listener and exporters.