对于在此页面上查看 .net 核心中 jaeger opentracing 的类似问题的任何人来说,这是一个工作 docker compose 片段和 Startup.cs 中的工作代码。我缺少的部分是让 jaeger 从 docker-compose 读取环境变量,因为默认情况下它试图通过本地主机上的 udp 发送跟踪。
version: '3.4'
services:
jaeger-agent:
container_name: 'tracing.jaeger.agent'
image: jaegertracing/all-in-one:latest
networks:
- jaeger-demo
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778/tcp"
- "16686:16686"
- "14268:14268"
- "9411:9411"
environment:
- LOG_LEVEL=debug
labels:
NAME: "jaeger-agent"
orderService:
container_name: 'tracing.orders.api'
image: ${DOCKER_REGISTRY-}orderservice.api
build:
context: .
dockerfile: OrdersApi/Dockerfile
networks:
- jaeger-demo
ports:
- "16252:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- JAEGER_SERVICE_NAME=OrdersApi
- JAEGER_AGENT_HOST=jaeger-agent
- JAEGER_AGENT_PORT=6831
- JAEGER_SAMPLER_TYPE=const
- JAEGER_SAMPLER_PARAM=1
depends_on:
- jaeger-agent
customerService:
container_name: 'tracing.customers.api'
image: ${DOCKER_REGISTRY-}customerservice.api
build:
context: .
dockerfile: CustomerApi/Dockerfile
networks:
- jaeger-demo
ports:
- "17000:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- JAEGER_SERVICE_NAME=CustomersApi
- JAEGER_AGENT_HOST=jaeger-agent
- JAEGER_AGENT_PORT=6831
- JAEGER_SAMPLER_TYPE=const
- JAEGER_SAMPLER_PARAM=1
depends_on:
- jaeger-agent
networks:
jaeger-demo:
将以下 nuget 包添加到您的 api 中。
<PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.6.2" />
<PackageReference Include="Jaeger" Version="0.4.2" />
然后在您的 Startup.cs 配置方法中,您需要配置 jaeger 和 opentracing,如下所示。
services.AddSingleton<ITracer>(serviceProvider =>
{
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
Jaeger.Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory).RegisterSenderFactory<ThriftSenderFactory>();
var config = Jaeger.Configuration.FromEnv(loggerFactory);
ITracer tracer = config.GetTracer();
GlobalTracer.Register(tracer);
return tracer;
});
services.AddOpenTracing();