Skip to content

Commit

Permalink
feat(trace): support for disabling tracing of specified spanName
Browse files Browse the repository at this point in the history
  • Loading branch information
chenquan committed Sep 6, 2022
1 parent 6078bf1 commit 95cde3e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
21 changes: 20 additions & 1 deletion rest/handler/tracinghandler.go
Expand Up @@ -2,26 +2,45 @@ 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"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
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 {
propagator := otel.GetTextMapPropagator()
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,
Expand All @@ -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)
})
}
}
43 changes: 43 additions & 0 deletions rest/handler/tracinghandler_test.go
Expand Up @@ -50,3 +50,46 @@ func TestOtelHandler(t *testing.T) {
})
}
}

func TestDontTracingSpanName(t *testing.T) {
ztrace.StartAgent(ztrace.Config{
Name: "go-zero-test",
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})

DontTracingSpanName("bar")

for _, test := range []string{"", "bar", "foo"} {
t.Run(test, func(t *testing.T) {
h := chain.New(TracingHandler("foo", test)).Then(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
spanCtx := trace.SpanContextFromContext(r.Context())
if test == "bar" {
assert.False(t, spanCtx.IsValid())
return
}

assert.True(t, spanCtx.IsValid())
}))
ts := httptest.NewServer(h)
defer ts.Close()

client := ts.Client()
err := func(ctx context.Context) error {
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
defer span.End()

req, _ := http.NewRequest("GET", ts.URL, nil)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))

res, err := client.Do(req)
assert.Nil(t, err)
return res.Body.Close()
}(context.Background())

assert.Nil(t, err)
})
}
}

0 comments on commit 95cde3e

Please sign in to comment.