Search code examples
c#jaeger

How can I send traces to Jaeger from C#?


I'm trying to use the Jaeger package to send traces to Jaeger from a C# app.

There are no minimal examples in the jaeger-client-csharp documentation, but from what I read, I think this should work.

using Jaeger;
using Jaeger.Samplers;

namespace jaegertest
{
    class Program
    {
        static void Main(string[] args)
        {
            var tracer = new Tracer.Builder("my-service")
                .WithSampler(new ConstSampler(true))
                .Build();

            using (var scope = tracer.BuildSpan("foo").StartActive(true))
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

I have jaeger-all-in-one.exe running but when I run this code there's no sign of any new traces. I've tried manually configuring samplers, senders, reporters, etc. but nothing I tried worked. What do I need to add to get my traces to appear in Jaeger?


Solution

  • This is the simplest working example that I was able to find.

    using Jaeger;
    using Jaeger.Reporters;
    using Jaeger.Samplers;
    using Jaeger.Senders.Thrift;
    
    namespace jaegertest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var tracer = new Tracer.Builder("my-service")
                    .WithSampler(new ConstSampler(true))
                    .WithReporter(new RemoteReporter.Builder()
                        .WithSender(new UdpSender())
                        .Build())
                    .Build();
    
                using (var scope = tracer.BuildSpan("foo").StartActive(true))
                {
                    System.Threading.Thread.Sleep(1000);
                }
    
                tracer.Dispose();
            }
        }
    }
    

    Here is a more realistic example that builds the tracer from a configuration.

    using Jaeger;
    using Jaeger.Samplers;
    using Jaeger.Senders;
    using Jaeger.Senders.Thrift;
    using Microsoft.Extensions.Logging;
    
    namespace jaegertest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var loggerFactory = new LoggerFactory();
    
                var samplerConfiguration = new Configuration.SamplerConfiguration(loggerFactory)
                    .WithType(ConstSampler.Type)
                    .WithParam(1);
    
                var senderResolver = new SenderResolver(loggerFactory)
                    .RegisterSenderFactory<ThriftSenderFactory>();
    
                var senderConfiguration = new Configuration.SenderConfiguration(loggerFactory)
                    .WithSenderResolver(senderResolver);
    
                var reporterConfiguration = new Configuration.ReporterConfiguration(loggerFactory)
                    .WithSender(senderConfiguration)
                    .WithLogSpans(true);
    
                var tracer = (Tracer)new Configuration("my-service", loggerFactory)
                    .WithSampler(samplerConfiguration)
                    .WithReporter(reporterConfiguration)
                    .GetTracer();
    
                using (var scope = tracer.BuildSpan("foo").StartActive(true))
                {
                    System.Threading.Thread.Sleep(1000);
                }
    
                tracer.Dispose();
            }
        }
    }