chore(trace): improve rest tracinghandler (#2783)
This commit is contained in:
@@ -31,47 +31,50 @@ 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 {
|
||||||
|
propagator := otel.GetTextMapPropagator()
|
||||||
|
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
||||||
|
|
||||||
return func(next http.Handler) http.Handler {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
propagator := otel.GetTextMapPropagator()
|
spanName := path
|
||||||
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
if len(spanName) == 0 {
|
||||||
|
spanName = r.URL.Path
|
||||||
|
}
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
if checkIgnore && ignorePaths.Contains(spanName) {
|
||||||
spanName := path
|
next.ServeHTTP(w, r)
|
||||||
if len(spanName) == 0 {
|
return
|
||||||
spanName = r.URL.Path
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ignorePaths.Contains(spanName) {
|
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
||||||
next.ServeHTTP(w, r)
|
spanCtx, span := tracer.Start(
|
||||||
return
|
ctx,
|
||||||
}
|
spanName,
|
||||||
|
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
|
||||||
|
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(
|
||||||
|
serviceName, spanName, r)...),
|
||||||
|
)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
|
// convenient for tracking error messages
|
||||||
spanCtx, span := tracer.Start(
|
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
|
||||||
ctx,
|
|
||||||
spanName,
|
|
||||||
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
|
|
||||||
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(
|
|
||||||
serviceName, spanName, r)...),
|
|
||||||
)
|
|
||||||
defer span.End()
|
|
||||||
|
|
||||||
// convenient for tracking error messages
|
trw := &response.WithCodeResponseWriter{Writer: w, Code: http.StatusOK}
|
||||||
propagator.Inject(spanCtx, propagation.HeaderCarrier(w.Header()))
|
next.ServeHTTP(trw, r.WithContext(spanCtx))
|
||||||
|
|
||||||
trw := &response.WithCodeResponseWriter{Writer: w, Code: http.StatusOK}
|
span.SetAttributes(semconv.HTTPAttributesFromHTTPStatusCode(trw.Code)...)
|
||||||
next.ServeHTTP(trw, r.WithContext(spanCtx))
|
span.SetStatus(semconv.SpanStatusFromHTTPStatusCodeAndSpanKind(trw.Code, oteltrace.SpanKindServer))
|
||||||
|
})
|
||||||
span.SetAttributes(semconv.HTTPAttributesFromHTTPStatusCode(trw.Code)...)
|
}
|
||||||
span.SetStatus(semconv.SpanStatusFromHTTPStatusCodeAndSpanKind(trw.Code, oteltrace.SpanKindServer))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
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...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user