Getting started
This page installs the Stalker Node Sensor from the npm registry, runs a small server with it, and reads back which functions ran for two different requests. At the end you have loaded Stalker into a Node process without changing its code, and read Function Coverage per Request Key from rat.
Stalker 0.1.0 is released as the npm package @slopkit/stalker, which this page installs, and as the Rust crate stalker-rust, which you add as a git dependency (see Next steps). The repositories are public, and neither git clone nor npm install needs an account.
Prerequisites
- Node 22.12 or newer, and
npm - Bun,
git,curlandjq. rat is a Bun program that you run from a checkout, and the steps usecurlandjqto talk to it.
The steps use three terminals.
- Terminal 1
Runs rat.
- Terminal 2
Runs your server with Stalker.
- Terminal 3
Sends requests.
Install Stalker
Terminal 2 Make a project directory. Point the @slopkit scope at the Stalker project's npm registry in its .npmrc, then install the package:
mkdir stalker-demo cd stalker-demo npm init -y echo '@slopkit:registry=https://gitlab.techtailors.net/api/v4/projects/slopkit%2Fstalker/packages/npm/' > .npmrc npm install @slopkit/[email protected]
Output
npm init -y prints the new package.json. npm install ends with the package count:
added 190 packages, and audited 191 packages in 1s
The count can differ, and npm may print deprecation, audit and install-script notices around it.
Start rat
Stalker sends every Signal to rat, which stores it. rat is a Bun program in the analytics repository, so you run it from a checkout.
Terminal 1 Run:
git clone https://gitlab.techtailors.net/slopkit/analytics.git cd analytics bun install COLLECTOR_DUCKDB_PATH=/tmp/rat.duckdb bash scripts/with-libstdcxx bun run collector/src/main.ts
Output
rat prints that it is listening and keeps running:
collector listening on http://127.0.0.1:7410
That is the address Stalker sends to by default.
Terminal 3 Check that rat answers:
curl http://127.0.0.1:7410/health
ok
Run a server with Stalker loaded
Terminal 2 In the stalker-demo directory, save this as server.cjs. It is a plain Node server with two functions that each run for one route only, and no Stalker code:
const http = require('node:http');
// Two code paths that only run for their own route.
function onlyInA() {
return 'a';
}
function onlyInB() {
return 'b';
}
const server = http.createServer(async (req, res) => {
const send = (body) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify(body));
};
if (req.url === '/echo') {
return send({ traceparent: req.headers.traceparent ?? null, baggage: req.headers.baggage ?? null });
}
if (req.url === '/work') {
// The server calls itself, so you can see the headers go out again.
const echo = await (await fetch('http://127.0.0.1:8080/echo')).json();
return send({ echo });
}
if (req.url === '/path-a') return send({ path: onlyInA() });
if (req.url === '/path-b') return send({ path: onlyInB() });
res.statusCode = 404;
send({ error: 'not found' });
});
server.listen(8080, '127.0.0.1', () => console.log('listening 8080'));
Start it with Stalker loaded:
STALKER_COVERAGE=1 node --require @slopkit/stalker/register server.cjs
listening 8080
The --require flag loads @slopkit/stalker/register before the server starts, and STALKER_COVERAGE=1 turns on Function Coverage. For ES modules use --import instead, and for Bun use --preload.
Send requests with a Request Key
A Request Key is the hunt.scenario and hunt.step entries in a request's baggage header.
Terminal 3 Send a request that also carries a traceparent:
curl -H 'traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01' \
-H 'baggage: hunt.scenario=signup,hunt.step=3' localhost:8080/work
The /work route calls the server's own /echo route and returns the headers it received. The trace id and the baggage arrive unchanged on the outgoing call. The span id is new, so yours will differ:
{"echo":{"traceparent":"00-0af7651916cd43dd8448eb211c80319c-c5f8a83291835ec8-01","baggage":"hunt.scenario=signup,hunt.step=3"}}
Now send two requests that run different functions, one for step 1 and one for step 2. Wait two seconds between them and after the second, so Stalker files each Request Key:
curl -H 'baggage: hunt.scenario=signup,hunt.step=1' localhost:8080/path-a sleep 2 curl -H 'baggage: hunt.scenario=signup,hunt.step=2' localhost:8080/path-b sleep 2
{"path":"a"}{"path":"b"}
Read back the functions that ran
rat answers Function Coverage over HTTP. Ask for step 1, then step 2, and keep your server's two functions:
curl -s 'localhost:7410/v1/coverage?scenario=signup&step=1' | jq -c '.files[].functions[] | select(.name | startswith("only")) | {name, line, count}'
curl -s 'localhost:7410/v1/coverage?scenario=signup&step=2' | jq -c '.files[].functions[] | select(.name | startswith("only")) | {name, line, count}'
Step 1 ran only onlyInA and step 2 ran only onlyInB:
{"name":"onlyInA","line":4,"count":1}
{"name":"onlyInB","line":7,"count":1}
The server code did not change. Stalker filed the functions that ran under each Request Key, and rat returned them.
Next steps
Open Docs for every setting, the Bun and Rust Sensors, and what Stalker does and does not cover.
A Rust service adds the stalker-rust crate as a git dependency on the release tag, because the crate is not on crates.io:
stalker-rust = { git = "https://gitlab.techtailors.net/slopkit/stalker.git", tag = "0.1.0" }
The source is at gitlab.techtailors.net/slopkit/stalker.