diff --git a/packages/opentelemetry-api/src/trace/NoopSpan.ts b/packages/opentelemetry-api/src/trace/NoopSpan.ts index cfa3c9c4c08..00a97066af7 100644 --- a/packages/opentelemetry-api/src/trace/NoopSpan.ts +++ b/packages/opentelemetry-api/src/trace/NoopSpan.ts @@ -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(); diff --git a/packages/opentelemetry-api/src/trace/span.ts b/packages/opentelemetry-api/src/trace/span.ts index 2a725afdc2a..5182e56221e 100644 --- a/packages/opentelemetry-api/src/trace/span.ts +++ b/packages/opentelemetry-api/src/trace/span.ts @@ -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; } diff --git a/packages/opentelemetry-exporter-collector/test/helper.ts b/packages/opentelemetry-exporter-collector/test/helper.ts index 46071b7e7fe..c77bdcd16f6 100644 --- a/packages/opentelemetry-exporter-collector/test/helper.ts +++ b/packages/opentelemetry-exporter-collector/test/helper.ts @@ -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: [ diff --git a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts index 20ef8467c8b..c487a138c2e 100644 --- a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts +++ b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts @@ -117,6 +117,7 @@ describe('JaegerExporter', () => { spanContext, startTime: [1566156729, 709], endTime: [1566156731, 709], + ended: true, status: { code: types.CanonicalCode.DATA_LOSS, }, diff --git a/packages/opentelemetry-exporter-jaeger/test/transform.test.ts b/packages/opentelemetry-exporter-jaeger/test/transform.test.ts index 4e81dd724bf..0f558a5e512 100644 --- a/packages/opentelemetry-exporter-jaeger/test/transform.test.ts +++ b/packages/opentelemetry-exporter-jaeger/test/transform.test.ts @@ -35,6 +35,7 @@ describe('transform', () => { spanContext, startTime: [1566156729, 709], endTime: [1566156731, 709], + ended: true, status: { code: types.CanonicalCode.OK, }, @@ -131,6 +132,7 @@ describe('transform', () => { spanContext, startTime: [1566156729, 709], endTime: [1566156731, 709], + ended: true, status: { code: types.CanonicalCode.DATA_LOSS, message: 'data loss', @@ -187,6 +189,7 @@ describe('transform', () => { spanContext, startTime: [1566156729, 709], endTime: [1566156731, 709], + ended: true, status: { code: types.CanonicalCode.OK, }, diff --git a/packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts index b4b0a5177a0..5c3010a4e3f 100644 --- a/packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts @@ -38,6 +38,7 @@ function getReadableSpan() { }, startTime: [startTime, 0], endTime: [startTime + duration, 0], + ended: true, duration: [duration, 0], status: { code: types.CanonicalCode.OK, @@ -141,6 +142,7 @@ describe('ZipkinExporter', () => { }, startTime: [startTime, 0], endTime: [startTime + duration, 0], + ended: true, duration: [duration, 0], status: { code: types.CanonicalCode.OK, @@ -167,6 +169,7 @@ describe('ZipkinExporter', () => { }, startTime: [startTime, 0], endTime: [startTime + duration, 0], + ended: true, duration: [duration, 0], status: { code: types.CanonicalCode.OK, diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index 0ae73d8f93a..902fd8f2472 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -172,6 +172,10 @@ export class Span implements types.Span, ReadableSpan { return true; } + isEnded(): boolean { + return this._ended; + } + toReadableSpan(): ReadableSpan { return this; } @@ -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( diff --git a/packages/opentelemetry-tracing/src/export/ReadableSpan.ts b/packages/opentelemetry-tracing/src/export/ReadableSpan.ts index 039752268de..3d461d285dc 100644 --- a/packages/opentelemetry-tracing/src/export/ReadableSpan.ts +++ b/packages/opentelemetry-tracing/src/export/ReadableSpan.ts @@ -36,4 +36,5 @@ export interface ReadableSpan { readonly links: Link[]; readonly events: TimedEvent[]; readonly duration: HrTime; + readonly ended: boolean; } diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index 7c56dc5e713..47391598e1e 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -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); + }); });