@embrace-io/web-sdk
Version:
1 lines • 4.01 kB
Source Map (JSON)
{"version":3,"file":"RetryingTransport.cjs","names":["OTelPerformanceManager","INITIAL_BACKOFF","MAX_BACKOFF","BACKOFF_MULTIPLIER"],"sources":["../../../src/transport/RetryingTransport/RetryingTransport.ts"],"sourcesContent":["import type {\n ExportResponse,\n IExporterTransport,\n} from '@opentelemetry/otlp-exporter-base';\nimport type { PerformanceManager } from '../../utils/index.ts';\nimport { OTelPerformanceManager } from '../../utils/index.ts';\nimport {\n BACKOFF_MULTIPLIER,\n INITIAL_BACKOFF,\n JITTER,\n MAX_ATTEMPTS,\n MAX_BACKOFF,\n} from './constants.ts';\n\n/**\n * Get a pseudo-random jitter that falls in the range of [-JITTER, +JITTER]\n */\nconst getJitter = () => Math.random() * (2 * JITTER) - JITTER;\n\n// Taken directly from open-telemetry/opentelemetry-js/experimental/packages/otlp-exporter-base/src/retrying-transport.ts\n// File is not exposed externally\n\n/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nexport class RetryingTransport implements IExporterTransport {\n public constructor(\n private readonly _transport: IExporterTransport,\n private readonly _perf: PerformanceManager = new OTelPerformanceManager(),\n ) {}\n\n public async send(\n data: Uint8Array,\n timeoutMillis: number,\n ): Promise<ExportResponse> {\n const deadline = this._perf.getNowMillis() + timeoutMillis;\n let result = await this._transport.send(data, timeoutMillis);\n let attempts = MAX_ATTEMPTS;\n let nextBackoff = INITIAL_BACKOFF;\n\n while (result.status === 'retryable' && attempts > 0) {\n attempts--;\n\n // use maximum of computed backoff and 0 to avoid negative timeouts\n const backoff = Math.max(\n Math.min(nextBackoff, MAX_BACKOFF) + getJitter(),\n 0,\n );\n nextBackoff = nextBackoff * BACKOFF_MULTIPLIER;\n const retryInMillis = result.retryInMillis ?? backoff;\n\n // return when expected retry time is after the export deadline.\n const remainingTimeoutMillis = deadline - this._perf.getNowMillis();\n if (retryInMillis > remainingTimeoutMillis) {\n return result;\n }\n\n result = await this._retry(data, remainingTimeoutMillis, retryInMillis);\n }\n\n return result;\n }\n\n public shutdown() {\n this._transport.shutdown();\n }\n\n private _retry(\n data: Uint8Array,\n timeoutMillis: number,\n inMillis: number,\n ): Promise<ExportResponse> {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n this._transport.send(data, timeoutMillis).then(resolve, reject);\n }, inMillis);\n });\n }\n}\n"],"mappings":";;;;;;;AAiBA,MAAM,kBAAkB,KAAK,OAAO,IAAK,KAAA;AAoBzC,IAAa,oBAAb,MAA6D;CAExC;CACA;CAFnB,YACE,YACA,QAA6C,IAAIA,wDAAAA,uBAAuB,GACxE;EAFiB,KAAA,aAAA;EACA,KAAA,QAAA;CAChB;CAEH,MAAa,KACX,MACA,eACyB;EACzB,MAAM,WAAW,KAAK,MAAM,aAAa,IAAI;EAC7C,IAAI,SAAS,MAAM,KAAK,WAAW,KAAK,MAAM,aAAa;EAC3D,IAAI,WAAA;EACJ,IAAI,cAAcC,8CAAAA;EAElB,OAAO,OAAO,WAAW,eAAe,WAAW,GAAG;GACpD;GAGA,MAAM,UAAU,KAAK,IACnB,KAAK,IAAI,aAAaC,8CAAAA,WAAW,IAAI,UAAU,GAC/C,CACF;GACA,cAAc,cAAcC,8CAAAA;GAC5B,MAAM,gBAAgB,OAAO,iBAAiB;GAG9C,MAAM,yBAAyB,WAAW,KAAK,MAAM,aAAa;GAClE,IAAI,gBAAgB,wBAClB,OAAO;GAGT,SAAS,MAAM,KAAK,OAAO,MAAM,wBAAwB,aAAa;EACxE;EAEA,OAAO;CACT;CAEA,WAAkB;EAChB,KAAK,WAAW,SAAS;CAC3B;CAEA,OACE,MACA,eACA,UACyB;EACzB,OAAO,IAAI,SAAS,SAAS,WAAW;GACtC,iBAAiB;IACf,KAAK,WAAW,KAAK,MAAM,aAAa,CAAC,CAAC,KAAK,SAAS,MAAM;GAChE,GAAG,QAAQ;EACb,CAAC;CACH;AACF"}