refactor(rest): use static config for trace ignore paths. (#2773)
This commit is contained in:
@@ -57,5 +57,7 @@ type (
|
|||||||
Signature SignatureConf `json:",optional"`
|
Signature SignatureConf `json:",optional"`
|
||||||
// There are default values for all the items in Middlewares.
|
// There are default values for all the items in Middlewares.
|
||||||
Middlewares MiddlewaresConf
|
Middlewares MiddlewaresConf
|
||||||
|
// TraceIgnorePaths is paths blacklist for trace middleware.
|
||||||
|
TraceIgnorePaths []string `json:",optional"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -118,7 +118,9 @@ func (ng *engine) buildChainWithNativeMiddlewares(fr featuredRoutes, route Route
|
|||||||
chn := chain.New()
|
chn := chain.New()
|
||||||
|
|
||||||
if ng.conf.Middlewares.Trace {
|
if ng.conf.Middlewares.Trace {
|
||||||
chn = chn.Append(handler.TracingHandler(ng.conf.Name, route.Path))
|
chn = chn.Append(handler.TracingHandler(ng.conf.Name,
|
||||||
|
route.Path,
|
||||||
|
handler.WithTraceIgnorePaths(ng.conf.TraceIgnorePaths)))
|
||||||
}
|
}
|
||||||
if ng.conf.Middlewares.Log {
|
if ng.conf.Middlewares.Log {
|
||||||
chn = chn.Append(ng.getLogHandler())
|
chn = chn.Append(ng.getLogHandler())
|
||||||
@@ -202,7 +204,9 @@ func (ng *engine) getShedder(priority bool) load.Shedder {
|
|||||||
func (ng *engine) notFoundHandler(next http.Handler) http.Handler {
|
func (ng *engine) notFoundHandler(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
chn := chain.New(
|
chn := chain.New(
|
||||||
handler.TracingHandler(ng.conf.Name, ""),
|
handler.TracingHandler(ng.conf.Name,
|
||||||
|
"",
|
||||||
|
handler.WithTraceIgnorePaths(ng.conf.TraceIgnorePaths)),
|
||||||
ng.getLogHandler(),
|
ng.getLogHandler(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,8 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/collection"
|
||||||
"github.com/zeromicro/go-zero/core/trace"
|
"github.com/zeromicro/go-zero/core/trace"
|
||||||
"github.com/zeromicro/go-zero/rest/internal/response"
|
"github.com/zeromicro/go-zero/rest/internal/response"
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
@@ -13,15 +12,26 @@ import (
|
|||||||
oteltrace "go.opentelemetry.io/otel/trace"
|
oteltrace "go.opentelemetry.io/otel/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var notTracingSpans sync.Map
|
type (
|
||||||
|
// TracingOption defines the method to customize an tracingOptions.
|
||||||
|
TracingOption func(options *tracingOptions)
|
||||||
|
|
||||||
// DontTraceSpan disable tracing for the specified span name.
|
// tracingOptions is TracingHandler options.
|
||||||
func DontTraceSpan(spanName string) {
|
tracingOptions struct {
|
||||||
notTracingSpans.Store(spanName, lang.Placeholder)
|
traceIgnorePaths []string
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// TracingHandler return a middleware that process the opentelemetry.
|
// TracingHandler return a middleware that process the opentelemetry.
|
||||||
func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
func TracingHandler(serviceName, path string, opts ...TracingOption) func(http.Handler) http.Handler {
|
||||||
|
var tracingOpts tracingOptions
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&tracingOpts)
|
||||||
|
}
|
||||||
|
|
||||||
|
ignorePaths := collection.NewSet()
|
||||||
|
ignorePaths.AddStr(tracingOpts.traceIgnorePaths...)
|
||||||
|
|
||||||
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)
|
||||||
@@ -32,7 +42,7 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|||||||
spanName = r.URL.Path
|
spanName = r.URL.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := notTracingSpans.Load(spanName); ok {
|
if ignorePaths.Contains(spanName) {
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -58,3 +68,10 @@ func TracingHandler(serviceName, path string) func(http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTraceIgnorePaths specifies the traceIgnorePaths option for TracingHandler.
|
||||||
|
func WithTraceIgnorePaths(traceIgnorePaths []string) TracingOption {
|
||||||
|
return func(options *tracingOptions) {
|
||||||
|
options.traceIgnorePaths = traceIgnorePaths
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,12 +63,9 @@ func TestDontTracingSpan(t *testing.T) {
|
|||||||
})
|
})
|
||||||
defer ztrace.StopAgent()
|
defer ztrace.StopAgent()
|
||||||
|
|
||||||
DontTraceSpan("bar")
|
|
||||||
defer notTracingSpans.Delete("bar")
|
|
||||||
|
|
||||||
for _, test := range []string{"", "bar", "foo"} {
|
for _, test := range []string{"", "bar", "foo"} {
|
||||||
t.Run(test, func(t *testing.T) {
|
t.Run(test, func(t *testing.T) {
|
||||||
h := chain.New(TracingHandler("foo", test)).Then(
|
h := chain.New(TracingHandler("foo", test, WithTraceIgnorePaths([]string{"bar"}))).Then(
|
||||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
span := trace.SpanFromContext(r.Context())
|
span := trace.SpanFromContext(r.Context())
|
||||||
spanCtx := span.SpanContext()
|
spanCtx := span.SpanContext()
|
||||||
|
|||||||
Reference in New Issue
Block a user