英文:
How to get in OpenTelemetry otelgin middleware the traceid or full context to provide it to grpc clients?
问题
目前,我正在使用Go语言开发一组分布式微服务,并希望对其进行追踪。我有一个名为"api-gateway"的服务,通过REST方式工作,并通过gRPC调用所需的微服务。
我使用OpenTelemetry进行gRPC路径的追踪,但是一些otelgin中间件似乎没有提供任何可用于进一步追踪gRPC服务中相同traceid的上下文。
我使用默认的gin框架,并注入otelgin中间件,代码如下:
r := gin.Default()
r.Use(middleware.Middleware(
"Service-Name-XYZ",
))
根据我查看的中间件代码,应该有一种可追踪的上下文,我可以将其提供给gRPC客户端,以将其跨度添加到追踪中。但是,似乎在gin上下文中完全找不到traceparent
和tracestate
,因此gRPC客户端无法进行opentelemetry上下文传播。
据我所见,该中间件使用了我在InitTracer
函数中设置的默认TracerProvider
和TextMapPropagator
,该函数在main.go
中被调用。
func InitTracer() func(context.Context) error {
headers := map[string]string{
"signoz-access-token": signozToken,
}
secureOption := otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
if len(insecure) > 0 {
secureOption = otlptracegrpc.WithInsecure()
}
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint(collectorURL),
otlptracegrpc.WithHeaders(headers),
),
)
if err != nil {
log.Fatal(err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", serviceName),
attribute.String("library.language", "go"),
),
)
if err != nil {
log.Printf("Could not set resources: ", err)
}
otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(sdktrace.NewBatchSpanProcessor(exporter)),
sdktrace.WithSyncer(exporter),
sdktrace.WithResource(resources),
),
)
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
)
return exporter.Shutdown
}
所以我的问题是:
我是否遗漏了什么,或者otelgin是否没有提供任何可追踪的上下文,我可以将其提供给gRPC客户端以使用?
英文:
Atm, I work on a bunch of distributed microservices in go which I want to trace. I have an "api-gateway" that works via REST and then calls the needed microservice via gRPC.
My tracing via OpenTelemetry works fine for the grpc path but somehow the otelgin middleware seems not to provide any context I could use further to trace with the same traceid in the grpc services.
I use gin default and then inject the otelgin middleware like this.
r := gin.Default()
r.Use(middleware.Middleware(
"Service-Name-XYZ",
))
As far as I took a look at the middleware code there should be somehow a tracable context that I can give to the grpc client to add his span to the trace but somehow the traceparent
and the tracestate
seems to completely miss in the gin context
so that the grpc client cant take action of opentelemetry context propagation.
As far as I see the middleware uses the default set TracerProvider
and the TextMapPropagator
which I set in my InitTracer
function which gets called in the main.go
.
func InitTracer() func(context.Context) error {
headers := map[string]string{
"signoz-access-token": signozToken,
}
secureOption := otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
if len(insecure) > 0 {
secureOption = otlptracegrpc.WithInsecure()
}
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint(collectorURL),
otlptracegrpc.WithHeaders(headers),
),
)
if err != nil {
log.Fatal(err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", serviceName),
attribute.String("library.language", "go"),
),
)
if err != nil {
log.Printf("Could not set resources: ", err)
}
otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(sdktrace.NewBatchSpanProcessor(exporter)),
sdktrace.WithSyncer(exporter),
sdktrace.WithResource(resources),
),
)
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
)
return exporter.Shutdown
}
So my question is:
Do I miss something or does otelgin not provide any tracable context that I can give to the grpc client to work with?
答案1
得分: 1
如果您正在使用otelgin中间件,可通过ginContext.Request.Context()
传播可追踪的上下文。因此,您可以将此请求上下文传递给您的gRPC客户端以传播追踪信息。
英文:
If you are using otelgin middleware, the traceable context is propagated through ginContext.Request.Context()
. So you can pass on this request context instead of ginContext to your gRPC client to propagate trace.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论