Merge pull request #2351 from morlay/log-fix

bklog: only log tracing ids when span exporter not nil
master
Tõnis Tiigi 2021-09-08 19:03:02 -07:00 committed by GitHub
commit e07f3883e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -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

View File

@ -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