observe-node 1.0.8 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "observe-node",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Structured logs + Prometheus metrics SDK for Node.js (Loki/Grafana ready)",
5
5
  "main": "src/index.js",
6
6
  "type": "commonjs",
package/src/index.js CHANGED
@@ -3,8 +3,12 @@ const start = require("./start");
3
3
  const { expressMiddleware } = require("./http/express");
4
4
  const { createExpressErrorMiddleware } = require("./http/expressError");
5
5
 
6
+ // Prometheus metrics helper
7
+ const { createPrometheusMetrics } = require("./metrics/prometheus");
8
+
6
9
  module.exports = {
7
10
  start,
8
11
  expressMiddleware,
9
12
  createExpressErrorMiddleware,
13
+ createPrometheusMetrics,
10
14
  };
@@ -0,0 +1,77 @@
1
+ // src/metrics/prometheus.js
2
+ const client = require("prom-client");
3
+
4
+ function createPrometheusMetrics(options = {}) {
5
+ const {
6
+ service = "app",
7
+ metricsPath = "/metrics",
8
+ collectDefaultMetrics = true,
9
+ buckets = [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2, 5],
10
+ } = options;
11
+
12
+ const register = new client.Registry();
13
+
14
+ if (collectDefaultMetrics) {
15
+ client.collectDefaultMetrics({ register });
16
+ }
17
+
18
+ const httpRequestsTotal = new client.Counter({
19
+ name: "http_requests_total",
20
+ help: "Total HTTP requests",
21
+ labelNames: ["service", "method", "route", "status"],
22
+ });
23
+
24
+ const httpRequestDurationSeconds = new client.Histogram({
25
+ name: "http_request_duration_seconds",
26
+ help: "HTTP request duration in seconds",
27
+ labelNames: ["service", "method", "route", "status"],
28
+ buckets,
29
+ });
30
+
31
+ register.registerMetric(httpRequestsTotal);
32
+ register.registerMetric(httpRequestDurationSeconds);
33
+
34
+ // ✅ Express middleware (counts requests)
35
+ function metricsMiddleware() {
36
+ return function (req, res, next) {
37
+ // do not count scraping itself
38
+ if (req.path === metricsPath) return next();
39
+
40
+ const start = process.hrtime.bigint();
41
+
42
+ res.on("finish", () => {
43
+ const end = process.hrtime.bigint();
44
+ const seconds = Number(end - start) / 1e9;
45
+
46
+ // route after match (best effort)
47
+ const route =
48
+ (req.route && req.route.path) ||
49
+ (req.baseUrl ? req.baseUrl + req.path : req.path) ||
50
+ req.path ||
51
+ "unknown";
52
+
53
+ const labels = {
54
+ service,
55
+ method: req.method,
56
+ route,
57
+ status: String(res.statusCode),
58
+ };
59
+
60
+ httpRequestsTotal.inc(labels, 1);
61
+ httpRequestDurationSeconds.observe(labels, seconds);
62
+ });
63
+
64
+ next();
65
+ };
66
+ }
67
+
68
+ // ✅ /metrics handler
69
+ async function metricsHandler(req, res) {
70
+ res.setHeader("Content-Type", register.contentType);
71
+ res.end(await register.metrics());
72
+ }
73
+
74
+ return { metricsMiddleware, metricsHandler, register };
75
+ }
76
+
77
+ module.exports = { createPrometheusMetrics };