Skip to content

Commit 1f1188c

Browse files
authored
✨Add NoopSpan: no-op implementation of Span (open-telemetry#45)
* 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
1 parent 93db59e commit 1f1188c

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

packages/opentelemetry-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
*/
1616

1717
export * from './resources/Resource';
18+
export * from './trace/NoopSpan';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as types from '@opentelemetry/types';
18+
import { SpanContext } from '@opentelemetry/types';
19+
20+
/**
21+
* The NoopSpan is the default {@link Span} that is used when no Span
22+
* implementation is available. All operations are no-op except context
23+
* propagation.
24+
*/
25+
export class NoopSpan implements types.Span {
26+
constructor(private readonly _spanContext: SpanContext) {}
27+
28+
// Returns a SpanContext.
29+
context(): types.SpanContext {
30+
return this._spanContext;
31+
}
32+
33+
// By default does nothing
34+
setAttribute(key: string, value: unknown): this {
35+
return this;
36+
}
37+
38+
// By default does nothing
39+
setAttributes(attributes: types.Attributes): this {
40+
return this;
41+
}
42+
43+
// By default does nothing
44+
addEvent(name: string, attributes?: types.Attributes): this {
45+
return this;
46+
}
47+
48+
// By default does nothing
49+
addLink(spanContext: types.SpanContext, attributes?: types.Attributes): this {
50+
return this;
51+
}
52+
53+
// By default does nothing
54+
setStatus(status: types.Status): this {
55+
return this;
56+
}
57+
58+
// By default does nothing
59+
updateName(name: string): this {
60+
return this;
61+
}
62+
63+
// By default does nothing
64+
end(endTime?: number): void {}
65+
66+
// isRecordingEvents always returns false for noopSpan.
67+
isRecordingEvents(): boolean {
68+
return false;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { NoopSpan } from '../../src/trace/NoopSpan';
19+
import { CanonicalCode, TraceOptions } from '@opentelemetry/types';
20+
21+
describe('NoopSpan', () => {
22+
it('do not crash', () => {
23+
const spanContext = {
24+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
25+
spanId: '6e0c63257de34c92',
26+
traceOptions: TraceOptions.UNSAMPLED,
27+
};
28+
29+
const span = new NoopSpan(spanContext);
30+
span.setAttribute('my_string_attribute', 'foo');
31+
span.setAttribute('my_number_attribute', 123);
32+
span.setAttribute('my_boolean_attribute', false);
33+
span.setAttribute('my_obj_attribute', { a: true });
34+
span.setAttribute('my_sym_attribute', Symbol('a'));
35+
span.setAttributes({
36+
my_string_attribute: 'foo',
37+
my_number_attribute: 123,
38+
});
39+
40+
span.addEvent('sent');
41+
span.addEvent('sent', { id: '42', key: 'value' });
42+
43+
span.addLink({
44+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
45+
spanId: '6e0c63257de34c92',
46+
});
47+
span.addLink(
48+
{
49+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
50+
spanId: '6e0c63257de34c92',
51+
},
52+
{ id: '42', key: 'value' }
53+
);
54+
55+
span.setStatus({ code: CanonicalCode.CANCELLED });
56+
57+
span.updateName('my-span');
58+
59+
assert.ok(!span.isRecordingEvents());
60+
assert.deepStrictEqual(span.context(), spanContext);
61+
span.end();
62+
});
63+
});

0 commit comments

Comments
 (0)