Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[forward port to master] tracing: allow the Resource to be set externally #4496

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions util/tracing/detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 @@ -112,13 +114,16 @@ func detect() error {
return err
}

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
}

// enable log with traceID when valid exporter
Expand All @@ -131,7 +136,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.SpanExporter = texp
Expand All @@ -156,7 +164,7 @@ func detect() error {

if len(readers) > 0 {
opts := make([]sdkmetric.Option, 0, len(readers)+1)
opts = append(opts, sdkmetric.WithResource(res))
opts = append(opts, sdkmetric.WithResource(Resource))
for _, r := range readers {
opts = append(opts, sdkmetric.WithReader(r))
}
Expand Down
Loading