Skip to content

Commit

Permalink
Merge pull request #4472 from milas/v0.12-otel-resource
Browse files Browse the repository at this point in the history
tracing: allow the `Resource` to be set externally
  • Loading branch information
tonistiigi committed Dec 8, 2023
2 parents 833949d + 7b3fe03 commit 3b6880d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions util/tracing/detect/detect.go
Expand Up @@ -26,6 +26,8 @@ type detector struct {
var ServiceName string
var Recorder *TraceRecorder

var Resource *resource.Resource

var detectors map[string]detector
var once sync.Once
var tp trace.TracerProvider
Expand Down Expand Up @@ -97,13 +99,16 @@ func detect() error {
// enable log with traceID when valid exporter
bklog.EnableLogWithTraceID(true)

res, err := resource.Detect(context.Background(), serviceNameDetector{})
if err != nil {
return err
}
res, err = resource.Merge(resource.Default(), res)
if err != nil {
return err
if Resource == nil {
res, err := resource.Detect(context.Background(), serviceNameDetector{})
if err != nil {
return err
}
res, err = resource.Merge(resource.Default(), res)
if err != nil {
return err
}
Resource = res
}

sp := sdktrace.NewBatchSpanProcessor(exp)
Expand All @@ -112,7 +117,10 @@ func detect() error {
Recorder.flush = sp.ForceFlush
}

sdktp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sp), sdktrace.WithResource(res))
sdktp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sp),
sdktrace.WithResource(Resource),
)
closers = append(closers, sdktp.Shutdown)

exporter = exp
Expand Down

0 comments on commit 3b6880d

Please sign in to comment.