monten 0.1.3 → 0.1.5
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/README.md +6 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/logger/format.js +110 -1
- package/dist/middleware/httpMiddleware.d.ts +1 -1
- package/dist/middleware/httpMiddleware.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ pnpm add monten
|
|
|
71
71
|
|
|
72
72
|
```ts
|
|
73
73
|
import express from 'express'
|
|
74
|
-
import { initObservability,
|
|
74
|
+
import { initObservability, monten } from 'monten'
|
|
75
75
|
|
|
76
76
|
const app = express()
|
|
77
77
|
|
|
@@ -83,7 +83,7 @@ initObservability({
|
|
|
83
83
|
})
|
|
84
84
|
|
|
85
85
|
// Add middleware (single line!)
|
|
86
|
-
app.use(
|
|
86
|
+
app.use(monten())
|
|
87
87
|
|
|
88
88
|
app.get('/health', (_req, res) => {
|
|
89
89
|
res.status(200).json({ ok: true })
|
|
@@ -132,12 +132,12 @@ initObservability({
|
|
|
132
132
|
})
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
### `
|
|
135
|
+
### `monten()`
|
|
136
136
|
|
|
137
137
|
Express middleware that captures request timing, errors, and emits logs/metrics.
|
|
138
138
|
|
|
139
139
|
```ts
|
|
140
|
-
app.use(
|
|
140
|
+
app.use(monten())
|
|
141
141
|
```
|
|
142
142
|
|
|
143
143
|
### `withDbTiming<T>(fn: () => Promise<T>): Promise<T>`
|
|
@@ -175,7 +175,7 @@ Each request produces a structured JSON log entry:
|
|
|
175
175
|
┌─────────────────────────────────────────────────────────┐
|
|
176
176
|
│ Express App │
|
|
177
177
|
├─────────────────────────────────────────────────────────┤
|
|
178
|
-
│
|
|
178
|
+
│ monten() │
|
|
179
179
|
│ ┌───────────────────────────────────────────────────┐ │
|
|
180
180
|
│ │ AsyncLocalStorage Context │ │
|
|
181
181
|
│ │ • requestId │ │
|
|
@@ -201,7 +201,7 @@ Each request produces a structured JSON log entry:
|
|
|
201
201
|
This library is designed to be fully removable:
|
|
202
202
|
|
|
203
203
|
1. Remove `initObservability()` call
|
|
204
|
-
2. Remove `app.use(
|
|
204
|
+
2. Remove `app.use(monten())`
|
|
205
205
|
3. Remove `withDbTiming()` wrappers (just call the inner function directly)
|
|
206
206
|
|
|
207
207
|
Your application will continue to function normally.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { ObservabilityInitConfig } from './types';
|
|
2
2
|
export declare function initObservability(config: ObservabilityInitConfig): void;
|
|
3
|
-
export declare function
|
|
3
|
+
export declare function monten(): (req: any, res: any, next: (err?: any) => void) => void;
|
|
4
4
|
export declare function withDbTiming<T>(fn: () => Promise<T>): Promise<T>;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initObservability = initObservability;
|
|
4
|
-
exports.
|
|
4
|
+
exports.monten = monten;
|
|
5
5
|
exports.withDbTiming = withDbTiming;
|
|
6
6
|
const observability_config_1 = require("./config/observability.config");
|
|
7
7
|
const httpMiddleware_1 = require("./middleware/httpMiddleware");
|
|
@@ -9,8 +9,8 @@ const dbTimer_1 = require("./db/dbTimer");
|
|
|
9
9
|
function initObservability(config) {
|
|
10
10
|
(0, observability_config_1.initObservabilityInternal)(config);
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
return (0, httpMiddleware_1.
|
|
12
|
+
function monten() {
|
|
13
|
+
return (0, httpMiddleware_1.monten)();
|
|
14
14
|
}
|
|
15
15
|
function withDbTiming(fn) {
|
|
16
16
|
return (0, dbTimer_1.withDbTiming)(fn);
|
package/dist/logger/format.js
CHANGED
|
@@ -1,6 +1,115 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatLog = formatLog;
|
|
4
|
+
// ANSI color codes
|
|
5
|
+
const colors = {
|
|
6
|
+
reset: '\x1b[0m',
|
|
7
|
+
red: '\x1b[31m',
|
|
8
|
+
green: '\x1b[32m',
|
|
9
|
+
yellow: '\x1b[33m',
|
|
10
|
+
blue: '\x1b[34m',
|
|
11
|
+
magenta: '\x1b[35m',
|
|
12
|
+
cyan: '\x1b[36m',
|
|
13
|
+
gray: '\x1b[90m',
|
|
14
|
+
bright: '\x1b[1m',
|
|
15
|
+
};
|
|
16
|
+
function colorize(text, color) {
|
|
17
|
+
return `${colors[color]}${text}${colors.reset}`;
|
|
18
|
+
}
|
|
19
|
+
function getLevelColor(level) {
|
|
20
|
+
switch (level) {
|
|
21
|
+
case 'error':
|
|
22
|
+
return 'red';
|
|
23
|
+
case 'warn':
|
|
24
|
+
return 'yellow';
|
|
25
|
+
case 'info':
|
|
26
|
+
return 'green';
|
|
27
|
+
case 'debug':
|
|
28
|
+
return 'cyan';
|
|
29
|
+
default:
|
|
30
|
+
return 'reset';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function getLatencyColor(latencyMs) {
|
|
34
|
+
if (!latencyMs)
|
|
35
|
+
return 'reset';
|
|
36
|
+
if (latencyMs > 1000)
|
|
37
|
+
return 'red'; // > 1s = too slow
|
|
38
|
+
if (latencyMs > 300)
|
|
39
|
+
return 'yellow'; // > 300ms = moderate
|
|
40
|
+
return 'green'; // fast
|
|
41
|
+
}
|
|
42
|
+
function formatTimestamp(timestamp) {
|
|
43
|
+
const date = new Date(timestamp);
|
|
44
|
+
return date.toISOString();
|
|
45
|
+
}
|
|
4
46
|
function formatLog(entry) {
|
|
5
|
-
|
|
47
|
+
const timestamp = colorize(formatTimestamp(entry.timestamp), 'gray');
|
|
48
|
+
const level = colorize(entry.level.toUpperCase().padEnd(5), getLevelColor(entry.level));
|
|
49
|
+
const service = colorize(`[${entry.serviceName}]`, 'cyan');
|
|
50
|
+
const message = colorize(entry.message, 'bright');
|
|
51
|
+
let output = `${timestamp} ${level} ${service}`;
|
|
52
|
+
if (entry.requestId) {
|
|
53
|
+
output += ` ${colorize(`reqId:${entry.requestId.slice(0, 8)}`, 'magenta')}`;
|
|
54
|
+
}
|
|
55
|
+
output += ` ${message}`;
|
|
56
|
+
if (entry.fields) {
|
|
57
|
+
const fields = entry.fields;
|
|
58
|
+
// Special handling for HTTP request logs
|
|
59
|
+
if (fields.method && fields.path) {
|
|
60
|
+
output += `\n ${colorize('→', 'gray')} ${colorize(fields.method, 'blue')} ${fields.path}`;
|
|
61
|
+
if (fields.statusCode) {
|
|
62
|
+
const statusColor = fields.statusCode >= 500
|
|
63
|
+
? 'red'
|
|
64
|
+
: fields.statusCode >= 400
|
|
65
|
+
? 'yellow'
|
|
66
|
+
: 'green';
|
|
67
|
+
output += ` ${colorize(`[${fields.statusCode}]`, statusColor)}`;
|
|
68
|
+
}
|
|
69
|
+
if (fields.latencyMs !== undefined) {
|
|
70
|
+
const latencyColor = getLatencyColor(fields.latencyMs);
|
|
71
|
+
output += ` ${colorize(`${fields.latencyMs}ms`, latencyColor)}`;
|
|
72
|
+
}
|
|
73
|
+
if (fields.dbTimeMs !== undefined) {
|
|
74
|
+
const dbColor = getLatencyColor(fields.dbTimeMs);
|
|
75
|
+
output += ` ${colorize(`(db: ${fields.dbTimeMs}ms)`, dbColor)}`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Special handling for metrics flush
|
|
79
|
+
else if (fields.metrics && Array.isArray(fields.metrics)) {
|
|
80
|
+
output += `\n ${colorize('→', 'gray')} Flushed ${colorize(String(fields.count), 'yellow')} metrics`;
|
|
81
|
+
fields.metrics.forEach((metric, idx) => {
|
|
82
|
+
if (metric.type === 'http_request') {
|
|
83
|
+
const latencyColor = getLatencyColor(metric.latencyMs);
|
|
84
|
+
const statusColor = metric.statusCode >= 500
|
|
85
|
+
? 'red'
|
|
86
|
+
: metric.statusCode >= 400
|
|
87
|
+
? 'yellow'
|
|
88
|
+
: 'green';
|
|
89
|
+
output += `\n ${colorize(`${idx + 1}.`, 'gray')} ${colorize(metric.method, 'blue')} ${metric.path} `;
|
|
90
|
+
output += `${colorize(`[${metric.statusCode}]`, statusColor)} `;
|
|
91
|
+
output += `${colorize(`${metric.latencyMs}ms`, latencyColor)}`;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Generic field display
|
|
96
|
+
else {
|
|
97
|
+
const fieldEntries = Object.entries(fields);
|
|
98
|
+
if (fieldEntries.length > 0) {
|
|
99
|
+
output +=
|
|
100
|
+
'\n ' +
|
|
101
|
+
fieldEntries
|
|
102
|
+
.map(([key, value]) => {
|
|
103
|
+
let valueColor = 'reset';
|
|
104
|
+
// Color numeric values based on size
|
|
105
|
+
if (typeof value === 'number') {
|
|
106
|
+
valueColor = getLatencyColor(value);
|
|
107
|
+
}
|
|
108
|
+
return `${colorize(key, 'gray')}: ${colorize(String(value), valueColor)}`;
|
|
109
|
+
})
|
|
110
|
+
.join(', ');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return output;
|
|
6
115
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function monten(): (req: any, res: any, next: (err?: any) => void) => void;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.monten = monten;
|
|
4
4
|
const observability_config_1 = require("../config/observability.config");
|
|
5
5
|
const requestContext_1 = require("../context/requestContext");
|
|
6
6
|
const logger_1 = require("../logger/logger");
|
|
7
7
|
const metricsBuffer_1 = require("../metrics/metricsBuffer");
|
|
8
8
|
const time_1 = require("../utils/time");
|
|
9
9
|
const uuid_1 = require("../utils/uuid");
|
|
10
|
-
function
|
|
10
|
+
function monten() {
|
|
11
11
|
return function httpMiddleware(req, res, next) {
|
|
12
12
|
const cfg = (0, observability_config_1.getObservabilityConfig)();
|
|
13
13
|
const requestId = (0, uuid_1.generateRequestId)();
|