From 0f52917bf3bff26834f1f101847bd29acbc8c717 Mon Sep 17 00:00:00 2001 From: Morlay Date: Wed, 8 Sep 2021 10:28:08 +0800 Subject: [PATCH] bklog: only log tracing ids when span exporter not nil Signed-off-by: Morlay --- util/bklog/log.go | 22 +++++++++++++++------- util/tracing/detect/detect.go | 5 +++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/util/bklog/log.go b/util/bklog/log.go index 73324a69c0de..99e6c9953656 100644 --- a/util/bklog/log.go +++ b/util/bklog/log.go @@ -12,6 +12,14 @@ var ( L = logrus.NewEntry(logrus.StandardLogger()) ) +var ( + logWithTraceID = false +) + +func EnableLogWithTraceID(b bool) { + logWithTraceID = b +} + type ( loggerKey struct{} ) @@ -33,13 +41,13 @@ func GetLogger(ctx context.Context) (l *logrus.Entry) { l = L } - spanContext := trace.SpanFromContext(ctx).SpanContext() - - if spanContext.IsValid() { - return l.WithFields(logrus.Fields{ - "traceID": spanContext.TraceID(), - "spanID": spanContext.SpanID(), - }) + if logWithTraceID { + if spanContext := trace.SpanFromContext(ctx).SpanContext(); spanContext.IsValid() { + return l.WithFields(logrus.Fields{ + "traceID": spanContext.TraceID(), + "spanID": spanContext.SpanID(), + }) + } } return l diff --git a/util/tracing/detect/detect.go b/util/tracing/detect/detect.go index 22b1ea2e4be0..ac8d2a6f3e1d 100644 --- a/util/tracing/detect/detect.go +++ b/util/tracing/detect/detect.go @@ -8,6 +8,7 @@ import ( "strconv" "sync" + "github.com/moby/buildkit/util/bklog" "github.com/pkg/errors" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" @@ -82,6 +83,10 @@ func detect() error { if exp == nil { return nil } + + // enable log with traceID when valid exporter + bklog.EnableLogWithTraceID(true) + res, err := resource.Detect(context.Background(), serviceNameDetector{}) if err != nil { return err