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

@@ -51,3 +51,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)
})
}
}