# The hive-matrix container's database-size step: measures a directory and # pushes the byte count as the gauge `hyperhive.matrix.database_bytes` to the # forwarder's OTLP/HTTP metrics receiver on loopback. # # `dir` is a `DynamicUser` unit's `/var/lib/`, which systemd makes a # symlink to `/var/lib/private/`. `--dereference-args` measures the # directory behind the link and works unchanged when `dir` is a real # directory; without it `du` reports the link itself (a few bytes). # # A result under 4096 bytes is not a database, since a directory entry alone # is that big: the step warns on stderr and pushes nothing, so the gauge # stays absent rather than wrong. # # Pure function — NOT a NixOS module. Needs `du`, `cut`, `date` and `curl`. { lib }: { dir, otlpPort }: '' set -euo pipefail bytes="$(du --summarize --bytes --dereference-args ${lib.escapeShellArg dir} | cut -f1)" case "$bytes" in "" | *[!0-9]*) echo "du answered a non-number: bytes='$bytes'" >&2 exit 1 ;; esac if [ "$bytes" -lt 4096 ]; then echo "warning: ${dir} measures $bytes bytes, too small for a database; pushing nothing" >&2 exit 0 fi printf '{"resourceMetrics":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"hive-matrix"}}]},"scopeMetrics":[{"metrics":[{"name":"hyperhive.matrix.database_bytes","gauge":{"dataPoints":[{"asInt":"%s","timeUnixNano":"%s"}]}}]}]}]}' \ "$bytes" "$(date +%s%N)" | curl --fail --silent --show-error --max-time 20 \ -H 'Content-Type: application/json' --data-binary @- \ http://127.0.0.1:${toString otlpPort}/v1/metrics ''