Getting started
This page installs the stalker command, runs a small server with Stalker attached, 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 or installing anything into it, and read Function Coverage per Request Key from rat.
Stalker 0.2.0 is released as the stalker command, which this page installs, as the npm package @slopkit/stalker for products that want the Sensor in their package.json, 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
- Linux on x86-64, and Node 22.12 or newer on your
PATH.stalkerruns your ownnodeorbun. - 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 Run the install script. It reads the stable channel from stalker.slopkit.dev, downloads the release binary for x86-64 Linux, checks its SHA-256 against the project's package registry, installs it into ~/.local/bin, and runs stalker --version. The binary is self-extracting and needs no Nix and no npm install.
curl -fsSL https://stalker.slopkit.dev/install.sh | sh
If ~/.local/bin is not on your PATH, add it for this shell, then check the install:
export PATH="$HOME/.local/bin:$PATH" stalker --version
stalker 0.2.0
Four environment variables change what the script does: STALKER_VERSION installs an exact version instead of the channel, STALKER_CHANNEL picks another channel, STALKER_INSTALL_DIR sets the directory, and STALKER_SHA256 sets the expected checksum instead of looking it up. The binary updates itself: run stalker update later to move to the newest release on the stable channel.
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 Make a directory. It needs no package.json:
mkdir stalker-demo cd stalker-demo
Save this as server.cjs in it. 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 attached:
STALKER_COVERAGE=1 stalker run -- node server.cjs
listening 8080
stalker run starts the command after -- with the Stalker preload loaded into your own node, and passes STALKER_* settings through, so STALKER_COVERAGE=1 turns on Function Coverage. ES modules work the same way, and so does Bun: stalker run -- bun run server.ts. The command's exit code is stalker's.
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.
To keep the Sensor in a product's package.json instead, install the npm package @slopkit/stalker from the project's registry and load it with node --require @slopkit/stalker/register server.cjs. The reference has the steps.
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.2.0" }
The source is at gitlab.techtailors.net/slopkit/stalker.