-
Notifications
You must be signed in to change notification settings - Fork 145
Handle non-extending Span classes in Reference constructor #162
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 |
---|---|---|
@@ -1,6 +1,23 @@ | ||
import Span from './span'; | ||
import SpanContext from './span_context'; | ||
|
||
const getContext = (contextOrSpan: SpanContext | Span): SpanContext => { | ||
treble-snake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (contextOrSpan instanceof SpanContext) { | ||
return contextOrSpan; | ||
} | ||
Comment on lines
+5
to
+7
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. This doesn't seem necessary. 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. 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? |
||
|
||
// Second check is for cases when a Span implementation does not extend | ||
// opentracing.Span class directly (like Jaeger), just implements the same interface. | ||
// The only false-positive case here is a non-extending SpanContext class, | ||
// which has a method called "context". | ||
// But that's too much of a specification violation to take care of. | ||
if (contextOrSpan instanceof Span || 'context' in contextOrSpan) { | ||
return contextOrSpan.context(); | ||
} | ||
|
||
return contextOrSpan; | ||
}; | ||
|
||
/** | ||
* Reference pairs a reference type constant (e.g., REFERENCE_CHILD_OF or | ||
* REFERENCE_FOLLOWS_FROM) with the SpanContext it points to. | ||
|
@@ -39,9 +56,6 @@ export default class Reference { | |
*/ | ||
constructor(type: string, referencedContext: SpanContext | Span) { | ||
this._type = type; | ||
this._referencedContext = ( | ||
referencedContext instanceof Span ? | ||
referencedContext.context() : | ||
referencedContext); | ||
this._referencedContext = getContext(referencedContext); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Span from '../../span'; | ||
import SpanContext from '../../span_context'; | ||
|
||
/** | ||
* Span implementation for unit tests. Extends opentracing.Span. | ||
*/ | ||
export class ExtendingSpan extends Span { | ||
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. could simply use MockSpan |
||
|
||
constructor(private ctx: SpanContext) { | ||
super(); | ||
} | ||
|
||
context(): SpanContext { | ||
return this.ctx; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import SpanContext from '../../span_context'; | ||
|
||
export class ExtendingSpanContext extends SpanContext { | ||
|
||
context(): any { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as noop from '../../noop'; | ||
import SpanContext from '../../span_context'; | ||
import Tracer from '../../tracer'; | ||
|
||
/** | ||
* Span implementation for unit tests. Does not extend opentracing.Span, but | ||
* implements the interface | ||
*/ | ||
export class NonExtendingSpan { | ||
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. 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? 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. Thanks for the helpful suggestions, I got rid of the fixtures indeed :) |
||
|
||
constructor(private ctx: SpanContext) { | ||
} | ||
|
||
context(): SpanContext { | ||
return this.ctx; | ||
} | ||
|
||
tracer(): Tracer { | ||
return this._tracer(); | ||
} | ||
|
||
setOperationName(name: string): this { | ||
this._setOperationName(name); | ||
return this; | ||
} | ||
|
||
setBaggageItem(key: string, value: string): this { | ||
this._setBaggageItem(key, value); | ||
return this; | ||
} | ||
|
||
getBaggageItem(key: string): string | undefined { | ||
return this._getBaggageItem(key); | ||
} | ||
|
||
setTag(key: string, value: any): this { | ||
this._addTags({ [key]: value }); | ||
return this; | ||
} | ||
|
||
addTags(keyValueMap: { [key: string]: any }): this { | ||
this._addTags(keyValueMap); | ||
return this; | ||
} | ||
|
||
log(keyValuePairs: { [key: string]: any }, timestamp?: number): this { | ||
this._log(keyValuePairs, timestamp); | ||
return this; | ||
} | ||
|
||
logEvent(eventName: string, payload: any): void { | ||
return this._log({ event: eventName, payload }); | ||
} | ||
|
||
finish(finishTime?: number): void { | ||
this._finish(finishTime); | ||
} | ||
|
||
protected _context(): SpanContext { | ||
return noop.spanContext!; | ||
} | ||
|
||
protected _tracer(): Tracer { | ||
return noop.tracer!; | ||
} | ||
|
||
protected _setOperationName(name: string): void { | ||
} | ||
|
||
protected _setBaggageItem(key: string, value: string): void { | ||
} | ||
|
||
protected _getBaggageItem(key: string): string | undefined { | ||
return undefined; | ||
} | ||
|
||
protected _addTags(keyValuePairs: { [key: string]: any }): void { | ||
} | ||
|
||
protected _log(keyValuePairs: { [key: string]: any }, timestamp?: number): void { | ||
} | ||
|
||
protected _finish(finishTime?: number): void { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class NonExtendingSpanContext { | ||
|
||
toTraceId(): string { | ||
return ''; | ||
} | ||
|
||
toSpanId(): string { | ||
return ''; | ||
} | ||
} |
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.
Looks like these checks are made in the Reference constructor anyway, thus are duplicated here. So I removed them.