UNPKG

@vtex/api

Version:
94 lines (93 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOtelInstruments = exports.RequestsMetricLabels = void 0; const constants_1 = require("../../constants"); const client_1 = require("./client"); exports.RequestsMetricLabels = { ACCOUNT_NAME: constants_1.AttributeKeys.VTEX_ACCOUNT_NAME, REQUEST_HANDLER: 'handler', STATUS_CODE: 'status_code', }; const createOtelConcurrentRequestsInstrument = async () => { const metricsClient = await (0, client_1.getMetricClient)(); return metricsClient.createGauge('io_http_requests_current', { description: 'The current number of requests in course.', unit: '1', }); }; const createOtelRequestsTimingsInstrument = async () => { const metricsClient = await (0, client_1.getMetricClient)(); return metricsClient.createHistogram('runtime_http_requests_duration_milliseconds', { description: 'The incoming http requests total duration.', unit: 'ms', }); }; const createOtelTotalRequestsInstrument = async () => { const metricsClient = await (0, client_1.getMetricClient)(); return metricsClient.createCounter('runtime_http_requests_total', { description: 'The total number of HTTP requests.', unit: '1', }); }; const createOtelRequestsResponseSizesInstrument = async () => { const metricsClient = await (0, client_1.getMetricClient)(); return metricsClient.createHistogram('runtime_http_response_size_bytes', { description: 'The outgoing response sizes (only applicable when the response isn\'t a stream).', unit: 'bytes', }); }; const createOtelTotalAbortedRequestsInstrument = async () => { const metricsClient = await (0, client_1.getMetricClient)(); return metricsClient.createCounter('runtime_http_aborted_requests_total', { description: 'The total number of HTTP requests aborted.', unit: '1', }); }; class OtelInstrumentsSingleton { static getInstance() { if (!OtelInstrumentsSingleton.instance) { OtelInstrumentsSingleton.instance = new OtelInstrumentsSingleton(); } return OtelInstrumentsSingleton.instance; } constructor() { } async getInstruments() { if (this.instruments) { return this.instruments; } if (this.initializingPromise) { return this.initializingPromise; } this.initializingPromise = this.initializeInstruments(); try { this.instruments = await this.initializingPromise; return this.instruments; } catch (error) { console.error('Failed to initialize OTel instruments:', error); this.initializingPromise = undefined; throw error; } finally { this.initializingPromise = undefined; } } async initializeInstruments() { const [concurrentRequests, requestTimings, totalRequests, responseSizes, abortedRequests,] = await Promise.all([ createOtelConcurrentRequestsInstrument(), createOtelRequestsTimingsInstrument(), createOtelTotalRequestsInstrument(), createOtelRequestsResponseSizesInstrument(), createOtelTotalAbortedRequestsInstrument(), ]); return { abortedRequests, concurrentRequests, requestTimings, responseSizes, totalRequests, }; } } const getOtelInstruments = () => OtelInstrumentsSingleton.getInstance().getInstruments(); exports.getOtelInstruments = getOtelInstruments;