Instrumentations load by scope
While AppSignal automatically instruments code executed inside the request handlers of supported HTTP frameworks, there are scenarios where you may need to use helper functions to manually instrument AppSignal, for example in a one-off script or before your application is built.
In such scenarios we recommend using the instrumentationsLoaded()
helper function, which returns a promise to ensure that theinstrumentation is ready to start reporting to AppSignal.
You only need to wrap your custom instrumentation helper calls within a promise, not your entire codebase. There are two ways of doing this depending on your Node.js configuration:
Using import
syntax
When using the ES import
syntax, you need to call await
before using any custom instrumentation, like in the example below:
import { sendError, instrumentationsLoaded } from "@appsignal/nodejs";
await instrumentationsLoaded();
sendError(new Error("Test error"));
Using require
syntax
When using the require
syntax, you need to call .then()
on the promise returned by instrumentationsLoaded()
, like in the example below:
const { instrumentationsLoaded, sendError } = require("@appsignal/nodejs");
instrumentationsLoaded().then(() => {
sendError(new Error("Test error"));
});