UNPKG

2.82 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.NoopTracer = void 0;
19const context_1 = require("../api/context");
20const context_utils_1 = require("../trace/context-utils");
21const NonRecordingSpan_1 = require("./NonRecordingSpan");
22const spancontext_utils_1 = require("./spancontext-utils");
23const contextApi = context_1.ContextAPI.getInstance();
24/**
25 * No-op implementations of {@link Tracer}.
26 */
27class NoopTracer {
28 // startSpan starts a noop span.
29 startSpan(name, options, context = contextApi.active()) {
30 const root = Boolean(options === null || options === void 0 ? void 0 : options.root);
31 if (root) {
32 return new NonRecordingSpan_1.NonRecordingSpan();
33 }
34 const parentFromContext = context && (0, context_utils_1.getSpanContext)(context);
35 if (isSpanContext(parentFromContext) &&
36 (0, spancontext_utils_1.isSpanContextValid)(parentFromContext)) {
37 return new NonRecordingSpan_1.NonRecordingSpan(parentFromContext);
38 }
39 else {
40 return new NonRecordingSpan_1.NonRecordingSpan();
41 }
42 }
43 startActiveSpan(name, arg2, arg3, arg4) {
44 let opts;
45 let ctx;
46 let fn;
47 if (arguments.length < 2) {
48 return;
49 }
50 else if (arguments.length === 2) {
51 fn = arg2;
52 }
53 else if (arguments.length === 3) {
54 opts = arg2;
55 fn = arg3;
56 }
57 else {
58 opts = arg2;
59 ctx = arg3;
60 fn = arg4;
61 }
62 const parentContext = ctx !== null && ctx !== void 0 ? ctx : contextApi.active();
63 const span = this.startSpan(name, opts, parentContext);
64 const contextWithSpanSet = (0, context_utils_1.setSpan)(parentContext, span);
65 return contextApi.with(contextWithSpanSet, fn, undefined, span);
66 }
67}
68exports.NoopTracer = 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