-
Notifications
You must be signed in to change notification settings - Fork 244
feat: support to inject tracerProvider #1202
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ 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 | ||
|
|
@@ -34,11 +36,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, | ||
| } | ||
|
|
||
|
|
@@ -47,6 +46,12 @@ func NewOTelObservabilityService(opts ...OTelObservabilityServiceOption) *OTelOb | |
| opt(o) | ||
| } | ||
|
|
||
| o.tracer = o.traceProvider.Tracer( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| instrumentationName, | ||
| // TODO: Can we have the package version here? | ||
| // trace.WithInstrumentationVersion("1.0.0"), | ||
| ) | ||
|
|
||
| return o | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not let them set it to 'nil' if they want?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent, panic. 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() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this blank line? If anything I would expect 'traceProvider' and 'tracer' to be grouped and the blank line after 'tracer'. But I'm not sure we need a line break with only 4 fields