diff --git a/pkg/util/logutil/grpc_interceptor.go b/pkg/util/logutil/grpc_interceptor.go index a6660b029b..35a4bbf303 100644 --- a/pkg/util/logutil/grpc_interceptor.go +++ b/pkg/util/logutil/grpc_interceptor.go @@ -66,6 +66,8 @@ func withLevelAndTrace(ctx context.Context) context.Context { if len(requestID) >= 1 { // inject traceid in order to pass client request id newctx = metadata.AppendToOutgoingContext(newctx, clientRequestIDKey, requestID[0]) + // inject traceid from client for info/debug/warn/error logs + newctx = log.WithTraceID(newctx, requestID[0]) } } if !traceID.IsValid() { diff --git a/pkg/util/logutil/grpc_interceptor_test.go b/pkg/util/logutil/grpc_interceptor_test.go index 193516bf00..32fb4542a3 100644 --- a/pkg/util/logutil/grpc_interceptor_test.go +++ b/pkg/util/logutil/grpc_interceptor_test.go @@ -54,6 +54,10 @@ func TestCtxWithLevelAndTrace(t *testing.T) { assert.True(t, ok) assert.Equal(t, "client-req-id", md.Get(clientRequestIDKey)[0]) assert.Equal(t, zapcore.ErrorLevel.String(), md.Get(logLevelRPCMetaKey)[0]) + expectedctx := context.TODO() + expectedctx = log.WithErrorLevel(expectedctx) + expectedctx = log.WithTraceID(expectedctx, md.Get(clientRequestIDKey)[0]) + assert.Equal(t, log.Ctx(expectedctx), log.Ctx(newctx)) }) }