@fabricorg/experiments-metrics 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/http.cjs +34 -0
- package/dist/http.cjs.map +1 -0
- package/dist/http.d.cts +22 -0
- package/dist/http.d.ts +22 -0
- package/dist/http.js +32 -0
- package/dist/http.js.map +1 -0
- package/dist/index.cjs +201 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +82 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.js +195 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fabric
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/http.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/http.ts
|
|
4
|
+
function httpMetricsMiddleware(collector) {
|
|
5
|
+
const requestsTotal = collector.registry.get("http_requests_total") ?? collector.counter("http_requests_total", "Total HTTP requests");
|
|
6
|
+
const requestDuration = collector.registry.get("http_request_duration_ms") ?? collector.histogram("http_request_duration_ms", "HTTP request duration in milliseconds");
|
|
7
|
+
return async (req, res, next) => {
|
|
8
|
+
const start = Date.now();
|
|
9
|
+
try {
|
|
10
|
+
await next();
|
|
11
|
+
} finally {
|
|
12
|
+
const duration = Date.now() - start;
|
|
13
|
+
const path = extractPath(req.url);
|
|
14
|
+
const labels = {
|
|
15
|
+
method: req.method,
|
|
16
|
+
status: String(res.statusCode),
|
|
17
|
+
path
|
|
18
|
+
};
|
|
19
|
+
requestsTotal.inc(labels);
|
|
20
|
+
requestDuration.observe(labels, duration);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function extractPath(url) {
|
|
25
|
+
try {
|
|
26
|
+
return new URL(url, "http://localhost").pathname;
|
|
27
|
+
} catch {
|
|
28
|
+
return url.split("?")[0] ?? "/";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.httpMetricsMiddleware = httpMetricsMiddleware;
|
|
33
|
+
//# sourceMappingURL=http.cjs.map
|
|
34
|
+
//# sourceMappingURL=http.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/http.ts"],"names":[],"mappings":";;;AAsBO,SAAS,sBAAsB,SAAA,EAA6B;AACjE,EAAA,MAAM,aAAA,GACH,UAAU,QAAA,CAAS,GAAA,CAAI,qBAAqB,CAAA,IAC7C,SAAA,CAAU,OAAA,CAAQ,qBAAA,EAAuB,qBAAqB,CAAA;AAChE,EAAA,MAAM,eAAA,GACH,UAAU,QAAA,CAAS,GAAA,CAAI,0BAA0B,CAAA,IAClD,SAAA,CAAU,SAAA,CAAU,0BAAA,EAA4B,uCAAuC,CAAA;AAEzF,EAAA,OAAO,OACL,GAAA,EACA,GAAA,EACA,IAAA,KACkB;AAClB,IAAA,MAAM,KAAA,GAAQ,KAAK,GAAA,EAAI;AACvB,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,EAAK;AAAA,IACb,CAAA,SAAE;AACA,MAAA,MAAM,QAAA,GAAW,IAAA,CAAK,GAAA,EAAI,GAAI,KAAA;AAC9B,MAAA,MAAM,IAAA,GAAO,WAAA,CAAY,GAAA,CAAI,GAAG,CAAA;AAChC,MAAA,MAAM,MAAA,GAAS;AAAA,QACb,QAAQ,GAAA,CAAI,MAAA;AAAA,QACZ,MAAA,EAAQ,MAAA,CAAO,GAAA,CAAI,UAAU,CAAA;AAAA,QAC7B;AAAA,OACF;AACA,MAAA,aAAA,CAAc,IAAI,MAAM,CAAA;AACxB,MAAA,eAAA,CAAgB,OAAA,CAAQ,QAAQ,QAAQ,CAAA;AAAA,IAC1C;AAAA,EACF,CAAA;AACF;AAEA,SAAS,YAAY,GAAA,EAAqB;AACxC,EAAA,IAAI;AACF,IAAA,OAAO,IAAI,GAAA,CAAI,GAAA,EAAK,kBAAkB,CAAA,CAAE,QAAA;AAAA,EAC1C,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,CAAE,CAAC,CAAA,IAAK,GAAA;AAAA,EAC9B;AACF","file":"http.cjs","sourcesContent":["import type { Counter, Histogram, MetricsCollector } from './index.js';\n\nexport interface MiddlewareRequest {\n method: string;\n url: string;\n}\n\nexport interface MiddlewareResponse {\n statusCode: number;\n}\n\nexport type NextFunction = () => Promise<void> | void;\n\n/**\n * HTTP metrics middleware.\n *\n * Records:\n * - `http_requests_total` (counter) — method, status, path\n * - `http_request_duration_ms` (histogram) — method, status, path\n *\n * The `path` label is derived from `req.url` using the pathname portion.\n */\nexport function httpMetricsMiddleware(collector: MetricsCollector) {\n const requestsTotal =\n (collector.registry.get('http_requests_total') as Counter | undefined) ??\n collector.counter('http_requests_total', 'Total HTTP requests');\n const requestDuration =\n (collector.registry.get('http_request_duration_ms') as Histogram | undefined) ??\n collector.histogram('http_request_duration_ms', 'HTTP request duration in milliseconds');\n\n return async (\n req: MiddlewareRequest,\n res: MiddlewareResponse,\n next: NextFunction,\n ): Promise<void> => {\n const start = Date.now();\n try {\n await next();\n } finally {\n const duration = Date.now() - start;\n const path = extractPath(req.url);\n const labels = {\n method: req.method,\n status: String(res.statusCode),\n path,\n };\n requestsTotal.inc(labels);\n requestDuration.observe(labels, duration);\n }\n };\n}\n\nfunction extractPath(url: string): string {\n try {\n return new URL(url, 'http://localhost').pathname;\n } catch {\n return url.split('?')[0] ?? '/';\n }\n}\n"]}
|
package/dist/http.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MetricsCollector } from './index.cjs';
|
|
2
|
+
|
|
3
|
+
interface MiddlewareRequest {
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
}
|
|
7
|
+
interface MiddlewareResponse {
|
|
8
|
+
statusCode: number;
|
|
9
|
+
}
|
|
10
|
+
type NextFunction = () => Promise<void> | void;
|
|
11
|
+
/**
|
|
12
|
+
* HTTP metrics middleware.
|
|
13
|
+
*
|
|
14
|
+
* Records:
|
|
15
|
+
* - `http_requests_total` (counter) — method, status, path
|
|
16
|
+
* - `http_request_duration_ms` (histogram) — method, status, path
|
|
17
|
+
*
|
|
18
|
+
* The `path` label is derived from `req.url` using the pathname portion.
|
|
19
|
+
*/
|
|
20
|
+
declare function httpMetricsMiddleware(collector: MetricsCollector): (req: MiddlewareRequest, res: MiddlewareResponse, next: NextFunction) => Promise<void>;
|
|
21
|
+
|
|
22
|
+
export { type MiddlewareRequest, type MiddlewareResponse, type NextFunction, httpMetricsMiddleware };
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MetricsCollector } from './index.js';
|
|
2
|
+
|
|
3
|
+
interface MiddlewareRequest {
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
}
|
|
7
|
+
interface MiddlewareResponse {
|
|
8
|
+
statusCode: number;
|
|
9
|
+
}
|
|
10
|
+
type NextFunction = () => Promise<void> | void;
|
|
11
|
+
/**
|
|
12
|
+
* HTTP metrics middleware.
|
|
13
|
+
*
|
|
14
|
+
* Records:
|
|
15
|
+
* - `http_requests_total` (counter) — method, status, path
|
|
16
|
+
* - `http_request_duration_ms` (histogram) — method, status, path
|
|
17
|
+
*
|
|
18
|
+
* The `path` label is derived from `req.url` using the pathname portion.
|
|
19
|
+
*/
|
|
20
|
+
declare function httpMetricsMiddleware(collector: MetricsCollector): (req: MiddlewareRequest, res: MiddlewareResponse, next: NextFunction) => Promise<void>;
|
|
21
|
+
|
|
22
|
+
export { type MiddlewareRequest, type MiddlewareResponse, type NextFunction, httpMetricsMiddleware };
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/http.ts
|
|
2
|
+
function httpMetricsMiddleware(collector) {
|
|
3
|
+
const requestsTotal = collector.registry.get("http_requests_total") ?? collector.counter("http_requests_total", "Total HTTP requests");
|
|
4
|
+
const requestDuration = collector.registry.get("http_request_duration_ms") ?? collector.histogram("http_request_duration_ms", "HTTP request duration in milliseconds");
|
|
5
|
+
return async (req, res, next) => {
|
|
6
|
+
const start = Date.now();
|
|
7
|
+
try {
|
|
8
|
+
await next();
|
|
9
|
+
} finally {
|
|
10
|
+
const duration = Date.now() - start;
|
|
11
|
+
const path = extractPath(req.url);
|
|
12
|
+
const labels = {
|
|
13
|
+
method: req.method,
|
|
14
|
+
status: String(res.statusCode),
|
|
15
|
+
path
|
|
16
|
+
};
|
|
17
|
+
requestsTotal.inc(labels);
|
|
18
|
+
requestDuration.observe(labels, duration);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function extractPath(url) {
|
|
23
|
+
try {
|
|
24
|
+
return new URL(url, "http://localhost").pathname;
|
|
25
|
+
} catch {
|
|
26
|
+
return url.split("?")[0] ?? "/";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { httpMetricsMiddleware };
|
|
31
|
+
//# sourceMappingURL=http.js.map
|
|
32
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/http.ts"],"names":[],"mappings":";AAsBO,SAAS,sBAAsB,SAAA,EAA6B;AACjE,EAAA,MAAM,aAAA,GACH,UAAU,QAAA,CAAS,GAAA,CAAI,qBAAqB,CAAA,IAC7C,SAAA,CAAU,OAAA,CAAQ,qBAAA,EAAuB,qBAAqB,CAAA;AAChE,EAAA,MAAM,eAAA,GACH,UAAU,QAAA,CAAS,GAAA,CAAI,0BAA0B,CAAA,IAClD,SAAA,CAAU,SAAA,CAAU,0BAAA,EAA4B,uCAAuC,CAAA;AAEzF,EAAA,OAAO,OACL,GAAA,EACA,GAAA,EACA,IAAA,KACkB;AAClB,IAAA,MAAM,KAAA,GAAQ,KAAK,GAAA,EAAI;AACvB,IAAA,IAAI;AACF,MAAA,MAAM,IAAA,EAAK;AAAA,IACb,CAAA,SAAE;AACA,MAAA,MAAM,QAAA,GAAW,IAAA,CAAK,GAAA,EAAI,GAAI,KAAA;AAC9B,MAAA,MAAM,IAAA,GAAO,WAAA,CAAY,GAAA,CAAI,GAAG,CAAA;AAChC,MAAA,MAAM,MAAA,GAAS;AAAA,QACb,QAAQ,GAAA,CAAI,MAAA;AAAA,QACZ,MAAA,EAAQ,MAAA,CAAO,GAAA,CAAI,UAAU,CAAA;AAAA,QAC7B;AAAA,OACF;AACA,MAAA,aAAA,CAAc,IAAI,MAAM,CAAA;AACxB,MAAA,eAAA,CAAgB,OAAA,CAAQ,QAAQ,QAAQ,CAAA;AAAA,IAC1C;AAAA,EACF,CAAA;AACF;AAEA,SAAS,YAAY,GAAA,EAAqB;AACxC,EAAA,IAAI;AACF,IAAA,OAAO,IAAI,GAAA,CAAI,GAAA,EAAK,kBAAkB,CAAA,CAAE,QAAA;AAAA,EAC1C,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,CAAE,CAAC,CAAA,IAAK,GAAA;AAAA,EAC9B;AACF","file":"http.js","sourcesContent":["import type { Counter, Histogram, MetricsCollector } from './index.js';\n\nexport interface MiddlewareRequest {\n method: string;\n url: string;\n}\n\nexport interface MiddlewareResponse {\n statusCode: number;\n}\n\nexport type NextFunction = () => Promise<void> | void;\n\n/**\n * HTTP metrics middleware.\n *\n * Records:\n * - `http_requests_total` (counter) — method, status, path\n * - `http_request_duration_ms` (histogram) — method, status, path\n *\n * The `path` label is derived from `req.url` using the pathname portion.\n */\nexport function httpMetricsMiddleware(collector: MetricsCollector) {\n const requestsTotal =\n (collector.registry.get('http_requests_total') as Counter | undefined) ??\n collector.counter('http_requests_total', 'Total HTTP requests');\n const requestDuration =\n (collector.registry.get('http_request_duration_ms') as Histogram | undefined) ??\n collector.histogram('http_request_duration_ms', 'HTTP request duration in milliseconds');\n\n return async (\n req: MiddlewareRequest,\n res: MiddlewareResponse,\n next: NextFunction,\n ): Promise<void> => {\n const start = Date.now();\n try {\n await next();\n } finally {\n const duration = Date.now() - start;\n const path = extractPath(req.url);\n const labels = {\n method: req.method,\n status: String(res.statusCode),\n path,\n };\n requestsTotal.inc(labels);\n requestDuration.observe(labels, duration);\n }\n };\n}\n\nfunction extractPath(url: string): string {\n try {\n return new URL(url, 'http://localhost').pathname;\n } catch {\n return url.split('?')[0] ?? '/';\n }\n}\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
function escapeLabelValue(v) {
|
|
5
|
+
return v.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
|
6
|
+
}
|
|
7
|
+
function formatLabels(labels) {
|
|
8
|
+
const entries = Object.entries(labels).sort(([a], [b]) => a.localeCompare(b));
|
|
9
|
+
if (entries.length === 0) return "";
|
|
10
|
+
return entries.map(([k, v]) => `${k}="${escapeLabelValue(v)}"`).join(",");
|
|
11
|
+
}
|
|
12
|
+
function labelKey(labels) {
|
|
13
|
+
return Object.entries(labels).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${k}=${v}`).join(",");
|
|
14
|
+
}
|
|
15
|
+
var Counter = class {
|
|
16
|
+
constructor(name, help = "") {
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.help = help;
|
|
19
|
+
}
|
|
20
|
+
name;
|
|
21
|
+
help;
|
|
22
|
+
type = "counter";
|
|
23
|
+
values = /* @__PURE__ */ new Map();
|
|
24
|
+
inc(labels = {}, value = 1) {
|
|
25
|
+
if (value < 0) {
|
|
26
|
+
throw new Error("counter can only be incremented by non-negative values");
|
|
27
|
+
}
|
|
28
|
+
const key = labelKey(labels);
|
|
29
|
+
const existing = this.values.get(key);
|
|
30
|
+
if (existing) {
|
|
31
|
+
existing.value += value;
|
|
32
|
+
} else {
|
|
33
|
+
this.values.set(key, { labels, value });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
get(labels = {}) {
|
|
37
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
38
|
+
}
|
|
39
|
+
collect() {
|
|
40
|
+
const lines = [];
|
|
41
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
42
|
+
lines.push(`# TYPE ${this.name} counter`);
|
|
43
|
+
for (const { labels, value } of this.values.values()) {
|
|
44
|
+
const lbl = formatLabels(labels);
|
|
45
|
+
lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);
|
|
46
|
+
}
|
|
47
|
+
return lines.join("\n");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var Gauge = class {
|
|
51
|
+
constructor(name, help = "") {
|
|
52
|
+
this.name = name;
|
|
53
|
+
this.help = help;
|
|
54
|
+
}
|
|
55
|
+
name;
|
|
56
|
+
help;
|
|
57
|
+
type = "gauge";
|
|
58
|
+
values = /* @__PURE__ */ new Map();
|
|
59
|
+
set(labels, value) {
|
|
60
|
+
const key = labelKey(labels);
|
|
61
|
+
this.values.set(key, { labels, value });
|
|
62
|
+
}
|
|
63
|
+
inc(labels = {}, value = 1) {
|
|
64
|
+
const key = labelKey(labels);
|
|
65
|
+
const existing = this.values.get(key);
|
|
66
|
+
if (existing) {
|
|
67
|
+
existing.value += value;
|
|
68
|
+
} else {
|
|
69
|
+
this.values.set(key, { labels, value });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
dec(labels = {}, value = 1) {
|
|
73
|
+
this.inc(labels, -value);
|
|
74
|
+
}
|
|
75
|
+
get(labels = {}) {
|
|
76
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
77
|
+
}
|
|
78
|
+
collect() {
|
|
79
|
+
const lines = [];
|
|
80
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
81
|
+
lines.push(`# TYPE ${this.name} gauge`);
|
|
82
|
+
for (const { labels, value } of this.values.values()) {
|
|
83
|
+
const lbl = formatLabels(labels);
|
|
84
|
+
lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);
|
|
85
|
+
}
|
|
86
|
+
return lines.join("\n");
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var Histogram = class {
|
|
90
|
+
constructor(name, help = "", buckets = [5, 10, 25, 50, 100, 250, 500, 1e3, 2500, 5e3, 1e4]) {
|
|
91
|
+
this.name = name;
|
|
92
|
+
this.help = help;
|
|
93
|
+
this.buckets = buckets;
|
|
94
|
+
}
|
|
95
|
+
name;
|
|
96
|
+
help;
|
|
97
|
+
buckets;
|
|
98
|
+
type = "histogram";
|
|
99
|
+
values = /* @__PURE__ */ new Map();
|
|
100
|
+
observe(labels, value) {
|
|
101
|
+
const key = labelKey(labels);
|
|
102
|
+
const existing = this.values.get(key) ?? {
|
|
103
|
+
labels,
|
|
104
|
+
buckets: [...this.buckets],
|
|
105
|
+
bucketCounts: new Array(this.buckets.length + 1).fill(0),
|
|
106
|
+
sum: 0,
|
|
107
|
+
count: 0
|
|
108
|
+
};
|
|
109
|
+
if (!this.values.has(key)) {
|
|
110
|
+
this.values.set(key, existing);
|
|
111
|
+
}
|
|
112
|
+
for (let i = 0; i < existing.buckets.length; i++) {
|
|
113
|
+
const bucket = existing.buckets[i];
|
|
114
|
+
if (value <= bucket) {
|
|
115
|
+
existing.bucketCounts[i]++;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
existing.bucketCounts[existing.buckets.length]++;
|
|
119
|
+
existing.sum += value;
|
|
120
|
+
existing.count++;
|
|
121
|
+
}
|
|
122
|
+
get(labels = {}) {
|
|
123
|
+
const existing = this.values.get(labelKey(labels));
|
|
124
|
+
if (!existing) return void 0;
|
|
125
|
+
return {
|
|
126
|
+
sum: existing.sum,
|
|
127
|
+
count: existing.count,
|
|
128
|
+
buckets: [...existing.bucketCounts]
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
collect() {
|
|
132
|
+
const lines = [];
|
|
133
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
134
|
+
lines.push(`# TYPE ${this.name} histogram`);
|
|
135
|
+
for (const data of this.values.values()) {
|
|
136
|
+
const lbl = formatLabels(data.labels);
|
|
137
|
+
const prefix = lbl ? `${this.name}_bucket{${lbl},le=` : `${this.name}_bucket{le=`;
|
|
138
|
+
for (let i = 0; i < data.buckets.length; i++) {
|
|
139
|
+
lines.push(`${prefix}"${data.buckets[i]}"} ${data.bucketCounts[i]}`);
|
|
140
|
+
}
|
|
141
|
+
lines.push(`${prefix}"+Inf"} ${data.bucketCounts[data.buckets.length]}`);
|
|
142
|
+
if (lbl) {
|
|
143
|
+
lines.push(`${this.name}_sum{${lbl}} ${data.sum}`);
|
|
144
|
+
lines.push(`${this.name}_count{${lbl}} ${data.count}`);
|
|
145
|
+
} else {
|
|
146
|
+
lines.push(`${this.name}_sum ${data.sum}`);
|
|
147
|
+
lines.push(`${this.name}_count ${data.count}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return lines.join("\n");
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var Registry = class {
|
|
154
|
+
metrics = /* @__PURE__ */ new Map();
|
|
155
|
+
register(metric) {
|
|
156
|
+
this.metrics.set(metric.name, metric);
|
|
157
|
+
}
|
|
158
|
+
unregister(name) {
|
|
159
|
+
this.metrics.delete(name);
|
|
160
|
+
}
|
|
161
|
+
get(name) {
|
|
162
|
+
return this.metrics.get(name);
|
|
163
|
+
}
|
|
164
|
+
/** Return all metrics in Prometheus-compatible text format. */
|
|
165
|
+
collect() {
|
|
166
|
+
const parts = [];
|
|
167
|
+
for (const metric of this.metrics.values()) {
|
|
168
|
+
parts.push(metric.collect());
|
|
169
|
+
}
|
|
170
|
+
return parts.join("\n\n");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
function createMetricsCollector() {
|
|
174
|
+
const registry = new Registry();
|
|
175
|
+
return {
|
|
176
|
+
counter(name, help) {
|
|
177
|
+
const c = new Counter(name, help);
|
|
178
|
+
registry.register(c);
|
|
179
|
+
return c;
|
|
180
|
+
},
|
|
181
|
+
histogram(name, help, buckets) {
|
|
182
|
+
const h = new Histogram(name, help, buckets);
|
|
183
|
+
registry.register(h);
|
|
184
|
+
return h;
|
|
185
|
+
},
|
|
186
|
+
gauge(name, help) {
|
|
187
|
+
const g = new Gauge(name, help);
|
|
188
|
+
registry.register(g);
|
|
189
|
+
return g;
|
|
190
|
+
},
|
|
191
|
+
registry
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
exports.Counter = Counter;
|
|
196
|
+
exports.Gauge = Gauge;
|
|
197
|
+
exports.Histogram = Histogram;
|
|
198
|
+
exports.Registry = Registry;
|
|
199
|
+
exports.createMetricsCollector = createMetricsCollector;
|
|
200
|
+
//# sourceMappingURL=index.cjs.map
|
|
201
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,SAAS,iBAAiB,CAAA,EAAmB;AAC3C,EAAA,OAAO,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,MAAM,CAAA,CAAE,OAAA,CAAQ,IAAA,EAAM,KAAK,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,KAAK,CAAA;AAC3E;AAEA,SAAS,aAAa,MAAA,EAAwB;AAC5C,EAAA,MAAM,UAAU,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,CAAE,KAAK,CAAC,CAAC,CAAC,CAAA,EAAG,CAAC,CAAC,CAAA,KAAM,CAAA,CAAE,aAAA,CAAc,CAAC,CAAC,CAAA;AAC5E,EAAA,IAAI,OAAA,CAAQ,MAAA,KAAW,CAAA,EAAG,OAAO,EAAA;AACjC,EAAA,OAAO,QAAQ,GAAA,CAAI,CAAC,CAAC,CAAA,EAAG,CAAC,CAAA,KAAM,CAAA,EAAG,CAAC,CAAA,EAAA,EAAK,iBAAiB,CAAC,CAAC,CAAA,CAAA,CAAG,CAAA,CAAE,KAAK,GAAG,CAAA;AAC1E;AAEA,SAAS,SAAS,MAAA,EAAwB;AACxC,EAAA,OAAO,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,CACzB,IAAA,CAAK,CAAC,CAAC,CAAC,CAAA,EAAG,CAAC,CAAC,CAAA,KAAM,EAAE,aAAA,CAAc,CAAC,CAAC,CAAA,CACrC,GAAA,CAAI,CAAC,CAAC,CAAA,EAAG,CAAC,CAAA,KAAM,CAAA,EAAG,CAAC,CAAA,CAAA,EAAI,CAAC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n\nfunction escapeLabelValue(v: string): string {\n return v.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"').replace(/\\n/g, '\\\\n');\n}\n\nfunction formatLabels(labels: Labels): string {\n const entries = Object.entries(labels).sort(([a], [b]) => a.localeCompare(b));\n if (entries.length === 0) return '';\n return entries.map(([k, v]) => `${k}=\"${escapeLabelValue(v)}\"`).join(',');\n}\n\nfunction labelKey(labels: Labels): string {\n return Object.entries(labels)\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([k, v]) => `${k}=${v}`)\n .join(',');\n}\n\n/** Base interface for all metric types. */\nexport interface Metric {\n readonly name: string;\n readonly help: string;\n readonly type: string;\n collect(): string;\n}\n\n/**\n * Counter — a cumulative metric that only increases.\n *\n * Use `inc()` to increment by a given value (default 1).\n */\nexport class Counter implements Metric {\n readonly type = 'counter';\n private values = new Map<string, { labels: Labels; value: number }>();\n\n constructor(\n readonly name: string,\n readonly help = '',\n ) {}\n\n inc(labels: Labels = {}, value = 1): void {\n if (value < 0) {\n throw new Error('counter can only be incremented by non-negative values');\n }\n const key = labelKey(labels);\n const existing = this.values.get(key);\n if (existing) {\n existing.value += value;\n } else {\n this.values.set(key, { labels, value });\n }\n }\n\n get(labels: Labels = {}): number {\n return this.values.get(labelKey(labels))?.value ?? 0;\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} counter`);\n for (const { labels, value } of this.values.values()) {\n const lbl = formatLabels(labels);\n lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);\n }\n return lines.join('\\n');\n }\n}\n\n/**\n * Gauge — a metric that can go up or down.\n *\n * Use `set()`, `inc()`, or `dec()` to mutate the current value.\n */\nexport class Gauge implements Metric {\n readonly type = 'gauge';\n private values = new Map<string, { labels: Labels; value: number }>();\n\n constructor(\n readonly name: string,\n readonly help = '',\n ) {}\n\n set(labels: Labels, value: number): void {\n const key = labelKey(labels);\n this.values.set(key, { labels, value });\n }\n\n inc(labels: Labels = {}, value = 1): void {\n const key = labelKey(labels);\n const existing = this.values.get(key);\n if (existing) {\n existing.value += value;\n } else {\n this.values.set(key, { labels, value });\n }\n }\n\n dec(labels: Labels = {}, value = 1): void {\n this.inc(labels, -value);\n }\n\n get(labels: Labels = {}): number {\n return this.values.get(labelKey(labels))?.value ?? 0;\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} gauge`);\n for (const { labels, value } of this.values.values()) {\n const lbl = formatLabels(labels);\n lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);\n }\n return lines.join('\\n');\n }\n}\n\n/**\n * Histogram — samples observations and counts them in configurable buckets.\n *\n * Default buckets (ms): [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]\n */\nexport class Histogram implements Metric {\n readonly type = 'histogram';\n private values = new Map<\n string,\n {\n labels: Labels;\n buckets: number[];\n bucketCounts: number[];\n sum: number;\n count: number;\n }\n >();\n\n constructor(\n readonly name: string,\n readonly help = '',\n readonly buckets: number[] = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000],\n ) {}\n\n observe(labels: Labels, value: number): void {\n const key = labelKey(labels);\n const existing = this.values.get(key) ?? {\n labels,\n buckets: [...this.buckets],\n bucketCounts: new Array(this.buckets.length + 1).fill(0),\n sum: 0,\n count: 0,\n };\n if (!this.values.has(key)) {\n this.values.set(key, existing);\n }\n\n // Prometheus histograms are cumulative: a value increments every bucket\n // whose upper bound is >= the observed value, plus the +Inf bucket.\n for (let i = 0; i < existing.buckets.length; i++) {\n const bucket = existing.buckets[i]!;\n if (value <= bucket) {\n existing.bucketCounts[i]++;\n }\n }\n existing.bucketCounts[existing.buckets.length]!++;\n existing.sum += value;\n existing.count++;\n }\n\n get(labels: Labels = {}): { sum: number; count: number; buckets: number[] } | undefined {\n const existing = this.values.get(labelKey(labels));\n if (!existing) return undefined;\n return {\n sum: existing.sum,\n count: existing.count,\n buckets: [...existing.bucketCounts],\n };\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} histogram`);\n\n for (const data of this.values.values()) {\n const lbl = formatLabels(data.labels);\n const prefix = lbl ? `${this.name}_bucket{${lbl},le=` : `${this.name}_bucket{le=`;\n\n for (let i = 0; i < data.buckets.length; i++) {\n lines.push(`${prefix}\"${data.buckets[i]}\"} ${data.bucketCounts[i]}`);\n }\n lines.push(`${prefix}\"+Inf\"} ${data.bucketCounts[data.buckets.length]}`);\n\n if (lbl) {\n lines.push(`${this.name}_sum{${lbl}} ${data.sum}`);\n lines.push(`${this.name}_count{${lbl}} ${data.count}`);\n } else {\n lines.push(`${this.name}_sum ${data.sum}`);\n lines.push(`${this.name}_count ${data.count}`);\n }\n }\n\n return lines.join('\\n');\n }\n}\n\n/**\n * Registry — holds references to all metrics and can export them in\n * Prometheus text exposition format.\n */\nexport class Registry {\n private metrics = new Map<string, Metric>();\n\n register(metric: Metric): void {\n this.metrics.set(metric.name, metric);\n }\n\n unregister(name: string): void {\n this.metrics.delete(name);\n }\n\n get(name: string): Metric | undefined {\n return this.metrics.get(name);\n }\n\n /** Return all metrics in Prometheus-compatible text format. */\n collect(): string {\n const parts: string[] = [];\n for (const metric of this.metrics.values()) {\n parts.push(metric.collect());\n }\n return parts.join('\\n\\n');\n }\n}\n\n/** Convenience factory that creates metrics and registers them automatically. */\nexport interface MetricsCollector {\n counter(name: string, help?: string): Counter;\n histogram(name: string, help?: string, buckets?: number[]): Histogram;\n gauge(name: string, help?: string): Gauge;\n readonly registry: Registry;\n}\n\nexport function createMetricsCollector(): MetricsCollector {\n const registry = new Registry();\n return {\n counter(name: string, help?: string) {\n const c = new Counter(name, help);\n registry.register(c);\n return c;\n },\n histogram(name: string, help?: string, buckets?: number[]) {\n const h = new Histogram(name, help, buckets);\n registry.register(h);\n return h;\n },\n gauge(name: string, help?: string) {\n const g = new Gauge(name, help);\n registry.register(g);\n return g;\n },\n registry,\n };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
type Labels = Record<string, string>;
|
|
2
|
+
/** Base interface for all metric types. */
|
|
3
|
+
interface Metric {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly help: string;
|
|
6
|
+
readonly type: string;
|
|
7
|
+
collect(): string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Counter — a cumulative metric that only increases.
|
|
11
|
+
*
|
|
12
|
+
* Use `inc()` to increment by a given value (default 1).
|
|
13
|
+
*/
|
|
14
|
+
declare class Counter implements Metric {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly help: string;
|
|
17
|
+
readonly type = "counter";
|
|
18
|
+
private values;
|
|
19
|
+
constructor(name: string, help?: string);
|
|
20
|
+
inc(labels?: Labels, value?: number): void;
|
|
21
|
+
get(labels?: Labels): number;
|
|
22
|
+
collect(): string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gauge — a metric that can go up or down.
|
|
26
|
+
*
|
|
27
|
+
* Use `set()`, `inc()`, or `dec()` to mutate the current value.
|
|
28
|
+
*/
|
|
29
|
+
declare class Gauge implements Metric {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly help: string;
|
|
32
|
+
readonly type = "gauge";
|
|
33
|
+
private values;
|
|
34
|
+
constructor(name: string, help?: string);
|
|
35
|
+
set(labels: Labels, value: number): void;
|
|
36
|
+
inc(labels?: Labels, value?: number): void;
|
|
37
|
+
dec(labels?: Labels, value?: number): void;
|
|
38
|
+
get(labels?: Labels): number;
|
|
39
|
+
collect(): string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Histogram — samples observations and counts them in configurable buckets.
|
|
43
|
+
*
|
|
44
|
+
* Default buckets (ms): [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
|
|
45
|
+
*/
|
|
46
|
+
declare class Histogram implements Metric {
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly help: string;
|
|
49
|
+
readonly buckets: number[];
|
|
50
|
+
readonly type = "histogram";
|
|
51
|
+
private values;
|
|
52
|
+
constructor(name: string, help?: string, buckets?: number[]);
|
|
53
|
+
observe(labels: Labels, value: number): void;
|
|
54
|
+
get(labels?: Labels): {
|
|
55
|
+
sum: number;
|
|
56
|
+
count: number;
|
|
57
|
+
buckets: number[];
|
|
58
|
+
} | undefined;
|
|
59
|
+
collect(): string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Registry — holds references to all metrics and can export them in
|
|
63
|
+
* Prometheus text exposition format.
|
|
64
|
+
*/
|
|
65
|
+
declare class Registry {
|
|
66
|
+
private metrics;
|
|
67
|
+
register(metric: Metric): void;
|
|
68
|
+
unregister(name: string): void;
|
|
69
|
+
get(name: string): Metric | undefined;
|
|
70
|
+
/** Return all metrics in Prometheus-compatible text format. */
|
|
71
|
+
collect(): string;
|
|
72
|
+
}
|
|
73
|
+
/** Convenience factory that creates metrics and registers them automatically. */
|
|
74
|
+
interface MetricsCollector {
|
|
75
|
+
counter(name: string, help?: string): Counter;
|
|
76
|
+
histogram(name: string, help?: string, buckets?: number[]): Histogram;
|
|
77
|
+
gauge(name: string, help?: string): Gauge;
|
|
78
|
+
readonly registry: Registry;
|
|
79
|
+
}
|
|
80
|
+
declare function createMetricsCollector(): MetricsCollector;
|
|
81
|
+
|
|
82
|
+
export { Counter, Gauge, Histogram, type Labels, type Metric, type MetricsCollector, Registry, createMetricsCollector };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
type Labels = Record<string, string>;
|
|
2
|
+
/** Base interface for all metric types. */
|
|
3
|
+
interface Metric {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly help: string;
|
|
6
|
+
readonly type: string;
|
|
7
|
+
collect(): string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Counter — a cumulative metric that only increases.
|
|
11
|
+
*
|
|
12
|
+
* Use `inc()` to increment by a given value (default 1).
|
|
13
|
+
*/
|
|
14
|
+
declare class Counter implements Metric {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly help: string;
|
|
17
|
+
readonly type = "counter";
|
|
18
|
+
private values;
|
|
19
|
+
constructor(name: string, help?: string);
|
|
20
|
+
inc(labels?: Labels, value?: number): void;
|
|
21
|
+
get(labels?: Labels): number;
|
|
22
|
+
collect(): string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gauge — a metric that can go up or down.
|
|
26
|
+
*
|
|
27
|
+
* Use `set()`, `inc()`, or `dec()` to mutate the current value.
|
|
28
|
+
*/
|
|
29
|
+
declare class Gauge implements Metric {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly help: string;
|
|
32
|
+
readonly type = "gauge";
|
|
33
|
+
private values;
|
|
34
|
+
constructor(name: string, help?: string);
|
|
35
|
+
set(labels: Labels, value: number): void;
|
|
36
|
+
inc(labels?: Labels, value?: number): void;
|
|
37
|
+
dec(labels?: Labels, value?: number): void;
|
|
38
|
+
get(labels?: Labels): number;
|
|
39
|
+
collect(): string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Histogram — samples observations and counts them in configurable buckets.
|
|
43
|
+
*
|
|
44
|
+
* Default buckets (ms): [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]
|
|
45
|
+
*/
|
|
46
|
+
declare class Histogram implements Metric {
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly help: string;
|
|
49
|
+
readonly buckets: number[];
|
|
50
|
+
readonly type = "histogram";
|
|
51
|
+
private values;
|
|
52
|
+
constructor(name: string, help?: string, buckets?: number[]);
|
|
53
|
+
observe(labels: Labels, value: number): void;
|
|
54
|
+
get(labels?: Labels): {
|
|
55
|
+
sum: number;
|
|
56
|
+
count: number;
|
|
57
|
+
buckets: number[];
|
|
58
|
+
} | undefined;
|
|
59
|
+
collect(): string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Registry — holds references to all metrics and can export them in
|
|
63
|
+
* Prometheus text exposition format.
|
|
64
|
+
*/
|
|
65
|
+
declare class Registry {
|
|
66
|
+
private metrics;
|
|
67
|
+
register(metric: Metric): void;
|
|
68
|
+
unregister(name: string): void;
|
|
69
|
+
get(name: string): Metric | undefined;
|
|
70
|
+
/** Return all metrics in Prometheus-compatible text format. */
|
|
71
|
+
collect(): string;
|
|
72
|
+
}
|
|
73
|
+
/** Convenience factory that creates metrics and registers them automatically. */
|
|
74
|
+
interface MetricsCollector {
|
|
75
|
+
counter(name: string, help?: string): Counter;
|
|
76
|
+
histogram(name: string, help?: string, buckets?: number[]): Histogram;
|
|
77
|
+
gauge(name: string, help?: string): Gauge;
|
|
78
|
+
readonly registry: Registry;
|
|
79
|
+
}
|
|
80
|
+
declare function createMetricsCollector(): MetricsCollector;
|
|
81
|
+
|
|
82
|
+
export { Counter, Gauge, Histogram, type Labels, type Metric, type MetricsCollector, Registry, createMetricsCollector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function escapeLabelValue(v) {
|
|
3
|
+
return v.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
|
4
|
+
}
|
|
5
|
+
function formatLabels(labels) {
|
|
6
|
+
const entries = Object.entries(labels).sort(([a], [b]) => a.localeCompare(b));
|
|
7
|
+
if (entries.length === 0) return "";
|
|
8
|
+
return entries.map(([k, v]) => `${k}="${escapeLabelValue(v)}"`).join(",");
|
|
9
|
+
}
|
|
10
|
+
function labelKey(labels) {
|
|
11
|
+
return Object.entries(labels).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${k}=${v}`).join(",");
|
|
12
|
+
}
|
|
13
|
+
var Counter = class {
|
|
14
|
+
constructor(name, help = "") {
|
|
15
|
+
this.name = name;
|
|
16
|
+
this.help = help;
|
|
17
|
+
}
|
|
18
|
+
name;
|
|
19
|
+
help;
|
|
20
|
+
type = "counter";
|
|
21
|
+
values = /* @__PURE__ */ new Map();
|
|
22
|
+
inc(labels = {}, value = 1) {
|
|
23
|
+
if (value < 0) {
|
|
24
|
+
throw new Error("counter can only be incremented by non-negative values");
|
|
25
|
+
}
|
|
26
|
+
const key = labelKey(labels);
|
|
27
|
+
const existing = this.values.get(key);
|
|
28
|
+
if (existing) {
|
|
29
|
+
existing.value += value;
|
|
30
|
+
} else {
|
|
31
|
+
this.values.set(key, { labels, value });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
get(labels = {}) {
|
|
35
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
36
|
+
}
|
|
37
|
+
collect() {
|
|
38
|
+
const lines = [];
|
|
39
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
40
|
+
lines.push(`# TYPE ${this.name} counter`);
|
|
41
|
+
for (const { labels, value } of this.values.values()) {
|
|
42
|
+
const lbl = formatLabels(labels);
|
|
43
|
+
lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);
|
|
44
|
+
}
|
|
45
|
+
return lines.join("\n");
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var Gauge = class {
|
|
49
|
+
constructor(name, help = "") {
|
|
50
|
+
this.name = name;
|
|
51
|
+
this.help = help;
|
|
52
|
+
}
|
|
53
|
+
name;
|
|
54
|
+
help;
|
|
55
|
+
type = "gauge";
|
|
56
|
+
values = /* @__PURE__ */ new Map();
|
|
57
|
+
set(labels, value) {
|
|
58
|
+
const key = labelKey(labels);
|
|
59
|
+
this.values.set(key, { labels, value });
|
|
60
|
+
}
|
|
61
|
+
inc(labels = {}, value = 1) {
|
|
62
|
+
const key = labelKey(labels);
|
|
63
|
+
const existing = this.values.get(key);
|
|
64
|
+
if (existing) {
|
|
65
|
+
existing.value += value;
|
|
66
|
+
} else {
|
|
67
|
+
this.values.set(key, { labels, value });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
dec(labels = {}, value = 1) {
|
|
71
|
+
this.inc(labels, -value);
|
|
72
|
+
}
|
|
73
|
+
get(labels = {}) {
|
|
74
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
75
|
+
}
|
|
76
|
+
collect() {
|
|
77
|
+
const lines = [];
|
|
78
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
79
|
+
lines.push(`# TYPE ${this.name} gauge`);
|
|
80
|
+
for (const { labels, value } of this.values.values()) {
|
|
81
|
+
const lbl = formatLabels(labels);
|
|
82
|
+
lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);
|
|
83
|
+
}
|
|
84
|
+
return lines.join("\n");
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var Histogram = class {
|
|
88
|
+
constructor(name, help = "", buckets = [5, 10, 25, 50, 100, 250, 500, 1e3, 2500, 5e3, 1e4]) {
|
|
89
|
+
this.name = name;
|
|
90
|
+
this.help = help;
|
|
91
|
+
this.buckets = buckets;
|
|
92
|
+
}
|
|
93
|
+
name;
|
|
94
|
+
help;
|
|
95
|
+
buckets;
|
|
96
|
+
type = "histogram";
|
|
97
|
+
values = /* @__PURE__ */ new Map();
|
|
98
|
+
observe(labels, value) {
|
|
99
|
+
const key = labelKey(labels);
|
|
100
|
+
const existing = this.values.get(key) ?? {
|
|
101
|
+
labels,
|
|
102
|
+
buckets: [...this.buckets],
|
|
103
|
+
bucketCounts: new Array(this.buckets.length + 1).fill(0),
|
|
104
|
+
sum: 0,
|
|
105
|
+
count: 0
|
|
106
|
+
};
|
|
107
|
+
if (!this.values.has(key)) {
|
|
108
|
+
this.values.set(key, existing);
|
|
109
|
+
}
|
|
110
|
+
for (let i = 0; i < existing.buckets.length; i++) {
|
|
111
|
+
const bucket = existing.buckets[i];
|
|
112
|
+
if (value <= bucket) {
|
|
113
|
+
existing.bucketCounts[i]++;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
existing.bucketCounts[existing.buckets.length]++;
|
|
117
|
+
existing.sum += value;
|
|
118
|
+
existing.count++;
|
|
119
|
+
}
|
|
120
|
+
get(labels = {}) {
|
|
121
|
+
const existing = this.values.get(labelKey(labels));
|
|
122
|
+
if (!existing) return void 0;
|
|
123
|
+
return {
|
|
124
|
+
sum: existing.sum,
|
|
125
|
+
count: existing.count,
|
|
126
|
+
buckets: [...existing.bucketCounts]
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
collect() {
|
|
130
|
+
const lines = [];
|
|
131
|
+
if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);
|
|
132
|
+
lines.push(`# TYPE ${this.name} histogram`);
|
|
133
|
+
for (const data of this.values.values()) {
|
|
134
|
+
const lbl = formatLabels(data.labels);
|
|
135
|
+
const prefix = lbl ? `${this.name}_bucket{${lbl},le=` : `${this.name}_bucket{le=`;
|
|
136
|
+
for (let i = 0; i < data.buckets.length; i++) {
|
|
137
|
+
lines.push(`${prefix}"${data.buckets[i]}"} ${data.bucketCounts[i]}`);
|
|
138
|
+
}
|
|
139
|
+
lines.push(`${prefix}"+Inf"} ${data.bucketCounts[data.buckets.length]}`);
|
|
140
|
+
if (lbl) {
|
|
141
|
+
lines.push(`${this.name}_sum{${lbl}} ${data.sum}`);
|
|
142
|
+
lines.push(`${this.name}_count{${lbl}} ${data.count}`);
|
|
143
|
+
} else {
|
|
144
|
+
lines.push(`${this.name}_sum ${data.sum}`);
|
|
145
|
+
lines.push(`${this.name}_count ${data.count}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return lines.join("\n");
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var Registry = class {
|
|
152
|
+
metrics = /* @__PURE__ */ new Map();
|
|
153
|
+
register(metric) {
|
|
154
|
+
this.metrics.set(metric.name, metric);
|
|
155
|
+
}
|
|
156
|
+
unregister(name) {
|
|
157
|
+
this.metrics.delete(name);
|
|
158
|
+
}
|
|
159
|
+
get(name) {
|
|
160
|
+
return this.metrics.get(name);
|
|
161
|
+
}
|
|
162
|
+
/** Return all metrics in Prometheus-compatible text format. */
|
|
163
|
+
collect() {
|
|
164
|
+
const parts = [];
|
|
165
|
+
for (const metric of this.metrics.values()) {
|
|
166
|
+
parts.push(metric.collect());
|
|
167
|
+
}
|
|
168
|
+
return parts.join("\n\n");
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
function createMetricsCollector() {
|
|
172
|
+
const registry = new Registry();
|
|
173
|
+
return {
|
|
174
|
+
counter(name, help) {
|
|
175
|
+
const c = new Counter(name, help);
|
|
176
|
+
registry.register(c);
|
|
177
|
+
return c;
|
|
178
|
+
},
|
|
179
|
+
histogram(name, help, buckets) {
|
|
180
|
+
const h = new Histogram(name, help, buckets);
|
|
181
|
+
registry.register(h);
|
|
182
|
+
return h;
|
|
183
|
+
},
|
|
184
|
+
gauge(name, help) {
|
|
185
|
+
const g = new Gauge(name, help);
|
|
186
|
+
registry.register(g);
|
|
187
|
+
return g;
|
|
188
|
+
},
|
|
189
|
+
registry
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { Counter, Gauge, Histogram, Registry, createMetricsCollector };
|
|
194
|
+
//# sourceMappingURL=index.js.map
|
|
195
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAEA,SAAS,iBAAiB,CAAA,EAAmB;AAC3C,EAAA,OAAO,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,MAAM,CAAA,CAAE,OAAA,CAAQ,IAAA,EAAM,KAAK,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,KAAK,CAAA;AAC3E;AAEA,SAAS,aAAa,MAAA,EAAwB;AAC5C,EAAA,MAAM,UAAU,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,CA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,MAAc,IAAA,EAAe;AACnC,MAAA,MAAM,CAAA,GAAI,IAAI,OAAA,CAAQ,IAAA,EAAM,IAAI,CAAA;AAChC,MAAA,QAAA,CAAS,SAAS,CAAC,CAAA;AACnB,MAAA,OAAO,CAAA;AAAA,IACT,CAAA;AAAA,IACA,SAAA,CAAU,IAAA,EAAc,IAAA,EAAe,OAAA,EAAoB;AACzD,MAAA,MAAM,CAAA,GAAI,IAAI,SAAA,CAAU,IAAA,EAAM,MAAM,OAAO,CAAA;AAC3C,MAAA,QAAA,CAAS,SAAS,CAAC,CAAA;AACnB,MAAA,OAAO,CAAA;AAAA,IACT,CAAA;AAAA,IACA,KAAA,CAAM,MAAc,IAAA,EAAe;AACjC,MAAA,MAAM,CAAA,GAAI,IAAI,KAAA,CAAM,IAAA,EAAM,IAAI,CAAA;AAC9B,MAAA,QAAA,CAAS,SAAS,CAAC,CAAA;AACnB,MAAA,OAAO,CAAA;AAAA,IACT,CAAA;AAAA,IACA;AAAA,GACF;AACF","file":"index.js","sourcesContent":["export type Labels = Record<string, string>;\n\nfunction escapeLabelValue(v: string): string {\n return v.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"').replace(/\\n/g, '\\\\n');\n}\n\nfunction formatLabels(labels: Labels): string {\n const entries = Object.entries(labels).sort(([a], [b]) => a.localeCompare(b));\n if (entries.length === 0) return '';\n return entries.map(([k, v]) => `${k}=\"${escapeLabelValue(v)}\"`).join(',');\n}\n\nfunction labelKey(labels: Labels): string {\n return Object.entries(labels)\n .sort(([a], [b]) => a.localeCompare(b))\n .map(([k, v]) => `${k}=${v}`)\n .join(',');\n}\n\n/** Base interface for all metric types. */\nexport interface Metric {\n readonly name: string;\n readonly help: string;\n readonly type: string;\n collect(): string;\n}\n\n/**\n * Counter — a cumulative metric that only increases.\n *\n * Use `inc()` to increment by a given value (default 1).\n */\nexport class Counter implements Metric {\n readonly type = 'counter';\n private values = new Map<string, { labels: Labels; value: number }>();\n\n constructor(\n readonly name: string,\n readonly help = '',\n ) {}\n\n inc(labels: Labels = {}, value = 1): void {\n if (value < 0) {\n throw new Error('counter can only be incremented by non-negative values');\n }\n const key = labelKey(labels);\n const existing = this.values.get(key);\n if (existing) {\n existing.value += value;\n } else {\n this.values.set(key, { labels, value });\n }\n }\n\n get(labels: Labels = {}): number {\n return this.values.get(labelKey(labels))?.value ?? 0;\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} counter`);\n for (const { labels, value } of this.values.values()) {\n const lbl = formatLabels(labels);\n lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);\n }\n return lines.join('\\n');\n }\n}\n\n/**\n * Gauge — a metric that can go up or down.\n *\n * Use `set()`, `inc()`, or `dec()` to mutate the current value.\n */\nexport class Gauge implements Metric {\n readonly type = 'gauge';\n private values = new Map<string, { labels: Labels; value: number }>();\n\n constructor(\n readonly name: string,\n readonly help = '',\n ) {}\n\n set(labels: Labels, value: number): void {\n const key = labelKey(labels);\n this.values.set(key, { labels, value });\n }\n\n inc(labels: Labels = {}, value = 1): void {\n const key = labelKey(labels);\n const existing = this.values.get(key);\n if (existing) {\n existing.value += value;\n } else {\n this.values.set(key, { labels, value });\n }\n }\n\n dec(labels: Labels = {}, value = 1): void {\n this.inc(labels, -value);\n }\n\n get(labels: Labels = {}): number {\n return this.values.get(labelKey(labels))?.value ?? 0;\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} gauge`);\n for (const { labels, value } of this.values.values()) {\n const lbl = formatLabels(labels);\n lines.push(lbl ? `${this.name}{${lbl}} ${value}` : `${this.name} ${value}`);\n }\n return lines.join('\\n');\n }\n}\n\n/**\n * Histogram — samples observations and counts them in configurable buckets.\n *\n * Default buckets (ms): [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]\n */\nexport class Histogram implements Metric {\n readonly type = 'histogram';\n private values = new Map<\n string,\n {\n labels: Labels;\n buckets: number[];\n bucketCounts: number[];\n sum: number;\n count: number;\n }\n >();\n\n constructor(\n readonly name: string,\n readonly help = '',\n readonly buckets: number[] = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000],\n ) {}\n\n observe(labels: Labels, value: number): void {\n const key = labelKey(labels);\n const existing = this.values.get(key) ?? {\n labels,\n buckets: [...this.buckets],\n bucketCounts: new Array(this.buckets.length + 1).fill(0),\n sum: 0,\n count: 0,\n };\n if (!this.values.has(key)) {\n this.values.set(key, existing);\n }\n\n // Prometheus histograms are cumulative: a value increments every bucket\n // whose upper bound is >= the observed value, plus the +Inf bucket.\n for (let i = 0; i < existing.buckets.length; i++) {\n const bucket = existing.buckets[i]!;\n if (value <= bucket) {\n existing.bucketCounts[i]++;\n }\n }\n existing.bucketCounts[existing.buckets.length]!++;\n existing.sum += value;\n existing.count++;\n }\n\n get(labels: Labels = {}): { sum: number; count: number; buckets: number[] } | undefined {\n const existing = this.values.get(labelKey(labels));\n if (!existing) return undefined;\n return {\n sum: existing.sum,\n count: existing.count,\n buckets: [...existing.bucketCounts],\n };\n }\n\n collect(): string {\n const lines: string[] = [];\n if (this.help) lines.push(`# HELP ${this.name} ${this.help}`);\n lines.push(`# TYPE ${this.name} histogram`);\n\n for (const data of this.values.values()) {\n const lbl = formatLabels(data.labels);\n const prefix = lbl ? `${this.name}_bucket{${lbl},le=` : `${this.name}_bucket{le=`;\n\n for (let i = 0; i < data.buckets.length; i++) {\n lines.push(`${prefix}\"${data.buckets[i]}\"} ${data.bucketCounts[i]}`);\n }\n lines.push(`${prefix}\"+Inf\"} ${data.bucketCounts[data.buckets.length]}`);\n\n if (lbl) {\n lines.push(`${this.name}_sum{${lbl}} ${data.sum}`);\n lines.push(`${this.name}_count{${lbl}} ${data.count}`);\n } else {\n lines.push(`${this.name}_sum ${data.sum}`);\n lines.push(`${this.name}_count ${data.count}`);\n }\n }\n\n return lines.join('\\n');\n }\n}\n\n/**\n * Registry — holds references to all metrics and can export them in\n * Prometheus text exposition format.\n */\nexport class Registry {\n private metrics = new Map<string, Metric>();\n\n register(metric: Metric): void {\n this.metrics.set(metric.name, metric);\n }\n\n unregister(name: string): void {\n this.metrics.delete(name);\n }\n\n get(name: string): Metric | undefined {\n return this.metrics.get(name);\n }\n\n /** Return all metrics in Prometheus-compatible text format. */\n collect(): string {\n const parts: string[] = [];\n for (const metric of this.metrics.values()) {\n parts.push(metric.collect());\n }\n return parts.join('\\n\\n');\n }\n}\n\n/** Convenience factory that creates metrics and registers them automatically. */\nexport interface MetricsCollector {\n counter(name: string, help?: string): Counter;\n histogram(name: string, help?: string, buckets?: number[]): Histogram;\n gauge(name: string, help?: string): Gauge;\n readonly registry: Registry;\n}\n\nexport function createMetricsCollector(): MetricsCollector {\n const registry = new Registry();\n return {\n counter(name: string, help?: string) {\n const c = new Counter(name, help);\n registry.register(c);\n return c;\n },\n histogram(name: string, help?: string, buckets?: number[]) {\n const h = new Histogram(name, help, buckets);\n registry.register(h);\n return h;\n },\n gauge(name: string, help?: string) {\n const g = new Gauge(name, help);\n registry.register(g);\n return g;\n },\n registry,\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fabricorg/experiments-metrics",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Lightweight zero-dependency Prometheus-style metrics collector.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./http": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/http.d.ts",
|
|
25
|
+
"default": "./dist/http.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/http.d.cts",
|
|
29
|
+
"default": "./dist/http.cjs"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.15.3",
|
|
38
|
+
"tsup": "^8.3.5",
|
|
39
|
+
"typescript": "^5.8.3",
|
|
40
|
+
"vitest": "^2.1.8"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"type-check": "tsc --noEmit",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"clean": "rm -rf dist"
|
|
47
|
+
}
|
|
48
|
+
}
|