chore(trace): improve rest tracinghandler (#2783)

master
cong 2 years ago committed by GitHub
parent a2dcb0079a
commit 43b8c7f641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,7 +31,7 @@ func TracingHandler(serviceName, path string, opts ...TracingOption) func(http.H
ignorePaths := collection.NewSet() ignorePaths := collection.NewSet()
ignorePaths.AddStr(tracingOpts.traceIgnorePaths...) ignorePaths.AddStr(tracingOpts.traceIgnorePaths...)
traceHandler := func(checkIgnore bool) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
propagator := otel.GetTextMapPropagator() propagator := otel.GetTextMapPropagator()
tracer := otel.GetTracerProvider().Tracer(trace.TraceName) tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
@ -42,7 +42,7 @@ func TracingHandler(serviceName, path string, opts ...TracingOption) func(http.H
spanName = r.URL.Path spanName = r.URL.Path
} }
if ignorePaths.Contains(spanName) { if checkIgnore && ignorePaths.Contains(spanName) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
@ -68,10 +68,13 @@ func TracingHandler(serviceName, path string, opts ...TracingOption) func(http.H
}) })
} }
} }
checkIgnore := ignorePaths.Count() > 0
return traceHandler(checkIgnore)
}
// WithTraceIgnorePaths specifies the traceIgnorePaths option for TracingHandler. // WithTraceIgnorePaths specifies the traceIgnorePaths option for TracingHandler.
func WithTraceIgnorePaths(traceIgnorePaths []string) TracingOption { func WithTraceIgnorePaths(traceIgnorePaths []string) TracingOption {
return func(options *tracingOptions) { return func(options *tracingOptions) {
options.traceIgnorePaths = traceIgnorePaths options.traceIgnorePaths = append(options.traceIgnorePaths, traceIgnorePaths...)
} }
} }

Loading…
Cancel
Save