# Getting started

This page installs the Stalker Node Sensor from source, runs an example 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 has no published release yet, so you install it from its repository. Both repositories below are public, and `git clone` needs no account.

## Prerequisites

- `git`
- `devenv`, which provides Node, Bun, `curl` and `jq` in the dev shell

The steps use three terminals. Terminal 1 runs rat, terminal 2 runs the example server, and terminal 3 sends requests. In each one, change into the `stalker` directory and run `devenv shell` first.

## Install Stalker

In terminal 1, run:

```
git clone https://gitlab.techtailors.net/slopkit/stalker.git
cd stalker
devenv shell
npm install
```

The dev shell prints its name, and `npm install` ends with the package count:

```
slopkit/stalker dev shell
...
added 194 packages, and audited 196 packages in 1s
```

The count can differ, and npm may print deprecation and audit notices after 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. Still in terminal 1, inside the dev shell:

```
git clone https://gitlab.techtailors.net/slopkit/analytics.git ../analytics
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, from the `stalker` directory, inside the dev shell, start the example server:

```
PORT=8080 STALKER_COVERAGE=1 node --require ./packages/node/register.cjs examples/node-server.cjs
```

```
listening 8080
```

`examples/node-server.cjs` contains no Stalker code. The `--require` flag loads `@slopkit/stalker/register` before the server starts, and `STALKER_COVERAGE=1` turns on Function Coverage.

## 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, from the `stalker` directory, inside the dev shell, 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-6d19a8cba5dd5975-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 the example'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":18,"count":1}
{"name":"onlyInB","line":21,"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. The source is at https://gitlab.techtailors.net/slopkit/stalker.
