UNPKG

2.69 kBJavaScriptView Raw
1/*
2 * Copyright The 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 */
16import { ContextAPI } from '../api/context';
17import { getSpanContext, setSpan } from '../trace/context-utils';
18import { NonRecordingSpan } from './NonRecordingSpan';
19import { isSpanContextValid } from './spancontext-utils';
20var context = ContextAPI.getInstance();
21/**
22 * No-op implementations of {@link Tracer}.
23 */
24var NoopTracer = /** @class */ (function () {
25 function NoopTracer() {
26 }
27 // startSpan starts a noop span.
28 NoopTracer.prototype.startSpan = function (name, options, context) {
29 var root = Boolean(options === null || options === void 0 ? void 0 : options.root);
30 if (root) {
31 return new NonRecordingSpan();
32 }
33 var parentFromContext = context && getSpanContext(context);
34 if (isSpanContext(parentFromContext) &&
35 isSpanContextValid(parentFromContext)) {
36 return new NonRecordingSpan(parentFromContext);
37 }
38 else {
39 return new NonRecordingSpan();
40 }
41 };
42 NoopTracer.prototype.startActiveSpan = function (name, arg2, arg3, arg4) {
43 var opts;
44 var ctx;
45 var fn;
46 if (arguments.length < 2) {
47 return;
48 }
49 else if (arguments.length === 2) {
50 fn = arg2;
51 }
52 else if (arguments.length === 3) {
53 opts = arg2;
54 fn = arg3;
55 }
56 else {
57 opts = arg2;
58 ctx = arg3;
59 fn = arg4;
60 }
61 var parentContext = ctx !== null && ctx !== void 0 ? ctx : context.active();
62 var span = this.startSpan(name, opts, parentContext);
63 var contextWithSpanSet = setSpan(parentContext, span);
64 return context.with(contextWithSpanSet, fn, undefined, span);
65 };
66 return NoopTracer;
67}());
68export { NoopTracer };
69function isSpanContext(spanContext) {
70 return (typeof spanContext === 'object' &&
71 typeof spanContext['spanId'] === 'string' &&
72 typeof spanContext['traceId'] === 'string' &&
73 typeof spanContext['traceFlags'] === 'number');
74}
75//# sourceMappingURL=NoopTracer.js.map
\No newline at end of file