Skip to content

Commit

Permalink
Merge pull request #2351 from morlay/log-fix
Browse files Browse the repository at this point in the history
bklog: only log tracing ids when span exporter not nil
  • Loading branch information
tonistiigi authored Sep 9, 2021
2 parents f5eb400 + 0f52917 commit e07f388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 15 additions & 7 deletions util/bklog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ var (
L = logrus.NewEntry(logrus.StandardLogger())
)

var (
logWithTraceID = false
)

func EnableLogWithTraceID(b bool) {
logWithTraceID = b
}

type (
loggerKey struct{}
)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions util/tracing/detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e07f388

Please sign in to comment.