@gasbuddy/configured-prometheus-client
Version:
A configuration driven prometheus client
86 lines (68 loc) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
class PromiseTimer {
constructor(metric, labels) {
this.metric = metric;
this.labels = labels;
}
label(objectOrFunction) {
if (typeof objectOrFunction === 'function') {
this.postTimerLabels = this.postTimerLabels || [];
this.postTimerLabels.push(objectOrFunction);
} else {
this.labels = Object.assign(this.labels || {}, objectOrFunction);
}
return this;
}
labelError(objectOrFunction) {
return this.label(e => {
if (e) {
return typeof objectOrFunction === 'function' ? objectOrFunction(e) : objectOrFunction;
}
return null;
});
}
labelSuccess(objectOrFunction) {
return this.label((e, r) => {
if (!e) {
return typeof objectOrFunction === 'function' ? objectOrFunction(r) : objectOrFunction;
}
return null;
});
}
async execute(promise) {
const p = Promise.resolve(promise);
if (!this.metric) {
// This allows promiseTimer to work even when metrics are not being collected
return p;
}
const end = this.metric.startTimer(this.labels);
let result;
let error;
try {
result = await p;
return result;
} catch (rejection) {
error = rejection;
throw error;
} finally {
let endLabels;
try {
if (this.postTimerLabels) {
for (const l of this.postTimerLabels) {
const newLabels = l(error, result);
if (newLabels) {
endLabels = Object.assign(endLabels || {}, newLabels);
}
}
}
} catch (ignoredError) {} // Nothing to do - the label assignment failed
// End the timer with any new label information
end(endLabels);
}
}
}
exports.default = PromiseTimer;