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()) L = logrus.NewEntry(logrus.StandardLogger())
) )
var (
logWithTraceID = false
)
func EnableLogWithTraceID(b bool) {
logWithTraceID = b
}
type ( type (
loggerKey struct{} loggerKey struct{}
) )
@ -33,14 +41,14 @@ func GetLogger(ctx context.Context) (l *logrus.Entry) {
l = L l = L
} }
spanContext := trace.SpanFromContext(ctx).SpanContext() if logWithTraceID {
if spanContext := trace.SpanFromContext(ctx).SpanContext(); spanContext.IsValid() {
if spanContext.IsValid() {
return l.WithFields(logrus.Fields{ return l.WithFields(logrus.Fields{
"traceID": spanContext.TraceID(), "traceID": spanContext.TraceID(),
"spanID": spanContext.SpanID(), "spanID": spanContext.SpanID(),
}) })
} }
}
return l return l
} }

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/moby/buildkit/util/bklog"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
@ -82,6 +83,10 @@ func detect() error {
if exp == nil { if exp == nil {
return nil return nil
} }
// enable log with traceID when valid exporter
bklog.EnableLogWithTraceID(true)
res, err := resource.Detect(context.Background(), serviceNameDetector{}) res, err := resource.Detect(context.Background(), serviceNameDetector{})
if err != nil { if err != nil {
return err return err