Node
Palette supports node behind a flag. At Palette, we use the Node integration to monitor our own backend performance.
Install
npm install @palette.dev/node
Usage
- Node
- Next.js
import { init, network, markers, profiler } from "@palette.dev/node";
init({
key: "<YOUR CLIENT KEY>",
plugins: [network(), markers(), profiler()],
});
// Auto instrumentation of API route handlers
export const handler = async (event, context) => {
// Some expensive work...
// Profiler automatically stops at the end of this handler
return {
statusCode: 200,
body: JSON.stringify({ message: "Hello World" }),
};
};
- Initialize Palette in
utils/palette.js:
utils/palette.js
import { init, network, markers, profiler } from "@palette.dev/node";
init({
key: "<YOUR CLIENT KEY>",
plugins: [network(), markers(), profiler()],
version: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA ?? "dev",
});
- Wrap your API routes using
withPaletteand importutils/palette.js:
pages/api/some-route.js
import { withPalette } from "@palette.dev/node";
import "../../utils/palette";
const handler = (req, res) => {
// ...
};
export default withPalette(handler, "/api/some-route");
Measuring Latency
In Next.js, API route latency is automatically collected. To add this metric to your dashboard, go to your project's dashboard > Select All Metrics > Select Mark and Measure > Select api.response.
You can also use the mark and measure APIs to measure the duration of database queries, network requests, and other potentially slow operations.
Here is an example of measuring the duration of a database query:
pages/api/some-route.js
import { mark, measure } from "@palette.dev/node";
const handler = async (req, res) => {
mark("my-query.start");
await db.query("select * from some_table where id = 1");
measure("my-query.total", "my-query.start");
// ...
};