Skip to content

Instantly share code, notes, and snippets.

@ahmadarif
Last active June 1, 2021 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadarif/05c16f9c141c3f2e54aee1bffac52223 to your computer and use it in GitHub Desktop.
Save ahmadarif/05c16f9c141c3f2e54aee1bffac52223 to your computer and use it in GitHub Desktop.
Plumier + APM

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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment