Register middleware on root file
require("elastic-apm-node").start(); // must be in the first line
// other import libraries
...
new Plumier()
.use(new APMMiddleware())
...
import { ActionResult, CustomMiddleware, HttpStatusError, Invocation } from "@plumier/core"; | |
import APM from 'elastic-apm-node'; | |
export class APMMiddleware implements CustomMiddleware { | |
async execute(next: Readonly<Invocation>): Promise<ActionResult> { | |
if (next.ctx?.route?.url) { | |
APM.setTransactionName(next.ctx.route.url); | |
} else { | |
APM.setTransactionName(next.ctx.url); | |
} | |
try { | |
return await next.proceed(); | |
} catch (e) { | |
if (e instanceof HttpStatusError) { | |
APM.captureError(e); | |
} else { | |
APM.captureError(e); | |
} | |
throw e; | |
} | |
} | |
} |