The Lab: Real Tools on Fixed Data
Observability tools are servers that watch the clock: Prometheus scrapes every few seconds, Loki stamps log lines, the Collector batches spans by time. That makes their output change on every run. This course still runs them for real, by feeding them data with fixed timestamps and asking about fixed moments. This lesson shows the four recipes you will see again and again.
What is in the sandbox
Every bash run block runs in a fresh Linux container with no network, as the user learner in an empty home directory:
prometheus --version | head -1
promtool --version | head -1
alertmanager --version | head -1
amtool --version | head -1
loki --version | head -1
logcli --version 2>&1 | head -1
otelcol-contrib --versionprometheus, version 3.15.1 (branch: HEAD, revision: bae3eced0a5369e050e83ea73bc381d7c4b1a719)
promtool, version 3.15.1 (branch: HEAD, revision: bae3eced0a5369e050e83ea73bc381d7c4b1a719)
alertmanager, version 0.34.1 (branch: HEAD, revision: 73c6bfe7393929211294c1954f30d8ed78e4d0ad)
amtool, version 0.34.1 (branch: HEAD, revision: 73c6bfe7393929211294c1954f30d8ed78e4d0ad)
loki, version 3.7.9 (branch: release-3.7.x, revision: d3b4a2f7)
logcli, version 3.7.9 (branch: release-3.7.x, revision: d3b4a2f7)
otelcol-contrib version 0.164.0
Also there: curl, jq, yq and python3. Grafana is not (it is a web UI), so its dashboards, provisioning files and alerting appear as configuration files and sample screens, not runs.
Recipe 1: promtool test rules
promtool test rules evaluates recording and alerting rules against synthetic series you write in the test file, on a virtual clock. No server, no waiting, fully deterministic. It is how real teams test alerts in CI, and in this course it is the main way to show what a PromQL expression returns:
cat > rules.yml <<'YML'
groups:
- name: demo
rules:
- record: job:http_requests:rate5m
expr: sum by (job) (rate(http_requests_total[5m]))
YML
cat > test.yml <<'YML'
rule_files: [rules.yml]
tests:
- interval: 1m
input_series:
- series: 'http_requests_total{job="linkly", instance="api-1"}'
values: '0+60x10' # 0, 60, 120, ... : 60 more every minute
- series: 'http_requests_total{job="linkly", instance="api-2"}'
values: '0+120x10'
promql_expr_test:
- expr: job:http_requests:rate5m
eval_time: 10m
exp_samples:
- labels: 'job:http_requests:rate5m{job="linkly"}'
value: 3
YML
promtool test rules test.yml SUCCESS
'0+60x10' is the expanding notation: start at 0 and add 60, ten times, one value per interval. One instance serves 1 request/s, the other 2, so the per-job rate is 3. The promtool tests lesson covers the notation fully.
Recipe 2: a Prometheus server on backfilled history
For longer queries, the session writes an hour of history in the OpenMetrics text format with explicit timestamps, turns it into TSDB blocks with promtool tsdb create-blocks-from openmetrics, starts Prometheus on those blocks and queries a fixed time with promtool query instant:
python3 - > data.om <<'PY'
start = 1767225600 # 2026-01-01T00:00:00Z
print("# HELP http_requests_total Requests handled.")
print("# TYPE http_requests_total counter")
for code, per_sec in (("302", 9), ("500", 1)):
for i in range(0, 41): # one sample every 15 s for 10 minutes
t = start + 15 * i
print(f'http_requests_total{{job="linkly",code="{code}"}} {per_sec * 15 * i} {t}')
print("# EOF")
PY
promtool tsdb create-blocks-from openmetrics data.om data >/dev/null
echo "global: {}" > prometheus.yml # Prometheus refuses to start without a config file
prometheus --config.file=prometheus.yml --storage.tsdb.path=data \
--storage.tsdb.retention.time=100y --web.listen-address=127.0.0.1:9090 >prom.log 2>&1 &
for i in $(seq 100); do curl -sf 127.0.0.1:9090/-/ready >/dev/null && break; sleep 0.2; done
promtool query instant --time=2026-01-01T00:10:00Z http://127.0.0.1:9090 'http_requests_total'
promtool query instant --time=2026-01-01T00:10:00Z http://127.0.0.1:9090 'rate(http_requests_total[2m])'http_requests_total{code="302", job="linkly"} => 5400 @[1767226200]
http_requests_total{code="500", job="linkly"} => 600 @[1767226200]
{code="302", job="linkly"} => 9 @[1767226200]
{code="500", job="linkly"} => 0.9999999999999999 @[1767226200]
Three rules make this deterministic:
- the data has fixed timestamps (2026-01-01, in Unix seconds), never "now";
- every query passes
--time(or--start/--endforquery range); - the retention is long (
100y), or Prometheus would delete blocks older than its default 15 days on start.
The wait loop is bounded (for i in $(seq 100), at most 20 seconds): if the server failed to start, the session continues and you see the error instead of a hang. The output shows each series, =>, the value and @[timestamp] (1767226200 = 00:10:00). The 302s grew by 9 per second and the 500s by 1; 0.9999999999999999 is ordinary floating-point rounding inside rate(), which is why dashboards round values for display.
Recipe 3: a Loki server on fixed log lines
Loki runs as a single binary with a small config (shown in full in the Loki lesson). The session pushes log lines with fixed timestamps through Loki's HTTP push API and queries them with logcli over a fixed time range. You will see it from the Logs section on.
Recipe 4: the OpenTelemetry Collector with a file exporter
otelcol-contrib validate --config=… checks a pipeline without starting it. To run one, the session starts the Collector on localhost, sends it spans or logs as OTLP/JSON with curl, and lets a file exporter write what came out, which jq then prints. See The OpenTelemetry Collector.
What stays a sample
| Shown for real | Shown as sample sessions or files |
|---|---|
| promtool, Prometheus on backfilled data, Alertmanager + amtool, Loki + logcli, the Collector | Grafana, Kubernetes deployments (kube-prometheus-stack, the Prometheus Operator in a cluster), Grafana Alloy, Fluent Bit, OpenSearch, Jaeger and Tempo UIs, Slack and PagerDuty notifications |
| Python and Go models of rate(), histograms, quantiles, sampling and context propagation | the real Go libraries (client_golang, the OpenTelemetry SDK), which need module downloads |
Sample output is realistic and consistent with the real tools, but it was not produced by this sandbox; lessons say so where it matters.
Test yourself
Why does every promtool query instant in this course pass --time?
- It is required by promtool
- Without it the query is evaluated at the current time, which changes on every run
- It makes queries faster
- It sets the scrape interval
What does the values string '0+60x10' mean in a promtool test?
- Ten values of 60
- Start at 0 and add 60 ten times, one value per interval
- 60 samples of value 0
- A rate of 60 per second
Why are wait loops written as for i in $(seq 100); do …; done rather than until …; do sleep 1; done?
- They are faster
- A server that never starts would make an unbounded loop hang the session
- Bash has no until
- seq prints the progress