Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

all: always check if opentracing.SpanFromContext is nil#42027

Merged
bobheadxi merged 1 commit into
mainfrom
opentracing-span-from-context-nil
Sep 26, 2022
Merged

all: always check if opentracing.SpanFromContext is nil#42027
bobheadxi merged 1 commit into
mainfrom
opentracing-span-from-context-nil

Conversation

@bobheadxi

Copy link
Copy Markdown
Member

policy.ShouldTrace does not guarantee that span has been started within a context, just that one should be created if anyone asks. opentracing.SpanFromContext documents that it can return nil if no span is started, which may cause a panic when used (https://github.com/sourcegraph/sourcegraph/pull/42003). To mitigate this, this we update the docstring of policy.ShouldTrace and make sure usages of opentracing.SpanFromContext always check for the nil-ness of the span. We do this even if a trace is started right before, just in case.

The OpenTelemetry equivalent, trace.SpanFromContext, returns a zero-value no-op span instead (though there are no usages of this function yet).

Test plan

tests pass still

@sourcegraph-bot

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 4ba015f...5e90b3f.

Notify File(s)
@beyang internal/search/zoekt/zoekt.go
@camdencheek internal/search/zoekt/zoekt.go
@keegancsmith cmd/frontend/graphqlbackend/search_results.go
internal/search/zoekt/zoekt.go
internal/trace/policy/policy.go
@rvantonder cmd/frontend/graphqlbackend/search_results.go
@sourcegraph/dev-experience internal/trace/policy/policy.go

@keegancsmith

Copy link
Copy Markdown
Member

TBH it feels like policy.ShouldTrace should only be used by the parts of our code which start a trace. Otherwise we should be relying on nilness of span to mean no tracing.

@camdencheek camdencheek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How do we feel about making SpanFromContext always return a valid value? In the case when there is no active span, its methods would just be no-ops. That way consumers of the tracing API don't need to care whether or not tracing is enabled. We'd only care if tracing is active when initializing a trace, probably at the API layer.

@bobheadxi

bobheadxi commented Sep 26, 2022

Copy link
Copy Markdown
Member Author

Otherwise we should be relying on nilness of span to mean no tracing.

In OpenTelemetry, I believe you can continue on as if a span is always returned - OpenTelemetry will provide a no-op span that can be used with no effect.

How do we feel about making SpanFromContext always return a valid value? In the case when there is no active span, its methods would just be no-ops. That way consumers of the tracing API don't need to care whether or not tracing is enabled. We'd only care if tracing is active when initializing a trace, probably at the API layer.

We can't in this case, because SpanFromContext is from a deprecated third-party library (the OpenTracing Go implementation). The OpenTelemetry library, however, does exactly this (returns a valid non-recording span). We could do a fork, but I'd rather see this migrated to OpenTelemetry instead (I opted not to in this PR to minimize the changes)

@bobheadxi bobheadxi merged commit fa42ad2 into main Sep 26, 2022
@bobheadxi bobheadxi deleted the opentracing-span-from-context-nil branch September 26, 2022 16:48
camdencheek pushed a commit that referenced this pull request Sep 26, 2022
policy.ShouldTrace does not guarantee that span has been started within a context, just that one should be created if anyone asks. opentracing.SpanFromContext documents that it can return nil if no span is started, which may cause a panic when used. To mitigate this, this we update the docstring of policy.ShouldTrace and make sure usages of opentracing.SpanFromContext always check for the nil-ness of the span. We do this even if a trace is started right before, just in case.

The OpenTelemetry equivalent, trace.SpanFromContext, returns a zero-value no-op span instead (though there are no usages of this function yet).
sashaostrikov pushed a commit that referenced this pull request Sep 29, 2022
policy.ShouldTrace does not guarantee that span has been started within a context, just that one should be created if anyone asks. opentracing.SpanFromContext documents that it can return nil if no span is started, which may cause a panic when used. To mitigate this, this we update the docstring of policy.ShouldTrace and make sure usages of opentracing.SpanFromContext always check for the nil-ness of the span. We do this even if a trace is started right before, just in case.

The OpenTelemetry equivalent, trace.SpanFromContext, returns a zero-value no-op span instead (though there are no usages of this function yet).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants