当谈到C#时,我在Opentraceing和Jaegertracing方面遇到了一些问题。我以前也做过这种工作,但在Java项目中。所以我开始怀疑,当谈到C#核心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;
}
}
应该向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;
}
}
启动.cs
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();
});
}
}
但这根本不起作用。让它与远程服务器一起工作,我缺少什么?
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;
}
}
我知道现在有几个不活跃的变量。看看他们是否还能用些什么。但现在不需要任何东西来推动它的发展。希望这能帮助其他人尝试让.NET Core与远程Jeagertracing服务器一起正常工作。