UNPKG

2.58 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.SimpleSpanProcessor = void 0;
19const api_1 = require("@opentelemetry/api");
20const core_1 = require("@opentelemetry/core");
21/**
22 * An implementation of the {@link SpanProcessor} that converts the {@link Span}
23 * to {@link ReadableSpan} and passes it to the configured exporter.
24 *
25 * Only spans that are sampled are converted.
26 */
27class SimpleSpanProcessor {
28 constructor(_exporter) {
29 this._exporter = _exporter;
30 this._isShutdown = false;
31 this._shuttingDownPromise = Promise.resolve();
32 }
33 forceFlush() {
34 // do nothing as all spans are being exported without waiting
35 return Promise.resolve();
36 }
37 // does nothing.
38 onStart(_span) { }
39 onEnd(span) {
40 if (this._isShutdown) {
41 return;
42 }
43 // prevent downstream exporter calls from generating spans
44 api_1.context.with(core_1.suppressTracing(api_1.context.active()), () => {
45 this._exporter.export([span], result => {
46 var _a;
47 if (result.code !== core_1.ExportResultCode.SUCCESS) {
48 core_1.globalErrorHandler((_a = result.error) !== null && _a !== void 0 ? _a : new Error(`SimpleSpanProcessor: span export failed (status ${result})`));
49 }
50 });
51 });
52 }
53 shutdown() {
54 if (this._isShutdown) {
55 return this._shuttingDownPromise;
56 }
57 this._isShutdown = true;
58 this._shuttingDownPromise = new Promise((resolve, reject) => {
59 Promise.resolve()
60 .then(() => {
61 return this._exporter.shutdown();
62 })
63 .then(resolve)
64 .catch(e => {
65 reject(e);
66 });
67 });
68 return this._shuttingDownPromise;
69 }
70}
71exports.SimpleSpanProcessor = SimpleSpanProcessor;
72//# sourceMappingURL=SimpleSpanProcessor.js.map
\No newline at end of file