Skip to content

Commit

Permalink
✨Add NoopSpan: no-op implementation of Span (open-telemetry#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mayurkale22 authored Jul 7, 2019
1 parent 93db59e commit 1f1188c
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './resources/Resource';
export * from './trace/NoopSpan';
70 changes: 70 additions & 0 deletions packages/opentelemetry-core/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
63 changes: 63 additions & 0 deletions packages/opentelemetry-core/test/trace/NoopSpan.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 1f1188c

Please sign in to comment.