Skip to content

Commit

Permalink
bklog: only log tracing ids when span exporter not nil
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Sep 8, 2021
1 parent ea773f6 commit 0509ac7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions util/bklog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bklog
import (
"context"

"github.com/moby/buildkit/util/tracing/detect"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -12,6 +13,10 @@ var (
L = logrus.NewEntry(logrus.StandardLogger())
)

var (
spanExporter, _ = detect.Exporter()
)

type (
loggerKey struct{}
)
Expand All @@ -33,13 +38,15 @@ func GetLogger(ctx context.Context) (l *logrus.Entry) {
l = L
}

spanContext := trace.SpanFromContext(ctx).SpanContext()
if spanExporter != nil {
spanContext := trace.SpanFromContext(ctx).SpanContext()

if spanContext.IsValid() {
return l.WithFields(logrus.Fields{
"traceID": spanContext.TraceID(),
"spanID": spanContext.SpanID(),
})
if spanContext.IsValid() {
return l.WithFields(logrus.Fields{
"traceID": spanContext.TraceID(),
"spanID": spanContext.SpanID(),
})
}
}

return l
Expand Down

0 comments on commit 0509ac7

Please sign in to comment.