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 { 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 { 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); } }