UNPKG

3.11 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.TraceAPI = void 0;
19const global_utils_1 = require("../internal/global-utils");
20const ProxyTracerProvider_1 = require("../trace/ProxyTracerProvider");
21const spancontext_utils_1 = require("../trace/spancontext-utils");
22const context_utils_1 = require("../trace/context-utils");
23const diag_1 = require("./diag");
24const API_NAME = 'trace';
25/**
26 * Singleton object which represents the entry point to the OpenTelemetry Tracing API
27 */
28class TraceAPI {
29 /** Empty private constructor prevents end users from constructing a new instance of the API */
30 constructor() {
31 this._proxyTracerProvider = new ProxyTracerProvider_1.ProxyTracerProvider();
32 this.wrapSpanContext = spancontext_utils_1.wrapSpanContext;
33 this.isSpanContextValid = spancontext_utils_1.isSpanContextValid;
34 this.deleteSpan = context_utils_1.deleteSpan;
35 this.getSpan = context_utils_1.getSpan;
36 this.getActiveSpan = context_utils_1.getActiveSpan;
37 this.getSpanContext = context_utils_1.getSpanContext;
38 this.setSpan = context_utils_1.setSpan;
39 this.setSpanContext = context_utils_1.setSpanContext;
40 }
41 /** Get the singleton instance of the Trace API */
42 static getInstance() {
43 if (!this._instance) {
44 this._instance = new TraceAPI();
45 }
46 return this._instance;
47 }
48 /**
49 * Set the current global tracer.
50 *
51 * @returns true if the tracer provider was successfully registered, else false
52 */
53 setGlobalTracerProvider(provider) {
54 const success = (0, global_utils_1.registerGlobal)(API_NAME, this._proxyTracerProvider, diag_1.DiagAPI.instance());
55 if (success) {
56 this._proxyTracerProvider.setDelegate(provider);
57 }
58 return success;
59 }
60 /**
61 * Returns the global tracer provider.
62 */
63 getTracerProvider() {
64 return (0, global_utils_1.getGlobal)(API_NAME) || this._proxyTracerProvider;
65 }
66 /**
67 * Returns a tracer from the global tracer provider.
68 */
69 getTracer(name, version) {
70 return this.getTracerProvider().getTracer(name, version);
71 }
72 /** Remove the global tracer provider */
73 disable() {
74 (0, global_utils_1.unregisterGlobal)(API_NAME, diag_1.DiagAPI.instance());
75 this._proxyTracerProvider = new ProxyTracerProvider_1.ProxyTracerProvider();
76 }
77}
78exports.TraceAPI = TraceAPI;
79//# sourceMappingURL=trace.js.map
\No newline at end of file