From 1f1188c22aadcebd2092826cfb892b8aad8bd39e Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Sat, 6 Jul 2019 21:05:50 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Add=20NoopSpan:=20no-op=20implementati?= =?UTF-8?q?on=20of=20Span=20(#45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add DefaultSpan: no-op implementation of Span * Implement setAttributes method based on pull/44 * Remove protected methods * Rename DefaultSpan -> BaseSpan, return non-null SpanContext, Add TODOs * Rename BaseSpan -> NoopSpan * NoopSpan: return a real SpanContext for propagation --- packages/opentelemetry-core/src/index.ts | 1 + .../opentelemetry-core/src/trace/NoopSpan.ts | 70 +++++++++++++++++++ .../test/trace/NoopSpan.test.ts | 63 +++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 packages/opentelemetry-core/src/trace/NoopSpan.ts create mode 100644 packages/opentelemetry-core/test/trace/NoopSpan.test.ts diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index ec4b4d63350..b49d41bddb6 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -15,3 +15,4 @@ */ export * from './resources/Resource'; +export * from './trace/NoopSpan'; diff --git a/packages/opentelemetry-core/src/trace/NoopSpan.ts b/packages/opentelemetry-core/src/trace/NoopSpan.ts new file mode 100644 index 00000000000..2b9ef043340 --- /dev/null +++ b/packages/opentelemetry-core/src/trace/NoopSpan.ts @@ -0,0 +1,70 @@ +/** + * Copyright 2019, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as types from '@opentelemetry/types'; +import { SpanContext } from '@opentelemetry/types'; + +/** + * The NoopSpan is the default {@link Span} that is used when no Span + * implementation is available. All operations are no-op except context + * propagation. + */ +export class NoopSpan implements types.Span { + constructor(private readonly _spanContext: SpanContext) {} + + // Returns a SpanContext. + context(): types.SpanContext { + return this._spanContext; + } + + // By default does nothing + setAttribute(key: string, value: unknown): this { + return this; + } + + // By default does nothing + setAttributes(attributes: types.Attributes): this { + return this; + } + + // By default does nothing + addEvent(name: string, attributes?: types.Attributes): this { + return this; + } + + // By default does nothing + addLink(spanContext: types.SpanContext, attributes?: types.Attributes): this { + return this; + } + + // By default does nothing + setStatus(status: types.Status): this { + return this; + } + + // By default does nothing + updateName(name: string): this { + return this; + } + + // By default does nothing + end(endTime?: number): void {} + + // isRecordingEvents always returns false for noopSpan. + isRecordingEvents(): boolean { + return false; + } +} diff --git a/packages/opentelemetry-core/test/trace/NoopSpan.test.ts b/packages/opentelemetry-core/test/trace/NoopSpan.test.ts new file mode 100644 index 00000000000..1528b8d145d --- /dev/null +++ b/packages/opentelemetry-core/test/trace/NoopSpan.test.ts @@ -0,0 +1,63 @@ +/** + * Copyright 2019, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import { NoopSpan } from '../../src/trace/NoopSpan'; +import { CanonicalCode, TraceOptions } from '@opentelemetry/types'; + +describe('NoopSpan', () => { + it('do not crash', () => { + const spanContext = { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceOptions: TraceOptions.UNSAMPLED, + }; + + const span = new NoopSpan(spanContext); + span.setAttribute('my_string_attribute', 'foo'); + span.setAttribute('my_number_attribute', 123); + span.setAttribute('my_boolean_attribute', false); + span.setAttribute('my_obj_attribute', { a: true }); + span.setAttribute('my_sym_attribute', Symbol('a')); + span.setAttributes({ + my_string_attribute: 'foo', + my_number_attribute: 123, + }); + + span.addEvent('sent'); + span.addEvent('sent', { id: '42', key: 'value' }); + + span.addLink({ + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + }); + span.addLink( + { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + }, + { id: '42', key: 'value' } + ); + + span.setStatus({ code: CanonicalCode.CANCELLED }); + + span.updateName('my-span'); + + assert.ok(!span.isRecordingEvents()); + assert.deepStrictEqual(span.context(), spanContext); + span.end(); + }); +});