1 回答

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
好吧,我想出了如何讓它工作。我添加了此中間件,它將請(qǐng)求上下文與標(biāo)頭跟蹤 ID、spanID 和跟蹤標(biāo)志同步。在此之后,我們可以根據(jù)需要?jiǎng)?chuàng)建跟蹤器和跨度。
func Tracing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
traceID, _ := trace.TraceIDFromHex(r.Header["X-B3-Traceid"][0])
spanID, _ := trace.SpanIDFromHex(r.Header["X-B3-Spanid"][0])
var traceFlags trace.TraceFlags
if r.Header["X-B3-Sampled"][0] == "1" {
traceFlags = trace.FlagsSampled
}
spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: traceFlags,
})
ctx := trace.ContextWithSpanContext(r.Context(), spanContext)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
- 1 回答
- 0 關(guān)注
- 95 瀏覽
添加回答
舉報(bào)