gpt4 book ai didi

go - 如何从字符串 traceid 创建 opentelemetry 跨度

转载 作者:行者123 更新时间:2023-12-05 02:35:58 27 4
gpt4 key购买 nike

我知道我们要使用上下文传播来获取创建为彼此子项的父 traceid 和跨度,但我的发布者使用的是 header (nats 而不是 http)

我的消息代理使用 header ,我在出站请求中将 traceid 和 spanid 设置为 header ,发送消息,然后订阅者应该能够创建一个新的跨度,将父 traceid 设置为来自要求。链接他们

我的出站请求如下所示:

        msg := new(nats.Msg)
msg.Data = []byte("new request being sent!")
msg.Subject = subject
getTraceID := requestSpan.SpanContext().TraceID().String()
header := make(nats.Header)
msg.Header = header
header.Set("traceid", getTraceID)

getSpanID := requestSpan.SpanContext().SpanID().String(
header.Set("spanid", getSpanID)
msg.Header = header

reply, err := nc.RequestMsg(msg, time.Duration(5*time.Second))

这有效,在订阅者端我可以获得跟踪和跨度 ID 的 header 值

如何使用 traceid 在订阅者端构建上下文/跨度?

我相信我可以在 channel 内做这样的事情:

    var traceID trace.TraceID
traceID, err = trace.TraceIDFromHex(request.TraceID)
if err != nil {
fmt.Println("error: ", err)
continue
}
var spanID trace.SpanID
spanID, err = trace.SpanIDFromHex(request.SpanID)
if err != nil {
fmt.Println("error: ", err)
continue
}

spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: 01,
})

ctx := context.Background()
ctx = trace.ContextWithSpanContext(ctx, spanContext)
var requestInLoopSpan trace.Span
ctx2, requestInLoopSpan := otel.Tracer("requestInLoop").Start(ctx, "requestInLoopSpan")

requestInLoopSpan.AddEvent("processing....") // NOT WORKING

最佳答案

知道了!

顺便说一句,输入 NewRequest:

type NewRequest struct {
Requestid string `json: "requestid"`
TraceID string
SpanID string
}

您需要将构造 spanContext 的代码包装在一个函数中:

func constructNewSpanContext(request NewRequest) (spanContext trace.SpanContext, err error) {
var traceID trace.TraceID
traceID, err = trace.TraceIDFromHex(request.TraceID)
if err != nil {
fmt.Println("error: ", err)
return spanContext, err
}
var spanID trace.SpanID
spanID, err = trace.SpanIDFromHex(request.SpanID)
if err != nil {
fmt.Println("error: ", err)
return spanContext, err
}
var spanContextConfig trace.SpanContextConfig
spanContextConfig.TraceID = traceID
spanContextConfig.SpanID = spanID
spanContextConfig.TraceFlags = 01
spanContextConfig.Remote = false
spanContext = trace.NewSpanContext(spanContextConfig)
return spanContext, nil
}

然后你称之为传递包含跟踪和跨度 ID 的东西

然后使用函数返回的 spanContext 来丰富上下文:

        spanContext, err := constructNewSpanContext(request)
if err != nil {
fmt.Println("ERROR: ", err)
}
fmt.Println("IS VALID? ", spanContext.IsValid()) // check if okay

requestContext := context.Background()
requestContext = trace.ContextWithSpanContext(requestContext, spanContext)

var requestInLoopSpan trace.Span
childContext, requestInLoopSpan := otel.Tracer("inboundmessage").Start(requestContext, "requestInLoopSpan")
requestInLoopSpan.AddEvent("processing....") // WORKING

关于go - 如何从字符串 traceid 创建 opentelemetry 跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70378025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com