UNPKG

3.53 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.PropagationAPI = void 0;
19const global_utils_1 = require("../internal/global-utils");
20const NoopTextMapPropagator_1 = require("../propagation/NoopTextMapPropagator");
21const TextMapPropagator_1 = require("../propagation/TextMapPropagator");
22const context_helpers_1 = require("../baggage/context-helpers");
23const utils_1 = require("../baggage/utils");
24const diag_1 = require("./diag");
25const API_NAME = 'propagation';
26const NOOP_TEXT_MAP_PROPAGATOR = new NoopTextMapPropagator_1.NoopTextMapPropagator();
27/**
28 * Singleton object which represents the entry point to the OpenTelemetry Propagation API
29 */
30class PropagationAPI {
31 /** Empty private constructor prevents end users from constructing a new instance of the API */
32 constructor() {
33 this.createBaggage = utils_1.createBaggage;
34 this.getBaggage = context_helpers_1.getBaggage;
35 this.setBaggage = context_helpers_1.setBaggage;
36 this.deleteBaggage = context_helpers_1.deleteBaggage;
37 }
38 /** Get the singleton instance of the Propagator API */
39 static getInstance() {
40 if (!this._instance) {
41 this._instance = new PropagationAPI();
42 }
43 return this._instance;
44 }
45 /**
46 * Set the current propagator.
47 *
48 * @returns true if the propagator was successfully registered, else false
49 */
50 setGlobalPropagator(propagator) {
51 return (0, global_utils_1.registerGlobal)(API_NAME, propagator, diag_1.DiagAPI.instance());
52 }
53 /**
54 * Inject context into a carrier to be propagated inter-process
55 *
56 * @param context Context carrying tracing data to inject
57 * @param carrier carrier to inject context into
58 * @param setter Function used to set values on the carrier
59 */
60 inject(context, carrier, setter = TextMapPropagator_1.defaultTextMapSetter) {
61 return this._getGlobalPropagator().inject(context, carrier, setter);
62 }
63 /**
64 * Extract context from a carrier
65 *
66 * @param context Context which the newly created context will inherit from
67 * @param carrier Carrier to extract context from
68 * @param getter Function used to extract keys from a carrier
69 */
70 extract(context, carrier, getter = TextMapPropagator_1.defaultTextMapGetter) {
71 return this._getGlobalPropagator().extract(context, carrier, getter);
72 }
73 /**
74 * Return a list of all fields which may be used by the propagator.
75 */
76 fields() {
77 return this._getGlobalPropagator().fields();
78 }
79 /** Remove the global propagator */
80 disable() {
81 (0, global_utils_1.unregisterGlobal)(API_NAME, diag_1.DiagAPI.instance());
82 }
83 _getGlobalPropagator() {
84 return (0, global_utils_1.getGlobal)(API_NAME) || NOOP_TEXT_MAP_PROPAGATOR;
85 }
86}
87exports.PropagationAPI = PropagationAPI;
88//# sourceMappingURL=propagation.js.map
\No newline at end of file