Skip to content

Commit

Permalink
feat: introduce ended property on Span
Browse files Browse the repository at this point in the history
Add a getter to Span and ReadableSpan to allow checking if the span is
ended without using a private field or rely on internals like
span.duration[0] < 0.

Refs: open-telemetry/opentelemetry-java#693
Refs: open-telemetry/opentelemetry-java#697
  • Loading branch information
Flarna committed Feb 4, 2020
1 parent de4c19e commit caeb0bc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/opentelemetry-api/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ export class NoopSpan implements Span {
}

// By default does nothing
end(endTime?: TimeInput): void {}
end(endTime?: TimeInput): void {
this._ended = true;
}

// isRecording always returns false for noopSpan.
isRecording(): boolean {
return false;
}

isEnded(): boolean {
return this._ended;
}

private _ended: boolean = false;
}

export const NOOP_SPAN = new NoopSpan();
7 changes: 7 additions & 0 deletions packages/opentelemetry-api/src/trace/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ export interface Span {
* with the AddEvent operation and attributes using setAttributes.
*/
isRecording(): boolean;

/**
* Returns if the span has been ended.
*
* @returns true if the Span has been ended.
*/
isEnded(): boolean;
}
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const mockedReadableSpan: ReadableSpan = {
parentSpanId: '78a8915098864388',
startTime: [1574120165, 429803070],
endTime: [1574120165, 438688070],
ended: true,
status: { code: 0 },
attributes: { component: 'document-load' },
links: [
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ describe('JaegerExporter', () => {
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.DATA_LOSS,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('transform', () => {
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.OK,
},
Expand Down Expand Up @@ -131,6 +132,7 @@ describe('transform', () => {
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.DATA_LOSS,
message: 'data loss',
Expand Down Expand Up @@ -187,6 +189,7 @@ describe('transform', () => {
spanContext,
startTime: [1566156729, 709],
endTime: [1566156731, 709],
ended: true,
status: {
code: types.CanonicalCode.OK,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function getReadableSpan() {
},
startTime: [startTime, 0],
endTime: [startTime + duration, 0],
ended: true,
duration: [duration, 0],
status: {
code: types.CanonicalCode.OK,
Expand Down Expand Up @@ -141,6 +142,7 @@ describe('ZipkinExporter', () => {
},
startTime: [startTime, 0],
endTime: [startTime + duration, 0],
ended: true,
duration: [duration, 0],
status: {
code: types.CanonicalCode.OK,
Expand All @@ -167,6 +169,7 @@ describe('ZipkinExporter', () => {
},
startTime: [startTime, 0],
endTime: [startTime + duration, 0],
ended: true,
duration: [duration, 0],
status: {
code: types.CanonicalCode.OK,
Expand Down
8 changes: 8 additions & 0 deletions packages/opentelemetry-tracing/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export class Span implements types.Span, ReadableSpan {
return true;
}

isEnded(): boolean {
return this._ended;
}

toReadableSpan(): ReadableSpan {
return this;
}
Expand All @@ -180,6 +184,10 @@ export class Span implements types.Span, ReadableSpan {
return this._duration;
}

get ended(): boolean {
return this._ended;
}

private _isSpanEnded(): boolean {
if (this._ended) {
this._logger.warn(
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-tracing/src/export/ReadableSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export interface ReadableSpan {
readonly links: Link[];
readonly events: TimedEvent[];
readonly duration: HrTime;
readonly ended: boolean;
}
7 changes: 7 additions & 0 deletions packages/opentelemetry-tracing/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,11 @@ describe('Span', () => {
span.updateName('bar-span');
assert.strictEqual(span.name, 'foo-span');
});

it('should have ended', () => {
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
assert.strictEqual(span.ended, false);
span.end();
assert.strictEqual(span.ended, true);
});
});

0 comments on commit caeb0bc

Please sign in to comment.