Skip to content

Instantly share code, notes, and snippets.

@ahmadarif
Last active June 1, 2021 15:56
Show Gist options
  • Select an option

  • Save ahmadarif/05c16f9c141c3f2e54aee1bffac52223 to your computer and use it in GitHub Desktop.

Select an option

Save ahmadarif/05c16f9c141c3f2e54aee1bffac52223 to your computer and use it in GitHub Desktop.

Revisions

  1. @ahmadarif-lab ahmadarif-lab revised this gist Jun 1, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Register middleware on root file

    ```
    ```ts
    require("elastic-apm-node").start(); // must be in the first line

    // other import libraries
  2. @ahmadarif-lab ahmadarif-lab created this gist Jun 1, 2021.
    23 changes: 23 additions & 0 deletions apm.middleware.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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;
    }
    }
    }
    12 changes: 12 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    Register middleware on root file

    ```
    require("elastic-apm-node").start(); // must be in the first line
    // other import libraries
    ...
    new Plumier()
    .use(new APMMiddleware())
    ...
    ```