@vtex/api
Version:
VTEX I/O API client
47 lines (46 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpAgentSingleton = void 0;
const agents_1 = require("../../agents");
class HttpAgentSingleton {
static getHttpAgent() {
if (!HttpAgentSingleton.httpAgent) {
HttpAgentSingleton.httpAgent = (0, agents_1.createHttpAgent)();
}
return HttpAgentSingleton.httpAgent;
}
static httpAgentStats() {
const sockets = HttpAgentSingleton.count(HttpAgentSingleton.httpAgent.sockets);
const freeSockets = HttpAgentSingleton.count(HttpAgentSingleton.httpAgent.freeSockets);
const pendingRequests = HttpAgentSingleton.count(HttpAgentSingleton.httpAgent.requests);
return {
freeSockets,
pendingRequests,
sockets,
};
}
/**
* Update HTTP agent statistics as diagnostics metrics (gauges).
* This method should be called periodically to export current HTTP agent state.
*/
static updateHttpAgentMetrics() {
if (!global.diagnosticsMetrics) {
console.warn('DiagnosticsMetrics not available. HTTP agent metrics not reported.');
return;
}
const stats = HttpAgentSingleton.httpAgentStats();
// Report HTTP agent stats as gauges (current state)
global.diagnosticsMetrics.setGauge('http_agent_sockets_current', stats.sockets, {});
global.diagnosticsMetrics.setGauge('http_agent_free_sockets_current', stats.freeSockets, {});
global.diagnosticsMetrics.setGauge('http_agent_pending_requests_current', stats.pendingRequests, {});
}
static count(obj) {
try {
return Object.values(obj).reduce((acc, val) => acc += val.length, 0);
}
catch (_) {
return 0;
}
}
}
exports.HttpAgentSingleton = HttpAgentSingleton;