basant307/AI_Governance_Project
048
1"use strict";2/*3 * Copyright The OpenTelemetry Authors4 *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 at8 *9 * https://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17Object.defineProperty(exports, "__esModule", { value: true });18exports.ZipkinExporter = void 0;19const api_1 = require("@opentelemetry/api");20const core_1 = require("@opentelemetry/core");21const index_1 = require("./platform/index");22const transform_1 = require("./transform");23const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");24const utils_1 = require("./utils");25/**26 * Zipkin Exporter27 */28class ZipkinExporter {29 DEFAULT_SERVICE_NAME = 'OpenTelemetry Service';30 _statusCodeTagName;31 _statusDescriptionTagName;32 _urlStr;33 _send;34 _getHeaders;35 _serviceName;36 _isShutdown;37 _sendingPromises = [];38 constructor(config = {}) {39 this._urlStr =40 config.url ||41 ((0, core_1.getStringFromEnv)('OTEL_EXPORTER_ZIPKIN_ENDPOINT') ??42 'http://localhost:9411/api/v2/spans');43 this._send = (0, index_1.prepareSend)(this._urlStr, config.headers);44 this._serviceName = config.serviceName;45 this._statusCodeTagName =46 config.statusCodeTagName || transform_1.defaultStatusCodeTagName;47 this._statusDescriptionTagName =48 config.statusDescriptionTagName || transform_1.defaultStatusErrorTagName;49 this._isShutdown = false;50 if (typeof config.getExportRequestHeaders === 'function') {51 this._getHeaders = (0, utils_1.prepareGetHeaders)(config.getExportRequestHeaders);52 }53 else {54 // noop55 this._beforeSend = function () { };56 }57 }58 /**59 * Export spans.60 */61 export(spans, resultCallback) {62 const serviceName = String(this._serviceName ||63 spans[0].resource.attributes[semantic_conventions_1.ATTR_SERVICE_NAME] ||64 this.DEFAULT_SERVICE_NAME);65 api_1.diag.debug('Zipkin exporter export');66 if (this._isShutdown) {67 setTimeout(() => resultCallback({68 code: core_1.ExportResultCode.FAILED,69 error: new Error('Exporter has been shutdown'),70 }));71 return;72 }73 const promise = new Promise(resolve => {74 this._sendSpans(spans, serviceName, result => {75 resolve();76 resultCallback(result);77 });78 });79 this._sendingPromises.push(promise);80 const popPromise = () => {81 const index = this._sendingPromises.indexOf(promise);82 this._sendingPromises.splice(index, 1);83 };84 promise.then(popPromise, popPromise);85 }86 /**87 * Shutdown exporter. Noop operation in this exporter.88 */89 shutdown() {90 api_1.diag.debug('Zipkin exporter shutdown');91 this._isShutdown = true;92 return this.forceFlush();93 }94 /**95 * Exports any pending spans in exporter96 */97 forceFlush() {98 return new Promise((resolve, reject) => {99 Promise.all(this._sendingPromises).then(() => {100 resolve();101 }, reject);102 });103 }104 /**105 * if user defines getExportRequestHeaders in config then this will be called106 * every time before send, otherwise it will be replaced with noop in107 * constructor108 * @default noop109 */110 _beforeSend() {111 if (this._getHeaders) {112 this._send = (0, index_1.prepareSend)(this._urlStr, this._getHeaders());113 }114 }115 /**116 * Transform spans and sends to Zipkin service.117 */118 _sendSpans(spans, serviceName, done) {119 const zipkinSpans = spans.map(span => (0, transform_1.toZipkinSpan)(span, String(span.attributes[semantic_conventions_1.ATTR_SERVICE_NAME] ||120 span.resource.attributes[semantic_conventions_1.ATTR_SERVICE_NAME] ||121 serviceName), this._statusCodeTagName, this._statusDescriptionTagName));122 this._beforeSend();123 return this._send(zipkinSpans, (result) => {124 if (done) {125 return done(result);126 }127 });128 }129}130exports.ZipkinExporter = ZipkinExporter;131//# sourceMappingURL=zipkin.js.map