-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
fApi.ts
29 lines (28 loc) · 1007 Bytes
/
fApi.ts
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
import { Histogram } from "prom-client";
import { fApi } from "../helpers.js";
const httpRequestDurationmS = new Histogram({
name: "request_duration_ms",
help: "Duration of HTTP requests in ms",
labelNames: ["method", "hostname", "pathname", "status"],
buckets: [1, 10, 50, 100, 250, 500],
});
type WithStartTime<T> = T & { _startTime: number };
fApi.extend({
hooks: {
beforeRequest: [
(options) => {
(<WithStartTime<typeof options>>options)._startTime = Date.now();
},
],
afterResponse: [
(res) => {
const url = res.requestUrl;
const options = <WithStartTime<typeof res.request.options>>res.request.options;
const thumbsIndex = url.pathname.indexOf("thumbnails");
const pathname = thumbsIndex !== -1 ? url.pathname.substring(0, thumbsIndex + thumbsIndex + 10) : url.pathname;
httpRequestDurationmS.observe({ method: options.method, hostname: url.hostname, pathname, status: res.statusCode }, Date.now() - options._startTime);
return res;
},
],
},
});