Add traceId to the response headers (#919)

* Add traceId to the request headers

* Add test cases

* Update refactor code
master
chenquan 3 years ago committed by GitHub
parent 14ec29991c
commit 7c842f22d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,6 @@
package trace package trace
const ( const (
traceIdKey = "X-Trace-ID" TraceIdKey = "X-Trace-ID"
spanIdKey = "X-Span-ID" spanIdKey = "X-Span-ID"
) )

@ -11,11 +11,11 @@ import (
func TestHttpPropagator_Extract(t *testing.T) { func TestHttpPropagator_Extract(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Set(traceIdKey, "trace") req.Header.Set(TraceIdKey, "trace")
req.Header.Set(spanIdKey, "span") req.Header.Set(spanIdKey, "span")
carrier, err := Extract(HttpFormat, req.Header) carrier, err := Extract(HttpFormat, req.Header)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "trace", carrier.Get(TraceIdKey))
assert.Equal(t, "span", carrier.Get(spanIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey))
_, err = Extract(HttpFormat, req) _, err = Extract(HttpFormat, req)
@ -24,11 +24,11 @@ func TestHttpPropagator_Extract(t *testing.T) {
func TestHttpPropagator_Inject(t *testing.T) { func TestHttpPropagator_Inject(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Set(traceIdKey, "trace") req.Header.Set(TraceIdKey, "trace")
req.Header.Set(spanIdKey, "span") req.Header.Set(spanIdKey, "span")
carrier, err := Inject(HttpFormat, req.Header) carrier, err := Inject(HttpFormat, req.Header)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "trace", carrier.Get(TraceIdKey))
assert.Equal(t, "span", carrier.Get(spanIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey))
_, err = Inject(HttpFormat, req) _, err = Inject(HttpFormat, req)
@ -37,12 +37,12 @@ func TestHttpPropagator_Inject(t *testing.T) {
func TestGrpcPropagator_Extract(t *testing.T) { func TestGrpcPropagator_Extract(t *testing.T) {
md := metadata.New(map[string]string{ md := metadata.New(map[string]string{
traceIdKey: "trace", TraceIdKey: "trace",
spanIdKey: "span", spanIdKey: "span",
}) })
carrier, err := Extract(GrpcFormat, md) carrier, err := Extract(GrpcFormat, md)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "trace", carrier.Get(TraceIdKey))
assert.Equal(t, "span", carrier.Get(spanIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey))
_, err = Extract(GrpcFormat, 1) _, err = Extract(GrpcFormat, 1)
@ -53,12 +53,12 @@ func TestGrpcPropagator_Extract(t *testing.T) {
func TestGrpcPropagator_Inject(t *testing.T) { func TestGrpcPropagator_Inject(t *testing.T) {
md := metadata.New(map[string]string{ md := metadata.New(map[string]string{
traceIdKey: "trace", TraceIdKey: "trace",
spanIdKey: "span", spanIdKey: "span",
}) })
carrier, err := Inject(GrpcFormat, md) carrier, err := Inject(GrpcFormat, md)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "trace", carrier.Get(TraceIdKey))
assert.Equal(t, "span", carrier.Get(spanIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey))
_, err = Inject(GrpcFormat, 1) _, err = Inject(GrpcFormat, 1)

@ -34,7 +34,7 @@ type Span struct {
func newServerSpan(carrier Carrier, serviceName, operationName string) tracespec.Trace { func newServerSpan(carrier Carrier, serviceName, operationName string) tracespec.Trace {
traceId := stringx.TakeWithPriority(func() string { traceId := stringx.TakeWithPriority(func() string {
if carrier != nil { if carrier != nil {
return carrier.Get(traceIdKey) return carrier.Get(TraceIdKey)
} }
return "" return ""
}, stringx.RandId) }, stringx.RandId)

@ -57,7 +57,7 @@ func TestServerSpan(t *testing.T) {
func TestServerSpan_WithCarrier(t *testing.T) { func TestServerSpan_WithCarrier(t *testing.T) {
md := metadata.New(map[string]string{ md := metadata.New(map[string]string{
traceIdKey: "a", TraceIdKey: "a",
spanIdKey: "0.1", spanIdKey: "0.1",
}) })
ctx, span := StartServerSpan(context.Background(), grpcCarrier(md), "service", "operation") ctx, span := StartServerSpan(context.Background(), grpcCarrier(md), "service", "operation")
@ -99,7 +99,7 @@ func TestSpan_Follow(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(stringx.RandId(), func(t *testing.T) { t.Run(stringx.RandId(), func(t *testing.T) {
md := metadata.New(map[string]string{ md := metadata.New(map[string]string{
traceIdKey: "a", TraceIdKey: "a",
spanIdKey: test.span, spanIdKey: test.span,
}) })
ctx, span := StartServerSpan(context.Background(), grpcCarrier(md), ctx, span := StartServerSpan(context.Background(), grpcCarrier(md),

@ -14,6 +14,6 @@ func (sc spanContext) SpanId() string {
} }
func (sc spanContext) Visit(fn func(key, val string) bool) { func (sc spanContext) Visit(fn func(key, val string) bool) {
fn(traceIdKey, sc.traceId) fn(TraceIdKey, sc.traceId)
fn(spanIdKey, sc.spanId) fn(spanIdKey, sc.spanId)
} }

@ -21,6 +21,8 @@ func TracingHandler(next http.Handler) http.Handler {
defer span.Finish() defer span.Finish()
r = r.WithContext(ctx) r = r.WithContext(ctx)
// Conveniently track error messages
w.Header().Set(trace.TraceIdKey, span.TraceId())
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }

@ -1,6 +1,8 @@
package handler package handler
import ( import (
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/core/trace"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -11,14 +13,19 @@ import (
func TestTracingHandler(t *testing.T) { func TestTracingHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Set("X-Trace-ID", "theid")
traceId := stringx.RandId()
req.Header.Set(trace.TraceIdKey, traceId)
handler := TracingHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := TracingHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span, ok := r.Context().Value(tracespec.TracingKey).(tracespec.Trace) span, ok := r.Context().Value(tracespec.TracingKey).(tracespec.Trace)
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, "theid", span.TraceId()) assert.Equal(t, traceId, span.TraceId())
})) }))
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req) handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code) assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, traceId, resp.Header().Get(trace.TraceIdKey))
} }

Loading…
Cancel
Save