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
|
import { TraceUtil, LogMetricTraceable, LogTraceable } from '@emprespresso/pengueno';
const greetings = ['hewwo :D', 'hiya cutie', 'boop!', 'sending virtual hugs!', 'stay pawsitive'];
const penguenoGreeting = () => greetings[Math.floor(Math.random() * greetings.length)];
export class PenguenoRequest extends Request {
private constructor(
_input: Request,
public readonly id: string,
public readonly at: Date,
) {
super(_input);
}
public baseResponseHeaders(): Record<string, string> {
const ServerRequestTime = this.at.getTime();
const ServerResponseTime = Date.now();
const DeltaTime = ServerResponseTime - ServerRequestTime;
const RequestId = this.id;
return Object.entries({
RequestId,
ServerRequestTime,
ServerResponseTime,
DeltaTime,
Hai: penguenoGreeting(),
}).reduce((acc, [key, val]) => ({ ...acc, [key]: val!.toString() }), {});
}
public static from(request: Request): LogMetricTraceable<PenguenoRequest> {
const id = crypto.randomUUID();
const url = new URL(request.url);
const { pathname } = url;
const logTraceable = LogTraceable.of(new PenguenoRequest(request, id, new Date())).bimap(
TraceUtil.withTrace(`RequestId = ${id}, Method = ${request.method}, Path = ${pathname}`),
);
return LogMetricTraceable.ofLogTraceable(logTraceable);
}
}
|