[英]opentelemetry golang always give all 0 for traceID and spanID
I am totally new to the distributed world and trying to use open telemetry for distributed tracing.我对分布式世界完全陌生,并尝试使用开放遥测进行分布式跟踪。 For phase 1, just trying to have a request id (traceID / spanID) going, so we can co-relate the request logs.
对于第 1 阶段,只是尝试让请求 id (traceID / spanID) 继续,因此我们可以关联请求日志。 Have not yet decided on collector/ exporter.
尚未决定收集器/出口商。
My span/ trace id is always zero, in below middleware code Is it because I have not initialized the tracer with all the exporter/ collector etc?我的跨度/跟踪 ID 始终为零,在下面的中间件代码中是因为我没有使用所有导出器/收集器等初始化跟踪器吗?
func AddSpanId(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
values := []interface{}{}
spanID, traceID := getRequestIDs(r)
values = append(values, "spanID", spanID)
values = append(values, "traceID", traceID)
//code to wrap values into context
}
func getRequestIDs(r *http.Request) (string, string) {
_, span := otel.Tracer("somename").Start(r.Context(), "handler")
fmt.Print(span.SpanContext().SpanID().String())
return span.SpanContext().SpanID().String(), span.SpanContext().TraceID().String()
}
You need to instrument your http
handler:您需要检测您的
http
处理程序:
import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
// Package-level tracer.
// This should be configured in your code setup instead of here.
var tracer = otel.Tracer("github.com/full/path/to/mypkg")
func main() {
// Wrap your httpHandler function.
handler := http.HandlerFunc(httpHandler)
wrappedHandler := otelhttp.NewHandler(handler, "hello-instrumented")
http.Handle("/hello-instrumented", wrappedHandler)
// And start the HTTP serve.
log.Fatal(http.ListenAndServe(":3030", nil))
}
https://opentelemetry.io/docs/instrumentation/go/libraries/ https://opentelemetry.io/docs/instrumentation/go/libraries/
You also rarely need to work directly with traceID and spanID.您也很少需要直接使用 traceID 和 spanID。 Starting Span API is typical building block to instrument the code.
启动 Span API 是检测代码的典型构建块。
_, span := otel.Tracer(name).Start(ctx, "Poll")
defer span.End()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.