-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnext-server.js
53 lines (45 loc) · 1.58 KB
/
next-server.js
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
const shimmer = require("shimmer");
const { client } = require("./prom.js");
const prefix = process.env.NEXTJS_PROMETHEUS_PREFIX ? process.env.NEXTJS_PROMETHEUS_PREFIX + "_" : "";
const httpRequestDurationSeconds = new client.Histogram({
name: `${prefix}http_request_duration_seconds`,
help: "Duration of HTTP requests to API routes in seconds",
labelNames: ["route"],
buckets: [0.01, 0.03, 0.05, 0.1, 0.9, 1],
});
const httpRequestsTotal = new client.Counter({
name: `${prefix}http_requests_total`,
help: "Total number of HTTP requests to API routes",
labelNames: ["route"],
});
const httpErrorsTotal = new client.Counter({
name: `${prefix}http_errors_total`,
help: "Total number of HTTP errors in API routes",
labelNames: ["route"],
});
module.exports = function initialize(nextServer) {
const Server = nextServer.default;
shimmer.wrap(Server.prototype, "runApi", function (originalFn) {
return async function wrappedRunApi() {
const [, , query, params, page] = arguments;
let hasError = false;
let response;
const startTime = performance.now();
try {
response = await originalFn.apply(this, arguments);
} catch (error) {
hasError = true;
} finally {
const endTime = performance.now();
const durationInSeconds = (endTime - startTime) / 1000;
httpRequestsTotal.labels(page).inc();
httpRequestDurationSeconds.labels(page).observe(durationInSeconds);
if (hasError) {
httpErrorsTotal.labels(page).inc();
return;
}
}
return response;
};
});
};