Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Handle non-extending Span classes in Reference constructor #162

Conversation

treble-snake
Copy link
Contributor

The interface allows passing both Span and SpanContext as an argument
for creating References.
But the instanceof check won't always work in runtime, for example, for cases
when Span implementation does not extend the opentracing.Span class, but just
implements the interface.
The popular jaeger-client-node library does exactly that.

This produces hard-to-track errors.

I added additional checks to distinguish between Span and SpanContext instances.

Resolves: #161

The interface allows passing both Span and SpanContext as an argument
for creating References.
But the `instanceof` check won't always work in runtime, for example, for cases
when Span implementation does not extend the opentracing.Span class, but just
implements the interface.
The popular `jaeger-client-node` library does exactly that.

This produces hard-to-track errors.

I added additional checks to distinguish between Span and SpanContext instances.

Resolves: opentracing#161
// Allow the user to pass a Span instead of a SpanContext
if (spanContext instanceof Span) {
spanContext = spanContext.context();
}
Copy link
Contributor Author

@treble-snake treble-snake Mar 27, 2021

Choose a reason for hiding this comment

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

Looks like these checks are made in the Reference constructor anyway, thus are duplicated here. So I removed them.

/**
* Span implementation for unit tests. Extends opentracing.Span.
*/
export class ExtendingSpan extends Span {
Copy link
Member

Choose a reason for hiding this comment

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

could simply use MockSpan

* Span implementation for unit tests. Does not extend opentracing.Span, but
* implements the interface
*/
export class NonExtendingSpan {
Copy link
Member

Choose a reason for hiding this comment

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

this seems a bit of an overkill to test one check, it could've been done with just an anonymous dict. Why maintain all this boilerplate that doesn't add anything useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the helpful suggestions, I got rid of the fixtures indeed :)

Remove unnecessary fixtures, use simpler approach.
Comment on lines +5 to +7
if (contextOrSpan instanceof SpanContext) {
return contextOrSpan;
}
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This covers a rare, but possible case when a class, extending SpanContext, has its own "context" method for some reason. Do you think it's not worth covering?

src/reference.ts Outdated Show resolved Hide resolved
Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
@treble-snake treble-snake force-pushed the handle-non-extending-spans-in-references branch from 178bac9 to 6222bc6 Compare March 29, 2021 06:33
@yurishkuro yurishkuro merged commit 1380d0f into opentracing:master Mar 29, 2021
@yurishkuro
Copy link
Member

Thanks

@treble-snake
Copy link
Contributor Author

It's been a pleasure :)

@yurishkuro
Copy link
Member

@treble-snake it's possible this change introduced a bug, see #172

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type checks won't always work in Reference creating functions
2 participants