# 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`. `stalker` runs your own `node` or `bun`.
- Bun, `git`, `curl` and `jq`. rat is a Bun program that you run from a checkout, and the steps use `curl` and `jq` to talk to it.

The steps use three terminals. Terminal 1 runs rat, terminal 2 runs your server with Stalker, and terminal 3 sends requests.

## Install Stalker

In 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. In terminal 1:

```
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
```

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. In terminal 3, check that rat answers:

```
curl http://127.0.0.1:7410/health
```

```
ok
```

## Run a server with Stalker loaded

In 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:

```js
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. In 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 the [reference](/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 https://gitlab.techtailors.net/slopkit/stalker.

<!-- verified:
Environment: this machine (Linux x86-64), Node 24.22.2 and Bun 1.4.4 on PATH. rat was a git archive of slopkit/analytics main (ec322b4), `bun install`ed in a scratch directory.
The release was not tagged yet, so `stalker` was built from the stack/stalker-cram branch of slopkit/stalker: `nix build .#stalker` (`result/bin/stalker`), and `cram build --config cram.toml --app stalker` (the self-extracting binary, run with XDG_DATA_HOME and HOME in a scratch directory).
 PASSED with both builds: rat started and answered /health with ok; server.cjs (no package.json, no node_modules in its directory) started with `STALKER_COVERAGE=1 stalker run -- node server.cjs` (listening 8080); /work returned the echoed traceparent and baggage; /path-a and /path-b with baggage steps 1 and 2; both /v1/coverage queries returned only onlyInA (line 4) and only onlyInB (line 7). The same with the examples/bun-server.ts example under `stalker run -- bun run srv.ts`. `stalker --version` printed stalker 0.2.0. The cram binary ran the user's own node (not the one bundled for the CLI).
 The span id in the /work output differs on every run.
NOT RUN until the 0.2.0 tag is released and the website pointers exist (check after the release): `curl -fsSL https://stalker.slopkit.dev/install.sh | sh` and its output, `stalker --version` from that install, `stalker update` (and the `cram: stalker 0.2.0 is already up to date` line), `STALKER_VERSION=0.2.0` and `STALKER_CHANNEL=stable` for the script, `npm install @slopkit/stalker@0.2.0` from the registry (the `stalker` bin of the package was checked with `npm pack --dry-run` only), and the Rust dependency with tag 0.2.0 (the tag does not exist yet).
-->
