feat(trace): support for disabling tracing of specified spanName (#2363)

This commit is contained in:
chen quan
2022-10-22 22:14:12 +08:00
committed by GitHub
parent 22bdf0bbd5
commit 7fe2492009
2 changed files with 63 additions and 1 deletions

View File

@@ -2,7 +2,9 @@ package handler
import (
"net/http"
"sync"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
@@ -10,6 +12,13 @@ import (
oteltrace "go.opentelemetry.io/otel/trace"
)
var dontTracingSpanNames sync.Map
// DontTracingSpanName disable tracing for the specified spanName.
func DontTracingSpanName(spanName string) {
dontTracingSpanNames.Store(spanName, lang.Placeholder)
}
// 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 {
@@ -17,11 +26,21 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
next.ServeHTTP(w, r)
}()
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
spanName := path
if len(spanName) == 0 {
spanName = r.URL.Path
}
_, ok := dontTracingSpanNames.Load(spanName)
if ok {
return
}
spanCtx, span := tracer.Start(
ctx,
spanName,
@@ -33,7 +52,7 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
// convenient for tracking error messages
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
next.ServeHTTP(w, r.WithContext(spanCtx))
r = r.WithContext(spanCtx)
})
}
}