# 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`, `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, 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/stalker@0.1.0
```

`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. 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, 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:

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

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 https://gitlab.techtailors.net/slopkit/stalker.

<!-- verified:
Environment: this machine (Linux x86-64) in the slopkit/stalker dev shell for Node 24.22.2 and Bun 1.4.4. rat was a git archive of slopkit/analytics main (ec322b4), `bun install`ed in a scratch directory.
The page was first run from an `npm pack` tarball of the release branch. After the 0.1.0 tag, `npm install @slopkit/stalker@0.1.0` from the registry with only the `.npmrc` scope line (no credentials) installed it, and `require.resolve("@slopkit/stalker/register")` found the preload.
 PASSED: rat started and answered /health with ok; server.cjs started with `STALKER_COVERAGE=1 node --require @slopkit/stalker/register 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 rat command was run from the cloned directory name `analytics`. The span id in the /work output differs on every run. The vulnerability and install-script notices that npm prints differ between machines.
NOT RUN: Bun and ES module preloads from the npm install, the Rust dependency from this site (it was checked against a local clone of the repository with a tag, with cargo metadata and cargo check), the registry install without credentials.
-->
