Add traceId to the response headers (#919)

* Add traceId to the request headers

* Add test cases

* Update refactor code
This commit is contained in:
chenquan
2021-08-17 10:12:08 +08:00
committed by GitHub
parent 14ec29991c
commit 7c842f22d0
7 changed files with 24 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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

View File

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