1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { trace, context, propagation, diag, } from '@opentelemetry/api';
|
17 | import { CompositePropagator, HttpBaggagePropagator, HttpTraceContextPropagator, getEnv, } from '@opentelemetry/core';
|
18 | import { Resource } from '@opentelemetry/resources';
|
19 | import { Tracer } from '.';
|
20 | import { DEFAULT_CONFIG } from './config';
|
21 | import { MultiSpanProcessor } from './MultiSpanProcessor';
|
22 | import { NoopSpanProcessor } from './export/NoopSpanProcessor';
|
23 |
|
24 | var merge = require('lodash.merge');
|
25 | import { BatchSpanProcessor } from './platform';
|
26 | export var ForceFlushState;
|
27 | (function (ForceFlushState) {
|
28 | ForceFlushState[ForceFlushState["resolved"] = 0] = "resolved";
|
29 | ForceFlushState[ForceFlushState["timeout"] = 1] = "timeout";
|
30 | ForceFlushState[ForceFlushState["error"] = 2] = "error";
|
31 | ForceFlushState[ForceFlushState["unresolved"] = 3] = "unresolved";
|
32 | })(ForceFlushState || (ForceFlushState = {}));
|
33 |
|
34 |
|
35 |
|
36 | var BasicTracerProvider = (function () {
|
37 | function BasicTracerProvider(config) {
|
38 | if (config === void 0) { config = {}; }
|
39 | var _a;
|
40 | this._registeredSpanProcessors = [];
|
41 | this._tracers = new Map();
|
42 | var mergedConfig = merge({}, DEFAULT_CONFIG, config);
|
43 | this.resource = (_a = mergedConfig.resource) !== null && _a !== void 0 ? _a : Resource.empty();
|
44 | this.resource = Resource.default().merge(this.resource);
|
45 | this._config = Object.assign({}, mergedConfig, {
|
46 | resource: this.resource,
|
47 | });
|
48 | var defaultExporter = this._buildExporterFromEnv();
|
49 | if (defaultExporter !== undefined) {
|
50 | var batchProcessor = new BatchSpanProcessor(defaultExporter);
|
51 | this.activeSpanProcessor = batchProcessor;
|
52 | }
|
53 | else {
|
54 | this.activeSpanProcessor = new NoopSpanProcessor();
|
55 | }
|
56 | }
|
57 | BasicTracerProvider.prototype.getTracer = function (name, version) {
|
58 | var key = name + "@" + (version || '');
|
59 | if (!this._tracers.has(key)) {
|
60 | this._tracers.set(key, new Tracer({ name: name, version: version }, this._config, this));
|
61 | }
|
62 | return this._tracers.get(key);
|
63 | };
|
64 | |
65 |
|
66 |
|
67 |
|
68 | BasicTracerProvider.prototype.addSpanProcessor = function (spanProcessor) {
|
69 | if (this._registeredSpanProcessors.length === 0) {
|
70 |
|
71 |
|
72 | this.activeSpanProcessor
|
73 | .shutdown()
|
74 | .catch(function (err) {
|
75 | return diag.error('Error while trying to shutdown current span processor', err);
|
76 | });
|
77 | }
|
78 | this._registeredSpanProcessors.push(spanProcessor);
|
79 | this.activeSpanProcessor = new MultiSpanProcessor(this._registeredSpanProcessors);
|
80 | };
|
81 | BasicTracerProvider.prototype.getActiveSpanProcessor = function () {
|
82 | return this.activeSpanProcessor;
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | BasicTracerProvider.prototype.register = function (config) {
|
92 | if (config === void 0) { config = {}; }
|
93 | trace.setGlobalTracerProvider(this);
|
94 | if (config.propagator === undefined) {
|
95 | config.propagator = this._buildPropagatorFromEnv();
|
96 | }
|
97 | if (config.contextManager) {
|
98 | context.setGlobalContextManager(config.contextManager);
|
99 | }
|
100 | if (config.propagator) {
|
101 | propagation.setGlobalPropagator(config.propagator);
|
102 | }
|
103 | };
|
104 | BasicTracerProvider.prototype.forceFlush = function () {
|
105 | var timeout = this._config.forceFlushTimeoutMillis;
|
106 | var promises = this._registeredSpanProcessors.map(function (spanProcessor) {
|
107 | return new Promise(function (resolve) {
|
108 | var state;
|
109 | var timeoutInterval = setTimeout(function () {
|
110 | resolve(new Error("Span processor did not completed within timeout period of " + timeout + " ms"));
|
111 | state = ForceFlushState.timeout;
|
112 | }, timeout);
|
113 | spanProcessor
|
114 | .forceFlush()
|
115 | .then(function () {
|
116 | clearTimeout(timeoutInterval);
|
117 | if (state !== ForceFlushState.timeout) {
|
118 | state = ForceFlushState.resolved;
|
119 | resolve(state);
|
120 | }
|
121 | })
|
122 | .catch(function (error) {
|
123 | clearTimeout(timeoutInterval);
|
124 | state = ForceFlushState.error;
|
125 | resolve(error);
|
126 | });
|
127 | });
|
128 | });
|
129 | return new Promise(function (resolve, reject) {
|
130 | Promise.all(promises)
|
131 | .then(function (results) {
|
132 | var errors = results.filter(function (result) { return result !== ForceFlushState.resolved; });
|
133 | if (errors.length > 0) {
|
134 | reject(errors);
|
135 | }
|
136 | else {
|
137 | resolve();
|
138 | }
|
139 | })
|
140 | .catch(function (error) { return reject([error]); });
|
141 | });
|
142 | };
|
143 | BasicTracerProvider.prototype.shutdown = function () {
|
144 | return this.activeSpanProcessor.shutdown();
|
145 | };
|
146 | BasicTracerProvider.prototype._getPropagator = function (name) {
|
147 | var _a;
|
148 | return (_a = BasicTracerProvider._registeredPropagators.get(name)) === null || _a === void 0 ? void 0 : _a();
|
149 | };
|
150 | BasicTracerProvider.prototype._getSpanExporter = function (name) {
|
151 | var _a;
|
152 | return (_a = BasicTracerProvider._registeredExporters.get(name)) === null || _a === void 0 ? void 0 : _a();
|
153 | };
|
154 | BasicTracerProvider.prototype._buildPropagatorFromEnv = function () {
|
155 | var _this = this;
|
156 |
|
157 | var uniquePropagatorNames = Array.from(new Set(getEnv().OTEL_PROPAGATORS));
|
158 | var propagators = uniquePropagatorNames.map(function (name) {
|
159 | var propagator = _this._getPropagator(name);
|
160 | if (!propagator) {
|
161 | diag.warn("Propagator \"" + name + "\" requested through environment variable is unavailable.");
|
162 | }
|
163 | return propagator;
|
164 | });
|
165 | var validPropagators = propagators.reduce(function (list, item) {
|
166 | if (item) {
|
167 | list.push(item);
|
168 | }
|
169 | return list;
|
170 | }, []);
|
171 | if (validPropagators.length === 0) {
|
172 | return;
|
173 | }
|
174 | else if (uniquePropagatorNames.length === 1) {
|
175 | return validPropagators[0];
|
176 | }
|
177 | else {
|
178 | return new CompositePropagator({
|
179 | propagators: validPropagators,
|
180 | });
|
181 | }
|
182 | };
|
183 | BasicTracerProvider.prototype._buildExporterFromEnv = function () {
|
184 | var exporterName = getEnv().OTEL_TRACES_EXPORTER;
|
185 | if (exporterName === 'none')
|
186 | return;
|
187 | var exporter = this._getSpanExporter(exporterName);
|
188 | if (!exporter) {
|
189 | diag.error("Exporter \"" + exporterName + "\" requested through environment variable is unavailable.");
|
190 | }
|
191 | return exporter;
|
192 | };
|
193 | BasicTracerProvider._registeredPropagators = new Map([
|
194 | ['tracecontext', function () { return new HttpTraceContextPropagator(); }],
|
195 | ['baggage', function () { return new HttpBaggagePropagator(); }],
|
196 | ]);
|
197 | BasicTracerProvider._registeredExporters = new Map();
|
198 | return BasicTracerProvider;
|
199 | }());
|
200 | export { BasicTracerProvider };
|
201 |
|
\ | No newline at end of file |