1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.unregisterGlobal = exports.getGlobal = exports.registerGlobal = void 0;
|
19 | const platform_1 = require("../platform");
|
20 | const version_1 = require("../version");
|
21 | const semver_1 = require("./semver");
|
22 | const major = version_1.VERSION.split('.')[0];
|
23 | const GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for(`opentelemetry.js.api.${major}`);
|
24 | const _global = platform_1._globalThis;
|
25 | function registerGlobal(type, instance, diag, allowOverride = false) {
|
26 | var _a;
|
27 | const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a !== void 0 ? _a : {
|
28 | version: version_1.VERSION,
|
29 | });
|
30 | if (!allowOverride && api[type]) {
|
31 |
|
32 | const err = new Error(`@opentelemetry/api: Attempted duplicate registration of API: ${type}`);
|
33 | diag.error(err.stack || err.message);
|
34 | return false;
|
35 | }
|
36 | if (api.version !== version_1.VERSION) {
|
37 |
|
38 | const err = new Error(`@opentelemetry/api: Registration of version v${api.version} for ${type} does not match previously registered API v${version_1.VERSION}`);
|
39 | diag.error(err.stack || err.message);
|
40 | return false;
|
41 | }
|
42 | api[type] = instance;
|
43 | diag.debug(`@opentelemetry/api: Registered a global for ${type} v${version_1.VERSION}.`);
|
44 | return true;
|
45 | }
|
46 | exports.registerGlobal = registerGlobal;
|
47 | function getGlobal(type) {
|
48 | var _a, _b;
|
49 | const globalVersion = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a === void 0 ? void 0 : _a.version;
|
50 | if (!globalVersion || !(0, semver_1.isCompatible)(globalVersion)) {
|
51 | return;
|
52 | }
|
53 | return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
|
54 | }
|
55 | exports.getGlobal = getGlobal;
|
56 | function unregisterGlobal(type, diag) {
|
57 | diag.debug(`@opentelemetry/api: Unregistering a global for ${type} v${version_1.VERSION}.`);
|
58 | const api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
|
59 | if (api) {
|
60 | delete api[type];
|
61 | }
|
62 | }
|
63 | exports.unregisterGlobal = unregisterGlobal;
|
64 |
|
\ | No newline at end of file |