Skip to content

Commit d619ad3

Browse files
authored
logs latency in TS API (#3042)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Add elapsed-time logging by timing requests and passing start time to `httpLogger` across all response paths. > > - **Runtime/Logging**: > - Update `httpLogger` to accept `startMs` and log request latency in ms. > - Capture `start = Date.now()` in `apiHandler` and `createMainRouter` request handlers. > - Invoke `httpLogger(req, res, start)` on all exits (auth failures, success responses, errors, 404) in `packages/ts-moose-lib/src/consumption-apis/runner.ts`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit d8061a5. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 8c896b0 commit d619ad3

File tree

1 file changed

+21
-11
lines changed
  • packages/ts-moose-lib/src/consumption-apis

1 file changed

+21
-11
lines changed

packages/ts-moose-lib/src/consumption-apis/runner.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ const toClientConfig = (config: ClickhouseConfig) => ({
5151

5252
const createPath = (apisDir: string, path: string) => `${apisDir}${path}.ts`;
5353

54-
const httpLogger = (req: http.IncomingMessage, res: http.ServerResponse) => {
55-
console.log(`${req.method} ${req.url} ${res.statusCode}`);
54+
const httpLogger = (
55+
req: http.IncomingMessage,
56+
res: http.ServerResponse,
57+
startMs: number,
58+
) => {
59+
console.log(
60+
`${req.method} ${req.url} ${res.statusCode} ${Date.now() - startMs}ms`,
61+
);
5662
};
5763

5864
const modulesCache = new Map<string, any>();
@@ -82,6 +88,8 @@ const apiHandler = async (
8288
) => {
8389
const apis = isDmv2 ? await getApis() : new Map();
8490
return async (req: http.IncomingMessage, res: http.ServerResponse) => {
91+
const start = Date.now();
92+
8593
try {
8694
const url = new URL(req.url || "", "http://localhost");
8795
const fileName = url.pathname;
@@ -101,20 +109,20 @@ const apiHandler = async (
101109
if (enforceAuth) {
102110
res.writeHead(401, { "Content-Type": "application/json" });
103111
res.end(JSON.stringify({ error: "Unauthorized" }));
104-
httpLogger(req, res);
112+
httpLogger(req, res, start);
105113
return;
106114
}
107115
}
108116
} else if (enforceAuth) {
109117
res.writeHead(401, { "Content-Type": "application/json" });
110118
res.end(JSON.stringify({ error: "Unauthorized" }));
111-
httpLogger(req, res);
119+
httpLogger(req, res, start);
112120
return;
113121
}
114122
} else if (enforceAuth) {
115123
res.writeHead(401, { "Content-Type": "application/json" });
116124
res.end(JSON.stringify({ error: "Unauthorized" }));
117-
httpLogger(req, res);
125+
httpLogger(req, res, start);
118126
return;
119127
}
120128

@@ -224,10 +232,10 @@ const apiHandler = async (
224232

225233
if (status) {
226234
res.writeHead(status, { "Content-Type": "application/json" });
227-
httpLogger(req, res);
235+
httpLogger(req, res, start);
228236
} else {
229237
res.writeHead(200, { "Content-Type": "application/json" });
230-
httpLogger(req, res);
238+
httpLogger(req, res, start);
231239
}
232240

233241
res.end(body);
@@ -237,16 +245,16 @@ const apiHandler = async (
237245
if (Object.getPrototypeOf(error).constructor.name === "TypeGuardError") {
238246
res.writeHead(400, { "Content-Type": "application/json" });
239247
res.end(JSON.stringify({ error: error.message }));
240-
httpLogger(req, res);
248+
httpLogger(req, res, start);
241249
}
242250
if (error instanceof Error) {
243251
res.writeHead(500, { "Content-Type": "application/json" });
244252
res.end(JSON.stringify({ error: error.message }));
245-
httpLogger(req, res);
253+
httpLogger(req, res, start);
246254
} else {
247255
res.writeHead(500, { "Content-Type": "application/json" });
248256
res.end();
249-
httpLogger(req, res);
257+
httpLogger(req, res, start);
250258
}
251259
}
252260
};
@@ -280,6 +288,8 @@ const createMainRouter = async (
280288
});
281289

282290
return async (req: http.IncomingMessage, res: http.ServerResponse) => {
291+
const start = Date.now();
292+
283293
const url = new URL(req.url || "", "http://localhost");
284294
const pathname = url.pathname;
285295

@@ -391,7 +401,7 @@ const createMainRouter = async (
391401

392402
res.writeHead(404, { "Content-Type": "application/json" });
393403
res.end(JSON.stringify({ error: "Not Found" }));
394-
httpLogger(req, res);
404+
httpLogger(req, res, start);
395405
};
396406
};
397407

0 commit comments

Comments
 (0)