forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨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
- Loading branch information
1 parent
93db59e
commit 1f1188c
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
export * from './resources/Resource'; | ||
export * from './trace/NoopSpan'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |