UNPKG

2.81 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.unregisterGlobal = exports.getGlobal = exports.registerGlobal = void 0;
19var platform_1 = require("../platform");
20var version_1 = require("../version");
21var semver_1 = require("./semver");
22var major = version_1.VERSION.split('.')[0];
23var GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for("opentelemetry.js.api." + major);
24var _global = platform_1._globalThis;
25function registerGlobal(type, instance, diag, allowOverride) {
26 var _a;
27 if (allowOverride === void 0) { allowOverride = false; }
28 var api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a !== void 0 ? _a : {
29 version: version_1.VERSION,
30 });
31 if (!allowOverride && api[type]) {
32 // already registered an API of this type
33 var err = new Error("@opentelemetry/api: Attempted duplicate registration of API: " + type);
34 diag.error(err.stack || err.message);
35 return false;
36 }
37 if (api.version !== version_1.VERSION) {
38 // All registered APIs must be of the same version exactly
39 var err = new Error('@opentelemetry/api: All API registration versions must match');
40 diag.error(err.stack || err.message);
41 return false;
42 }
43 api[type] = instance;
44 diag.debug("@opentelemetry/api: Registered a global for " + type + " v" + version_1.VERSION + ".");
45 return true;
46}
47exports.registerGlobal = registerGlobal;
48function getGlobal(type) {
49 var _a, _b;
50 var globalVersion = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a === void 0 ? void 0 : _a.version;
51 if (!globalVersion || !semver_1.isCompatible(globalVersion)) {
52 return;
53 }
54 return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
55}
56exports.getGlobal = getGlobal;
57function unregisterGlobal(type, diag) {
58 diag.debug("@opentelemetry/api: Unregistering a global for " + type + " v" + version_1.VERSION + ".");
59 var api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
60 if (api) {
61 delete api[type];
62 }
63}
64exports.unregisterGlobal = unregisterGlobal;
65//# sourceMappingURL=global-utils.js.map
\No newline at end of file