From 1ed2d45894899281bc861f43cafdf361a4183987 Mon Sep 17 00:00:00 2001 From: Morlay Date: Fri, 9 Jul 2021 11:54:04 +0800 Subject: [PATCH] log with traceID and spanID Signed-off-by: Morlay --- cmd/buildkitd/main.go | 1 + util/log/log.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 util/log/log.go diff --git a/cmd/buildkitd/main.go b/cmd/buildkitd/main.go index 00244429..94efa9fe 100644 --- a/cmd/buildkitd/main.go +++ b/cmd/buildkitd/main.go @@ -45,6 +45,7 @@ import ( "github.com/moby/buildkit/util/appdefaults" "github.com/moby/buildkit/util/archutil" "github.com/moby/buildkit/util/grpcerrors" + _ "github.com/moby/buildkit/util/log" "github.com/moby/buildkit/util/profiler" "github.com/moby/buildkit/util/resolver" "github.com/moby/buildkit/util/stack" diff --git a/util/log/log.go b/util/log/log.go new file mode 100644 index 00000000..6e3536a3 --- /dev/null +++ b/util/log/log.go @@ -0,0 +1,28 @@ +package log + +import ( + "context" + + "github.com/containerd/containerd/log" + "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/trace" +) + +func init() { + log.G = GetLogger +} + +func GetLogger(ctx context.Context) *logrus.Entry { + l := log.GetLogger(ctx) + + spanContext := trace.SpanFromContext(ctx).SpanContext() + + if spanContext.IsValid() { + return l.WithFields(logrus.Fields{ + "traceID": spanContext.TraceID(), + "spanID": spanContext.SpanID(), + }) + } + + return l +}