Tracepanel

Tracing your code

Add steps of your own to a trace with span(), make a script or a scheduled job a trace with trace(), and name the signed-in user with setUser().

Tracepanel records your framework and libraries on its own. To see your own code in a trace, wrap it in span(). To trace work that isn't a request, such as a script or a scheduled job, wrap it in trace().

span()

span() runs a function as a step of the current trace. The SQL, calls and logs it makes go inside the step in the call tree:

import { span } from "@tracepanel/js";

const rows = await span("load stale rows", () => loadStaleRows());

It returns what the function returns. An error the function throws marks the step as failed and goes on to your code. A third argument adds attributes to the step:

await span("charge", () => charge(order), { "order.id": order.id });

trace()

trace() runs a function as a trace of its own. The console lists it under Jobs:

import { span, trace } from "@tracepanel/js";

await trace("nightly cleanup", async () => {
  await span("load stale rows", () => loadStaleRows());
});

Inside a request, trace() is a step of the request, as span() is. The kind option makes the trace a task, which the console lists under Tasks, and attrs adds its attributes:

await trace("send digest", () => sendDigest(user), {
  kind: "task",
  attrs: { "task.queue": "mail" },
});

Run a script with Tracepanel on through the tracepanel command:

npx @tracepanel/js run node script.js

Outside development, span() and trace() run their function and nothing else.

profile()

profile() profiles the rest of the current trace, a request's or a trace()'s, on Node, as the panel's Profile button profiles a request. profile({ memory: true }) counts its memory too:

import { profile } from "@tracepanel/js";

app.post("/reports", async (req, res) => {
  if (req.user.isStaff) profile();
  res.json(await rebuildReport());
});

From then on the trace's outgoing calls, through fetch() and node:http, ask the apps they reach to profile their part, and the BullMQ jobs it adds ask the worker. It returns whether the trace is profiled: false outside a trace, outside development or with TRACEPANEL_PROFILE_ENABLED=0. A trace profiled already keeps its reason. Otherwise the Profile tab says it was profiled by your code. See Profile.

setUser()

setUser() names the signed-in user on the current trace. Call it where the app knows who is signed in, such as its auth middleware:

import { setUser } from "@tracepanel/js";

setUser({ id: customer.id, username: customer.email });

The trace gets user.id, and user.username and user.email when you give them. The console shows whose request it was, and finds a user's traces. setUser(null) marks a request nobody signed in to. The username and email stay on your machine. Outside a trace, setUser() does nothing.

On this page