使用 C# .Net Core 连接到远程 Jaegertracing

Jon*_*ahl 2 c# .net-core opentracing jaeger

当涉及到 C# 时,我在 Opentracing 和 Jaegertracing 方面遇到了一些问题。我以前有过这个工作,但是在 Java 项目中。所以我开始怀疑在 C# .NET Core Web 服务方面我缺少什么。

这是我的课程开始使用我的示踪剂

    public static class MyTracer
    {
        public static ITracer tracer = null;

        public static ITracer InitTracer()
        {
            Environment.SetEnvironmentVariable("JAEGER_SERVICE_NAME", "my-store");
            Environment.SetEnvironmentVariable("JAEGER_AGENT_HOST", "192.168.2.27");
            Environment.SetEnvironmentVariable("JAEGER_AGENT_PORT", "6831");
            Environment.SetEnvironmentVariable("JAEGER_SAMPLER_TYPE", "const");                     
            Environment.SetEnvironmentVariable("JAEGER_REPORTER_LOG_SPANS", "false");
            Environment.SetEnvironmentVariable("JAEGER_SAMPLER_PARAM","1");
            Environment.SetEnvironmentVariable("JAEGER_SAMPLER_MANAGER_HOST_PORT", "5778");
            Environment.SetEnvironmentVariable("JAEGER_REPORTER_FLUSH_INTERVAL" , "1000");
            Environment.SetEnvironmentVariable("JAEGER_REPORTER_MAX_QUEUE_SIZE" , "100");


            var loggerFactory = new LoggerFactory();

            var config = Configuration.FromEnv(loggerFactory);

            tracer = config.GetTracer();

            if (!GlobalTracer.IsRegistered())
            {
                GlobalTracer.Register(tracer);
            }
            return tracer;
        }
    }
Run Code Online (Sandbox Code Playgroud)

应向 Jaeger 代理和收集器报告以在 UI 中显示的控制器代码。

[Route("api/[controller]")]
[ApiController]
public class ComponentController : ControllerBase
{
    private readonly ITracer tracer;

    public ComponentController(ITracer tracer)
    {
        this.tracer = tracer;
    }

    /// <summary>
    /// Get component by ID
    /// </summary>
    /// <returns></returns>
    [HttpGet("GetComponent")]
    public ActionResult<ComponentModel> GetComponent(string id)
    {   
        var builder = tracer.BuildSpan("operationName");            
        var span = builder.Start();
        // Set some context data
        span.Log("Getting data");
        span.SetTag(Tags.SpanKind, "Getting data request");
        span.Finish();                     

        ComponentModel component = ComponentManager.GetComponent(id);    

        return component;
    }


}
Run Code Online (Sandbox Code Playgroud)

启动文件

    public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        // Use "OpenTracing.Contrib.NetCore" to automatically generate spans for ASP.NET Core, Entity Framework Core, ...
        // See https://github.com/opentracing-contrib/csharp-netcore for details.
        services.AddOpenTracing();

        //Init tracer
        services.AddSingleton<ITracer>(t => MyTracer.InitTracer());

        services.AddHealthChecks();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

但这根本行不通。我在这里缺少什么才能让它与远程服务器一起工作?

Jon*_*ahl 10

iv终于找到了解决办法。这似乎与记者的启动方式有关。无论如何,我将跟踪器类更改为这个。

    public static class MyTracer
{
    public static ITracer tracer = null;
    public static ITracer InitTracer(IServiceProvider serviceProvider)
    {
        string serviceName = serviceProvider.GetRequiredService<IHostingEnvironment>().ApplicationName;

        Environment.SetEnvironmentVariable("JAEGER_SERVICE_NAME", "my-store");
        //Environment.SetEnvironmentVariable("JAEGER_AGENT_HOST", "192.168.2.27");
        //Environment.SetEnvironmentVariable("JAEGER_AGENT_PORT", "6831");
        //Environment.SetEnvironmentVariable("JAEGER_SAMPLER_TYPE", "const");                     
        //Environment.SetEnvironmentVariable("JAEGER_REPORTER_LOG_SPANS", "false");
        //Environment.SetEnvironmentVariable("JAEGER_SAMPLER_PARAM","1");
        //Environment.SetEnvironmentVariable("JAEGER_SAMPLER_MANAGER_HOST_PORT", "5778");
        //Environment.SetEnvironmentVariable("JAEGER_REPORTER_FLUSH_INTERVAL" , "1000");
        //Environment.SetEnvironmentVariable("JAEGER_REPORTER_MAX_QUEUE_SIZE" , "100");
        //application - server - id = server - x

        var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();

        var sampler = new ConstSampler(sample: true);
        var reporter = new RemoteReporter.Builder()
         .WithLoggerFactory(loggerFactory)
         .WithSender(new UdpSender("192.168.2.27", 6831, 0))
         .Build();

        tracer = new Tracer.Builder(serviceName)
         .WithLoggerFactory(loggerFactory)
         .WithSampler(sampler)
         .WithReporter(reporter)
         .Build();

        if (!GlobalTracer.IsRegistered())
        {
            GlobalTracer.Register(tracer);
        }
        return tracer;
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道现在这里有几个非活动变量。将看看它们是否仍然有用。但是现在不需要任何东西来让它滚动。希望这可以帮助其他人尝试让 .NET Core 与远程 Jeagertracing 服务器一起正常工作。