UNPKG

2.59 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.getSpanContext = exports.setSpanContext = exports.deleteSpan = exports.setSpan = exports.getActiveSpan = exports.getSpan = void 0;
19const context_1 = require("../context/context");
20const NonRecordingSpan_1 = require("./NonRecordingSpan");
21const context_2 = require("../api/context");
22/**
23 * span key
24 */
25const SPAN_KEY = (0, context_1.createContextKey)('OpenTelemetry Context Key SPAN');
26/**
27 * Return the span if one exists
28 *
29 * @param context context to get span from
30 */
31function getSpan(context) {
32 return context.getValue(SPAN_KEY) || undefined;
33}
34exports.getSpan = getSpan;
35/**
36 * Gets the span from the current context, if one exists.
37 */
38function getActiveSpan() {
39 return getSpan(context_2.ContextAPI.getInstance().active());
40}
41exports.getActiveSpan = getActiveSpan;
42/**
43 * Set the span on a context
44 *
45 * @param context context to use as parent
46 * @param span span to set active
47 */
48function setSpan(context, span) {
49 return context.setValue(SPAN_KEY, span);
50}
51exports.setSpan = setSpan;
52/**
53 * Remove current span stored in the context
54 *
55 * @param context context to delete span from
56 */
57function deleteSpan(context) {
58 return context.deleteValue(SPAN_KEY);
59}
60exports.deleteSpan = deleteSpan;
61/**
62 * Wrap span context in a NoopSpan and set as span in a new
63 * context
64 *
65 * @param context context to set active span on
66 * @param spanContext span context to be wrapped
67 */
68function setSpanContext(context, spanContext) {
69 return setSpan(context, new NonRecordingSpan_1.NonRecordingSpan(spanContext));
70}
71exports.setSpanContext = setSpanContext;
72/**
73 * Get the span context of the span if it exists.
74 *
75 * @param context context to get values from
76 */
77function getSpanContext(context) {
78 var _a;
79 return (_a = getSpan(context)) === null || _a === void 0 ? void 0 : _a.spanContext();
80}
81exports.getSpanContext = getSpanContext;
82//# sourceMappingURL=context-utils.js.map
\No newline at end of file