@mollitia/prometheus
Version:
Prometheus Mollitia Addon
451 lines (450 loc) • 13.7 kB
JavaScript
var q = Object.defineProperty;
var j = (s, e, t) => e in s ? q(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var p = (s, e, t) => (j(s, typeof e != "symbol" ? e + "" : e, t), t);
import * as l from "mollitia";
var H = /* @__PURE__ */ ((s) => (s.COUNTER = "counter", s.GAUGE = "gauge", s))(H || {});
class f {
// Private attributes
// Constructor
constructor(e, t) {
// Public Attributes
p(this, "key");
p(this, "type");
p(this, "labels");
p(this, "values");
p(this, "description");
this.key = e, this.type = H.COUNTER, this.labels = (t == null ? void 0 : t.labels) || {}, this.values = {}, t != null && t.description && (this.description = t == null ? void 0 : t.description);
}
// Public Methods
inc(e = 1, t) {
return this.values[t] = this.values[t] || 0, this.values[t] += e, this.values[t];
}
reset(e) {
return this.values[e] = 0, this.values[e];
}
scrapHelp() {
let e = `# HELP ${this.key} ${this.description}
`;
return e += `# TYPE ${this.key} ${this.type}
`, e;
}
scrapValues() {
let e = "";
if (Object.keys(this.values).length)
for (const t in this.values) {
let r = `circuit="${t}"`;
for (const a in this.labels)
r += `, ${a}="${this.labels[a]}"`;
e += `${this.key}{${r}} ${this.values[t]}
`;
}
return e;
}
}
class E {
// Private attributes
// Constructor
constructor(e, t) {
// Public Attributes
p(this, "key");
p(this, "type");
p(this, "labels");
p(this, "values");
p(this, "description");
this.key = e, this.type = H.GAUGE, this.labels = (t == null ? void 0 : t.labels) || {}, this.values = {}, t != null && t.description && (this.description = t == null ? void 0 : t.description);
}
// Public Methods
inc(e = 1, t) {
return this.values[t] = this.values[t] || 0, this.values[t] += e, this.values[t];
}
get(e) {
return this.values[e];
}
set(e, t) {
return this.values[t] = e, this.values[t];
}
scrapHelp() {
let e = `# HELP ${this.key} ${this.description}
`;
return e += `# TYPE ${this.key} ${this.type}
`, e;
}
scrapValues() {
let e = "";
if (Object.keys(this.values).length)
for (const t in this.values) {
let r = `circuit="${t}"`;
for (const c in this.labels)
r += `, ${c}="${this.labels[c]}"`;
const a = this.values[t] % 1 ? this.values[t].toFixed(2) : this.values[t];
e += `${this.key}{${r}} ${a}
`;
}
return this.values = {}, e;
}
}
const C = (s) => {
if (s.constructor.name === l.Circuit.name) {
const e = s;
return e.prometheus.perMethod ? `${e.prometheus.name}_${e.prometheus.funcName}` : s.prometheus.name;
} else
return s.prometheus.name;
}, M = (s, e) => {
var w, x, d, $, b, v, y, m, u, T, g, O, D, S, F, N;
let t = (w = e.prometheus) == null ? void 0 : w.labels;
s.constructor.name !== l.Circuit.name && (t = { ...t, module: ((x = e.prometheus) == null ? void 0 : x.name) || "" });
const r = new f(
`${(d = e.prometheus) != null && d.prefix ? `${($ = e.prometheus) == null ? void 0 : $.prefix}_` : ""}total_executions`,
{
description: "Total Executions",
labels: t
}
), a = new f(
`${(b = e.prometheus) != null && b.prefix ? `${(v = e.prometheus) == null ? void 0 : v.prefix}_` : ""}total_success`,
{
description: "Total Success",
labels: t
}
), c = new f(
`${(y = e.prometheus) != null && y.prefix ? `${(m = e.prometheus) == null ? void 0 : m.prefix}_` : ""}total_failures`,
{
description: "Total Failures",
labels: t
}
), i = {}, n = new E(
`${(u = e.prometheus) != null && u.prefix ? `${(T = e.prometheus) == null ? void 0 : T.prefix}_` : ""}duration`,
{
description: "Maximum Duration of Circuit Execution",
labels: { ...t, metricType: "MAX" }
}
), o = new E(
`${(g = e.prometheus) != null && g.prefix ? `${(O = e.prometheus) == null ? void 0 : O.prefix}_` : ""}duration`,
{
description: "Average Duration of Circuit Execution",
labels: { ...t, metricType: "AVG" }
}
), _ = new E(
`${(D = e.prometheus) != null && D.prefix ? `${(S = e.prometheus) == null ? void 0 : S.prefix}_` : ""}duration`,
{
description: "Minimum Duration of Circuit Execution",
labels: { ...t, metricType: "MIN" }
}
), k = new E(
`${(F = e.prometheus) != null && F.prefix ? `${(N = e.prometheus) == null ? void 0 : N.prefix}_` : ""}duration`,
{
description: "Count of Circuit Execution",
labels: { ...t, metricType: "COUNT" }
}
);
return s.on("execute", (R, G) => {
const h = C(R);
i[h] = i[h] || 0;
const L = Date.now();
r.inc(1, h), G.then(() => {
const A = k.inc(1, h);
A === 1 && (i[h] = 0);
const B = Date.now() - L;
i[h] += B;
const P = _.get(h);
(!P || P > B) && _.set(B, h);
const U = n.get(h);
(!U || U < B) && n.set(B, h), o.set(i[h] / A, h);
}).catch(() => {
c.inc(1, h);
});
}), {
total_executions: r,
total_success: a,
total_failures: c,
duration_max: n,
duration_ave: o,
duration_min: _,
duration_count: k
};
}, I = (s, e) => M(s, e), Y = (s, e) => {
var c, i, n, o;
const t = M(s, e), r = { ...(c = e.prometheus) == null ? void 0 : c.labels, module: ((i = e.prometheus) == null ? void 0 : i.name) || "" }, a = new f(
`${(n = e.prometheus) != null && n.prefix ? `${(o = e.prometheus) == null ? void 0 : o.prefix}_` : ""}total_failures_timeout`,
{
description: "Total Timeout Failures",
labels: r
}
);
return s.on("timeout", () => {
a.inc(1, s.prometheus.name);
}), {
...t,
total_failures_timeout: a
};
}, X = (s, e) => {
var o, _, k, w, x, d, $, b, v, y;
const t = M(s, e), r = {
...(o = e.prometheus) == null ? void 0 : o.labels,
module: ((_ = e.prometheus) == null ? void 0 : _.name) || ""
}, a = new f(
`${(k = e.prometheus) != null && k.prefix ? `${(w = e.prometheus) == null ? void 0 : w.prefix}_` : ""}success_without_retries`,
{
description: "Success Without Retries",
labels: r
}
), c = new f(
`${(x = e.prometheus) != null && x.prefix ? `${(d = e.prometheus) == null ? void 0 : d.prefix}_` : ""}success_with_retries`,
{
description: "Success With Retries",
labels: r
}
), i = new f(
`${($ = e.prometheus) != null && $.prefix ? `${(b = e.prometheus) == null ? void 0 : b.prefix}_` : ""}failures_without_retries`,
{
description: "Failures Without Retries",
labels: r
}
), n = new f(
`${(v = e.prometheus) != null && v.prefix ? `${(y = e.prometheus) == null ? void 0 : y.prefix}_` : ""}failures_with_retries`,
{
description: "Failures With Retries",
labels: r
}
);
return s.on("success-without-retry", (m) => {
const u = C(m);
a.inc(1, u);
}), s.on("success-with-retry", (m) => {
const u = C(m);
c.inc(1, u);
}), s.on("failure-without-retry", (m) => {
const u = C(m);
i.inc(1, u);
}), s.on("failure-with-retry", (m) => {
const u = C(m);
n.inc(1, u);
}), {
...t,
success_without_retries: a,
success_with_retries: c,
failures_without_retries: i,
failures_with_retries: n
};
}, z = (s, e) => M(s, e), J = (s, e) => M(s, e), K = (s, e) => M(s, e), Q = (s, e) => M(s, e);
function Z(s) {
throw new Error(s);
}
const ee = (s, e) => {
var c, i, n, o, _, k, w;
const t = M(s, e), r = {
...(c = e.prometheus) == null ? void 0 : c.labels,
module: ((i = e.prometheus) == null ? void 0 : i.name) || ""
};
let a = { ...t };
if (s instanceof l.SlidingWindowBreaker) {
let x = function(m) {
const u = e.prometheus.name;
switch (d.reset(u), $.reset(u), b.reset(u), m) {
case l.BreakerState.CLOSED: {
d.inc(1, u);
break;
}
case l.BreakerState.HALF_OPENED: {
$.inc(1, u);
break;
}
case l.BreakerState.OPENED: {
b.inc(1, u);
break;
}
default:
Z(m);
}
};
const d = new f(
`${(n = e.prometheus) != null && n.prefix ? `${e.prometheus.prefix}_` : ""}breaker_state`,
{
description: "Breaker State",
labels: {
...r,
state: "closed"
}
}
), $ = new f(
`${(o = e.prometheus) != null && o.prefix ? `${e.prometheus.prefix}_` : ""}breaker_state`,
{
description: "Breaker State",
labels: {
...r,
state: "half-opened"
}
}
), b = new f(
`${(_ = e.prometheus) != null && _.prefix ? `${e.prometheus.prefix}_` : ""}breaker_state`,
{
description: "Breaker State",
labels: {
...r,
state: "opened"
}
}
), v = new E(
`${(k = e.prometheus) != null && k.prefix ? `${e.prometheus.prefix}_` : ""}failure_rate`,
{
description: "Failure Rate (%)",
labels: r
}
), y = new E(
`${(w = e.prometheus) != null && w.prefix ? `${e.prometheus.prefix}_` : ""}slow_rate`,
{
description: "Slow Rate (%)",
labels: r
}
);
x(s.state ?? l.BreakerState.CLOSED), s.on("state-changed", x), s.on("execute", async (m, u) => {
const T = C(s);
try {
await u;
} catch {
} finally {
const g = await s.getState(), O = g.requests.filter(({ result: R }) => R === l.SlidingWindowRequestResult.FAILURE).length, D = g.requests.filter(({ result: R }) => R === l.SlidingWindowRequestResult.TIMEOUT).length, S = g.requests.length, F = S > 0 ? O / S * 100 : 0, N = S > 0 ? D / S * 100 : 0;
v.set(F, T), y.set(N, T);
}
}), a = {
...t,
closed_breaker_state: d,
half_opened_breaker_state: $,
opened_breaker_state: b,
failure_rate: v,
slow_rate: y
};
}
return a;
}, se = "0.0.9", V = [], W = [], re = () => {
const s = {};
for (const e of V) {
const t = e.prometheus.name;
for (const r in e.prometheus.metrics)
s[r] ? s[r].circuits[t] = e.prometheus.metrics[r].values[t] : s[r] = {
circuits: {
[t]: e.prometheus.metrics[r].values[t] || 0
},
modules: {}
};
}
for (const e of W) {
const t = e.prometheus.name;
for (const r in e.prometheus.metrics)
Object.keys(e.prometheus.metrics[r].values).length && (s[r] ? s[r].modules[t] = e.prometheus.metrics[r].values : s[r] = {
circuits: {},
modules: {
[t]: e.prometheus.metrics[r].values
}
});
}
return s;
}, ae = () => {
const s = {};
for (const r of V)
for (const a in r.prometheus.metrics)
s[a] ? s[a].value += r.prometheus.metrics[a].scrapValues() : s[a] = {
key: r.prometheus.metrics[a].key,
help: r.prometheus.metrics[a].scrapHelp(),
value: r.prometheus.metrics[a].scrapValues()
};
for (const r of W)
for (const a in r.prometheus.metrics)
s[a] ? s[a].value += r.prometheus.metrics[a].scrapValues() : s[a] = {
key: r.prometheus.metrics[a].key,
help: r.prometheus.metrics[a].scrapHelp(),
value: r.prometheus.metrics[a].scrapValues()
};
let e = "";
const t = /* @__PURE__ */ new Set();
for (const r in s) {
const a = s[r].key;
t.has(a) || (e += s[r].help, t.add(a)), e += `${s[r].value}
`;
}
return e;
};
class ce {
// Lifecycle
onCircuitCreate(e, t) {
if (t.prometheus) {
e.prometheus = {
name: t.prometheus.name,
perMethod: t.prometheus.perMethod ? t.prometheus.perMethod : !1,
labels: t.prometheus.labels || {},
metrics: I(e, t),
scrap: () => {
let a = "";
const c = /* @__PURE__ */ new Set();
for (const i in e.prometheus.metrics) {
const n = e.prometheus.metrics[i].key;
c.has(n) || (a += e.prometheus.metrics[i].scrapHelp(), c.add(n)), a += e.prometheus.metrics[i].scrapValues();
}
return a;
}
};
const r = e.fn.bind(e);
e.fn = (a, c) => (e.prometheus.funcName = c || a.name, r(a, c)), V.push(e);
}
}
onModuleCreate(e, t) {
if (t.prometheus) {
let r;
switch (e.constructor.name) {
case l.Timeout.name: {
r = Y;
break;
}
case l.Retry.name: {
r = X;
break;
}
case l.Bulkhead.name: {
r = z;
break;
}
case l.Cache.name: {
r = J;
break;
}
case l.Ratelimit.name: {
r = K;
break;
}
case l.Fallback.name: {
r = Q;
break;
}
case l.SlidingWindowBreakerOptions.name:
case l.SlidingCountBreaker.name:
case l.SlidingTimeBreaker.name: {
r = ee;
break;
}
default:
r = () => ({});
}
e.prometheus = {
name: t.prometheus.name,
labels: t.prometheus.labels || {},
metrics: r(e, t),
scrap: () => {
let a = "";
const c = /* @__PURE__ */ new Set();
for (const i in e.prometheus.metrics) {
const n = e.prometheus.metrics[i].key;
c.has(n) || (a += e.prometheus.metrics[i].scrapHelp(), c.add(n)), a += e.prometheus.metrics[i].scrapValues();
}
return a;
}
}, W.push(e);
}
}
}
export {
ce as PrometheusAddon,
V as circuits,
re as metrics,
W as modules,
ae as scrap,
se as version
};