如何通过kafka传播开放遥测跨度?

问题描述 投票:0回答:1

我想将跟踪从一个服务传播到另一个我通过 Kafka 发送数据的服务。 我能够使用以下逻辑创建子跟踪。但是,opentelemetry 代理不会将子跟踪发送到 otel-collector。 我不明白为什么这不起作用。是否有推荐的方法通过传播器序列化和传递上下文。 我只能找到 http propogator,所以不确定如何以字节数组格式发送数据。

Take an existing span and get its span context
spanContextParent := span.SpanContext()
parentTraceId := spanContextParent.TraceID()
parentSpanId := spanContextParent.SpanID()
Taking out the trace id and 
// taking original span context
stateByteArray := []byte(spanContext.TraceState().String())
traceByteArray := []byte(spanContext.TraceID().String())
spanByteArray := []byte(spanContext.SpanID().String())

// we send byte array via kafka producer
// Asssuming sending this string to some service now converting back
traceId, err := trace.TraceIDFromHex(string(traceByteArray))
if err != nil {
    return trace.SpanContextConfig{}, err
}

spanIDFromHex, err := trace.SpanIDFromHex(string(spanByteArray))
if err != nil {
    return trace.SpanContextConfig{}, err
}
traceState, err := trace.ParseTraceState(string(stateByteArray))
if err != nil {
    return trace.SpanContextConfig{}, err
}
config := trace.SpanContextConfig{
    TraceID:    traceId,
    SpanID:     spanIDFromHex,
    TraceState: traceState,
}
// start a child span
newSpanContext := trace.NewSpanContext(config.OtelParentSpanContextConfig)
remoteSpanContext := trace.ContextWithRemoteSpanContext(ctx, newSpanContext)
newCtx, dualSpan := m.StartSpan(remoteSpanContext, spanName)
apache-kafka message-queue trace open-telemetry telemetry
1个回答
0
投票

您应该使用传播器来注入和提取跨度上下文。 传播器并不特定于 HTTP,您只需要指定如何表示 kafka headers。

opentelemetry-demosarama的上下文传播示例:生产者消费者

© www.soinside.com 2019 - 2024. All rights reserved.