teletouch-observability 1.0.0

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 ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "teletouch-observability",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "exports": {
7
+ ".": "./src/index.js"
8
+ },
9
+ "files": ["src"],
10
+ "type": "module",
11
+ "scripts": {
12
+ "test": "mocha"
13
+ },
14
+ "keywords": [
15
+ "observability",
16
+ "metrics",
17
+ "logs",
18
+ "prometheus"
19
+ ],
20
+ "author": "Rodrigo Ruiz <teparuiz>",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "prom-client": "^15.1.3",
24
+ "winston": "^3.19.0"
25
+ },
26
+ "devDependencies": {
27
+ "chai": "^6.2.2",
28
+ "mocha": "^11.7.6",
29
+ "supertest": "^7.2.2"
30
+ }
31
+ }
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./logger";
2
+ export * from "./metrics";
3
+ export * from "./middleware";
4
+ export * from "./metricsServer";
package/src/logger.js ADDED
@@ -0,0 +1,91 @@
1
+ import winston from 'winston';
2
+
3
+ const logger = winston.createLogger({
4
+ level: "info",
5
+ format: winston.format.combine(
6
+ winston.format.timestamp({
7
+ format: 'YYYY-MM-DD HH:mm:ss'
8
+ }),
9
+ winston.format.splat(),
10
+ winston.format.errors({ stack: true }),
11
+ winston.format.json()
12
+ ),
13
+ transports: [
14
+ new winston.transports.Console({
15
+ stderrLevels: ["error", "warn"]
16
+ })
17
+ ]
18
+ })
19
+
20
+ process.on('uncaughtException', (error) => {
21
+ logger.error({
22
+ source: 'process',
23
+ event: 'uncaughtException',
24
+ error: {
25
+ name: error?.name,
26
+ message: error?.message || String(error),
27
+ stack: error?.stack,
28
+ },
29
+ });
30
+ process.exit(1);
31
+ })
32
+
33
+ process.on('unhandledRejection', (reason) => {
34
+ logger.error({
35
+ source: 'process',
36
+ event: 'unhandledRejection',
37
+ error: {
38
+ name: reason?.name,
39
+ message: reason?.message || String(reason),
40
+ stack: reason?.stack,
41
+ },
42
+ });
43
+ });
44
+
45
+ export function ErrorLog(source, error, requestId, req = null) {
46
+ logger.error({
47
+ requestId,
48
+ source,
49
+ event: 'error',
50
+ method: req?.method,
51
+ path: req?.path,
52
+ error: {
53
+ name: error?.name,
54
+ message: error?.message || String(error),
55
+ stack: error?.stack,
56
+ },
57
+ });
58
+ }
59
+
60
+ export function WarnLog(source, message, requestId, req = null) {
61
+ logger.warn({
62
+ requestId,
63
+ source,
64
+ event: 'warn',
65
+ method: req?.method,
66
+ path: req?.path,
67
+ message,
68
+ });
69
+ }
70
+
71
+ export function InfoLog(source, data, requestId, req = null) {
72
+ logger.info({
73
+ requestId,
74
+ source,
75
+ event: 'info',
76
+ method: req?.method,
77
+ path: req?.path,
78
+ data,
79
+ });
80
+ }
81
+
82
+ export function DebugLog(source, data, requestId, req = null) {
83
+ logger.debug({
84
+ requestId,
85
+ source,
86
+ event: 'debug',
87
+ method: req?.method,
88
+ path: req?.path,
89
+ data,
90
+ });
91
+ }
package/src/metrics.js ADDED
@@ -0,0 +1,7 @@
1
+ import client from "prom-client";
2
+
3
+ export const register = new client.Registry();
4
+
5
+ client.collectDefaultMetrics({ register });
6
+
7
+ export const metrics = register.metrics;
@@ -0,0 +1,41 @@
1
+ import http from "http";
2
+ import { register } from "./metrics.js";
3
+
4
+ export class MetricsServer {
5
+ constructor({ port = 9464, registry = register }) {
6
+ this.port = port;
7
+ this.registry = registry;
8
+ this.server = null;
9
+ }
10
+
11
+ start() {
12
+ this.server = http.createServer(async (req, res) => {
13
+ const url = new URL(req.url, `http://${req.headers.host}`);
14
+ if (url.pathname !== "/metrics") {
15
+ res.statusCode = 404;
16
+ return res.end("not found");
17
+ }
18
+
19
+ try {
20
+ res.setHeader(
21
+ "Content-Type",
22
+ this.registry.contentType
23
+ );
24
+
25
+ const metrics = await this.registry.metrics();
26
+ res.statusCode = 200;
27
+ res.end(metrics);
28
+ } catch (err) {
29
+ res.statusCode = 500;
30
+ res.end("error generating metrics");
31
+ }
32
+ });
33
+
34
+ this.server.listen(this.port);
35
+ return this.server;
36
+ }
37
+
38
+ stop() {
39
+ this.server?.close();
40
+ }
41
+ }
@@ -0,0 +1,40 @@
1
+ import { randomUUID } from 'crypto';
2
+
3
+
4
+ export class HttpLogger {
5
+ constructor({logger}) {
6
+ this.logger = logger;
7
+ }
8
+
9
+ middleware() {
10
+ return (req, res, next) => {
11
+ const requestId = randomUUID();
12
+ req.requestId = requestId;
13
+
14
+ const start = process.hrtime.bigint();
15
+ const url = req.originalUrl || req.url;
16
+
17
+ this.logger.info("http_request_in", {
18
+ requestId,
19
+ method: req.method,
20
+ url,
21
+ body: req.body,
22
+ statusCode: res.statusCode,
23
+ source: 'http'
24
+ })
25
+
26
+ res.on('finish', () => {
27
+ this.logger.info("http_request_out", {
28
+ requestId,
29
+ method: req.method,
30
+ url,
31
+ source: 'http',
32
+ statusCode: res.statusCode,
33
+ duration: process.hrtime.bigint() - start
34
+ })
35
+ })
36
+
37
+ next();
38
+ }
39
+ }
40
+ }