forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.ts
More file actions
55 lines (44 loc) · 1.64 KB
/
Copy pathwrapper.ts
File metadata and controls
55 lines (44 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { v4 as uuidv4 } from "uuid";
import { logger } from "./logger";
import { metrics } from "./metrics";
export type ApiHandler = (_req: any, _res: any) => Promise<void> | void;
export function withObservability(handler: ApiHandler, name: string): ApiHandler {
return async (req, res) => {
const requestId = uuidv4();
const startTime = Date.now();
// Attach request context for logging
const childLogger = logger.child({
requestId,
method: req.method,
url: req.url,
clientIp: req.headers["x-forwarded-for"] || req.socket.remoteAddress,
});
try {
childLogger.info({ body: req.body }, `Request started: ${name}`);
// Inject logger into request if needed, or just use the childLogger
req.logger = childLogger;
req.requestId = requestId;
await handler(req, res);
const duration = Date.now() - startTime;
metrics.emit("api_request_duration_ms", duration, { path: name, status: res.statusCode });
childLogger.info(
{ statusCode: res.statusCode, duration },
`Request completed: ${name}`
);
} catch (error) {
const duration = Date.now() - startTime;
const message = error instanceof Error ? error.message : "Unknown error";
childLogger.error(
{ error: message, stack: error instanceof Error ? error.stack : undefined, duration },
`Request failed: ${name}`
);
metrics.emit("api_request_error_total", 1, { path: name, error: message });
if (!res.writableEnded) {
res.status(500).json({
error: "Internal server error",
requestId,
});
}
}
};
}