Skip to content

Commit

Permalink
tracing: allow the Resource to be set externally
Browse files Browse the repository at this point in the history
This is a workaround for the brittleness when constructing OTel
`Resource` objects. Internally, the OTel libraries do their own
detection which can be merged with one created in code. However,
the `semconv` spec versions must match. (NOT module version! the
`semconv` package has multiple subpackages for each spec version,
e.g. `semconv/v1.17`, `semconv/v1.21`, etc.)

This creates a problem when BuildKit is used as a library - the
importing app might be using a different, otherwise compatible
version of the OTel libraries, so when it creates a resource, it
will be merged with one of a different version.

By allowing the `Resource` to be set (like the `Recorder`), the
calling code can construct a resource using known consistent
library versions that work, and then allow BuildKit to take over
the rest of the initialization process for OTel.

Signed-off-by: Milas Bowman <[email protected]>
  • Loading branch information
milas committed Dec 8, 2023
1 parent 833949d commit 7b3fe03
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions util/tracing/detect/detect.go
Original file line number Diff line number Diff line change
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 7b3fe03

Please sign in to comment.