From 919df4cd021a3d1b0e6c6e76008486900292fd34 Mon Sep 17 00:00:00 2001 From: madogar <36537062+madogar@users.noreply.github.com> Date: Tue, 16 Apr 2024 22:43:20 +0530 Subject: [PATCH] enhance: changes to propagate traceid from client (#32264) https://github.com/milvus-io/milvus/issues/32321 Issue Description: Tracing is an important means of identifying bottleneck points in a system and is crucial for debugging production issues. Milvus(or any DB) is generally the most downstream system for an user call -- a user call can originate from UI and pass through multiple components, in micro-services architecture, before reaching Milvus. So, when an user experiences a glitch, one would debug the call trace via logs using a common trace id. As of now, Milvus generates a new trace id for every call and this request is to make sure client can pass the trace id which will be used for all the logs across the Milvus sub-components so that one can fetch logs for a user call across the components -- including Milvus. Signed-off-by: Shreesha Srinath Madogaran Co-authored-by: Shreesha Srinath Madogaran --- pkg/util/logutil/grpc_interceptor.go | 2 ++ pkg/util/logutil/grpc_interceptor_test.go | 4 ++++ 2 files changed, 6 insertions(+) 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)) }) }