Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

// OTelObservabilityService implements the ObservabilityService interface from cloudevents
type OTelObservabilityService struct {
traceProvider trace.TracerProvider
tracer trace.Tracer
spanAttributesGetter func(cloudevents.Event) []attribute.KeyValue
spanNameFormatter func(cloudevents.Event) string
Expand All @@ -34,11 +35,8 @@ func NewOTelObservabilityService(opts ...OTelObservabilityServiceOption) *OTelOb
tracerProvider := otel.GetTracerProvider()

o := &OTelObservabilityService{
tracer: tracerProvider.Tracer(
instrumentationName,
// TODO: Can we have the package version here?
// trace.WithInstrumentationVersion("1.0.0"),
),
traceProvider: tracerProvider,
tracer: nil,
spanNameFormatter: defaultSpanNameFormatter,
}

Expand All @@ -47,6 +45,12 @@ func NewOTelObservabilityService(opts ...OTelObservabilityServiceOption) *OTelOb
opt(o)
}

o.tracer = o.traceProvider.Tracer(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better/safer to check o.traceProvider for 'nil' and either set tracer to nil or call o.traceProvider.Tracer ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know oTel, but if you allow/check for nil then you can decide if 'nil' for traceProvider means 'nil' for 'tracer too, or to default traceProvider to otel.GetTracerProvider()

Copy link
Copy Markdown
Author

@minuk-dev minuk-dev Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the below rule. I think it's very useful way to set a guard.

open-telemetry/opentelemetry-go-contrib#1104

ref. open-telemetry/opentelemetry-go-contrib#1107

instrumentationName,
// TODO: Can we have the package version here?
// trace.WithInstrumentationVersion("1.0.0"),
)

return o
}

Expand Down
10 changes: 10 additions & 0 deletions observability/opentelemetry/v2/client/otel_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package client

import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/observability"
Expand Down Expand Up @@ -37,6 +38,15 @@ func WithSpanNameFormatter(nameFormatter func(cloudevents.Event) string) OTelObs
}
}

// WithTracerProvider sets the tracer provider to use for creating spans.
func WithTracerProvider(tracerProvider trace.TracerProvider) OTelObservabilityServiceOption {
return func(os *OTelObservabilityService) {
if tracerProvider != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not let them set it to 'nil' if they want?

Copy link
Copy Markdown
Author

@minuk-dev minuk-dev Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent, panic.
If someone want to disable trace, we can use noop.TracerProvider.

https://github.com/open-telemetry/opentelemetry-go/blob/trace/v1.38.0/trace/noop/noop.go#L36

I think it's the same context with #1202 (comment)

os.traceProvider = tracerProvider
}
}
}

var defaultSpanNameFormatter func(cloudevents.Event) string = func(e cloudevents.Event) string {
return observability.ClientSpanName + "." + e.Context.GetType()
}