Code: Metrics with Jennifer: Prometheus, StatsD and InfluxDB
Metrics with Prometheus, StatsD and InfluxDB: the code
The programs behind the slides, for Jennifer 0.27.0. They import the decks @deckyard/prometheus, @deckyard/statsd and @deckyard/influxdb, listed in deck.toml, so run jvc install here first to fill vendor/. Run everything from this folder. The servers are throwaway containers bound to localhost; stop them with docker rm -f metrics-prom metrics-pushgw metrics-statsd metrics-influx.
Prometheus and StatsD
docker run --rm -d --name metrics-prom --network host -v "$PWD/prom:/etc/prometheus:ro" prom/prometheus:v3.5.0 --config.file=/etc/prometheus/prometheus.yml --web.listen-address=127.0.0.1:9090
docker run --rm -d --name metrics-pushgw -p 127.0.0.1:9091:9091 prom/pushgateway
docker run --rm -d --name metrics-statsd -p 127.0.0.1:9102:9102 -p 127.0.0.1:9125:9125/udp prom/statsd-exporterPrometheus uses host networking so it can scrape the shop on 127.0.0.1:8080; prom/prometheus.yml scrapes the shop, the Pushgateway and the statsd_exporter, and prom/rules.yml holds the alert rule.
shop.j: a small HTTP service on127.0.0.1:8080with a counter, a histogram and an info gauge, served on/metricsfrom the same accept loop ("A counter in a value-semantic deck", "The shop's three metrics", "Serving /metrics from one httpd loop"). Run:jennifer run shop.j(stays running)load.j: sends about 75 seconds of traffic to the shop; one checkout in ten fails ("Prometheus scrapes the shop"). Run:jennifer run load.jup.j: the queryup, one line per scrape target ("Prometheus scrapes the shop"). Run:jennifer run up.jquery.j: request rate by status and the 95th percentile latency by route ("PromQL from Jennifer: rate and quantiles"). Run:jennifer run query.jbackup.j: a short batch job that pushes three gauges to the Pushgateway ("Batch jobs: push to a Pushgateway"). Run:jennifer run backup.jfiring.j: the firing alerts and the age of the last backup ("From metrics to alerts"). Run it after the shop has had a minute ofload.jtraffic:jennifer run firing.jstatsd_listen.j: a stand-in StatsD agent on UDP127.0.0.1:8125that prints every datagram ("StatsD: one UDP line per event"). Run:jennifer run statsd_listen.j(stays running; needs no container)statsd_worker.j: a queue worker that sends counters, timers, sampled counts and a gauge. Without arguments it sends three jobs to127.0.0.1:8125(the listener above);jennifer run statsd_worker.j 127.0.0.1:9125 20sends twenty jobs to the statsd_exporter ("StatsD: one UDP line per event", "StatsD into Prometheus, and fire-and-forget"). Point it at a port where nothing listens to see that it does not fail.statsd_batch.j: three metrics in one datagram to127.0.0.1:8125. Run:jennifer run statsd_batch.jprom_errors.j: an invalid metric and label name, a query Prometheus refuses, a stopped Prometheus (port 9999) and an invalid StatsD name ("What Prometheus and StatsD throw"). Run:jennifer run prom_errors.j
InfluxDB
docker run --rm -d --name metrics-influx -p 127.0.0.1:8086:8086 -e DOCKER_INFLUXDB_INIT_MODE=setup -e DOCKER_INFLUXDB_INIT_USERNAME=demo -e DOCKER_INFLUXDB_INIT_PASSWORD=demo-password -e DOCKER_INFLUXDB_INIT_ORG=demo -e DOCKER_INFLUXDB_INIT_BUCKET=shop -e DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=demo-token-not-secret influxdb:2.7The programs use the organisation demo, the bucket shop and the placeholder token demo-token-not-secret written into them.
influx_lines.j: building points and printing their line protocol; needs no server ("InfluxDB: one line per point"). Run:jennifer run influx_lines.jinflux_ts.j: a timestamp in nanoseconds and one wrongly given in seconds, read back with InfluxQL ("Timestamps and batches"). Run:jennifer run influx_ts.jinflux_worker.j: writes 2000 job points spread over the last ten minutes, in batches of the given size ("Timestamps and batches"). Run:jennifer run influx_worker.j 1(one write per point), thenjennifer run influx_worker.j 500influx_flux.j: the 95th percentile duration per queue with Flux, parsed with thecsvmodule ("Flux from Jennifer: exact quantiles"). Run after the worker:jennifer run influx_flux.jinflux_ql.j: the same data through the 1.x client and InfluxQL ("Flux from Jennifer: exact quantiles"). Run:jennifer run influx_ql.jinflux_card.j: 2000 points with an id as a tag, then the series count of both measurements ("Tags, fields and cardinality"). Run:jennifer run influx_card.jinflux_errors.j: a wrong token, a misspelt bucket, a field type conflict, a point without fields, a newline in a tag and a stopped server (port 8099) ("What InfluxDB throws"). Run after the worker:jennifer run influx_errors.jinflux_partial.j: a batch with one bad point throws, yet the good point was stored ("What InfluxDB throws"). Run:jennifer run influx_partial.j
| File | Size | |
|---|---|---|
| LICENSE | 899 B | raw |
| README.md | 4.9 KiB | raw |
| backup.j | 1.2 KiB | raw |
| deck.toml | 314 B | raw |
| firing.j | 673 B | raw |
| influx_card.j | 1.0 KiB | raw |
| influx_errors.j | 1.3 KiB | raw |
| influx_flux.j | 673 B | raw |
| influx_lines.j | 477 B | raw |
| influx_partial.j | 775 B | raw |
| influx_ql.j | 548 B | raw |
| influx_ts.j | 980 B | raw |
| influx_worker.j | 1.6 KiB | raw |
| load.j | 585 B | raw |
| prom/prometheus.yml | 369 B | raw |
| prom/rules.yml | 358 B | raw |
| prom_errors.j | 1.2 KiB | raw |
| query.j | 812 B | raw |
| shop.j | 3.1 KiB | raw |
| statsd_batch.j | 463 B | raw |
| statsd_listen.j | 443 B | raw |
| statsd_worker.j | 993 B | raw |
| up.j | 388 B | raw |