You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/tal-tech/go-zero/core/trace"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/propagation"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
|
oteltrace "go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// TracingHandler return a middleware that process the opentelemetry.
|
|
func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
propagator := otel.GetTextMapPropagator()
|
|
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
|
spanCtx, span := tracer.Start(
|
|
ctx,
|
|
path,
|
|
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
|
|
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(
|
|
serviceName, path, r)...),
|
|
)
|
|
defer span.End()
|
|
|
|
// convenient for tracking error messages
|
|
sc := span.SpanContext()
|
|
if sc.HasTraceID() {
|
|
w.Header().Set(trace.TraceIdKey, sc.TraceID().String())
|
|
}
|
|
|
|
next.ServeHTTP(w, r.WithContext(spanCtx))
|
|
})
|
|
}
|
|
}
|