mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 13:28:49 +08:00
919df4cd02
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 <smadogaran@salesforce.com> Co-authored-by: Shreesha Srinath Madogaran <smadogaran@salesforce.com>
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package logutil
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/zap/zapcore"
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
)
|
|
|
|
func TestCtxWithLevelAndTrace(t *testing.T) {
|
|
t.Run("debug level", func(t *testing.T) {
|
|
ctx := withMetaData(context.TODO(), zapcore.DebugLevel)
|
|
newctx := withLevelAndTrace(ctx)
|
|
|
|
assert.Equal(t, log.Ctx(log.WithDebugLevel(context.TODO())), log.Ctx(newctx))
|
|
})
|
|
|
|
t.Run("info level", func(t *testing.T) {
|
|
ctx := context.TODO()
|
|
newctx := withLevelAndTrace(withMetaData(ctx, zapcore.InfoLevel))
|
|
assert.Equal(t, log.Ctx(log.WithInfoLevel(ctx)), log.Ctx(newctx))
|
|
})
|
|
|
|
t.Run("warn level", func(t *testing.T) {
|
|
ctx := context.TODO()
|
|
newctx := withLevelAndTrace(withMetaData(ctx, zapcore.WarnLevel))
|
|
assert.Equal(t, log.Ctx(log.WithWarnLevel(ctx)), log.Ctx(newctx))
|
|
})
|
|
|
|
t.Run("error level", func(t *testing.T) {
|
|
ctx := context.TODO()
|
|
newctx := withLevelAndTrace(withMetaData(ctx, zapcore.ErrorLevel))
|
|
assert.Equal(t, log.Ctx(log.WithErrorLevel(ctx)), log.Ctx(newctx))
|
|
})
|
|
|
|
t.Run("fatal level", func(t *testing.T) {
|
|
ctx := context.TODO()
|
|
newctx := withLevelAndTrace(withMetaData(ctx, zapcore.FatalLevel))
|
|
assert.Equal(t, log.Ctx(log.WithFatalLevel(ctx)), log.Ctx(newctx))
|
|
})
|
|
|
|
t.Run(("pass through variables"), func(t *testing.T) {
|
|
md := metadata.New(map[string]string{
|
|
logLevelRPCMetaKey: zapcore.ErrorLevel.String(),
|
|
clientRequestIDKey: "client-req-id",
|
|
})
|
|
ctx := metadata.NewIncomingContext(context.TODO(), md)
|
|
newctx := withLevelAndTrace(ctx)
|
|
md, ok := metadata.FromOutgoingContext(newctx)
|
|
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))
|
|
})
|
|
}
|
|
|
|
func withMetaData(ctx context.Context, level zapcore.Level) context.Context {
|
|
md := metadata.New(map[string]string{
|
|
logLevelRPCMetaKey: level.String(),
|
|
})
|
|
return metadata.NewIncomingContext(ctx, md)
|
|
}
|