Publishing a metric for a number pool that lives in git

The available-MSISDN count is a subtraction over two directories of text files, so the only way to put it on a dashboard was to run the tool on a schedule and push the answer.

DevOps

The pool is a directory, not a table

MSISDNs for SIM batches live in a repository. msisdns-for-sim-batch/stock/ holds the ranges as they were bought (six files, one per purchase ticket). used/ holds one file per allocated batch, and allocating numbers means running a small Go binary and committing the file it writes. That is why the README opens with git pull and closes with COMMIT AND PUSH, in capitals — two people allocating against a stale checkout hand out the same numbers twice.

"How many are left" is a flag on that same binary (-remaining, one line of output). Reading it takes three things — a checkout, a Linux box, and a reason to care that morning. Nobody was going to do that daily. Running out arrives as a step, never as a slide: a fresh batch takes weeks to become usable numbers, so the warning has to come tens of thousands of numbers early.

One workflow, sixty-one lines

The whole thing is one scheduled job (.github/workflows/msisdn-monitoring.yml, 14 August 2025, a single commit). Daily cron at 08:00 UTC, plus workflow_dispatch so it can be run by hand. It checks out, runs -remaining, lifts the count out with awk '{print $3}', and pushes one sample:

METRIC_DATA="msisdn_stock_available{job=\"$JOB_NAME\",instance=\"$INSTANCE_NAME\"} $MSISDN_COUNT"
curl -X POST -H "Content-Type: text/plain" --data-binary "$METRIC_DATA" \
  "$PUSHGATEWAY_URL/metrics/job/$JOB_NAME/instance/$INSTANCE_NAME"

A Pushgateway, because the job only lives for about twenty seconds a day. Nothing can scrape it in that window. The gateway keeps the last value it was handed, so the series is flat between runs; a missed run looks like a steady stock. The quantity moves in steps of thousands, days apart, which is the only reason that trade is acceptable here.

actions/checkout is pinned to a commit SHA (692973e…, the commit behind the v4.1.7 tag). The step also pins ref: master, so the published figure is that branch's figure. An allocation sitting in an open pull request is invisible to it.

The label names change under you

The threshold lives in the alerts repo, and its expression cannot use the labels the workflow set:

min(msisdn_stock_available{job="pushgateway",
    exported_instance="wireless_fulfillment_scripts",
    exported_job="msisdn_stock_monitor"}) <= 50000

The gateway is itself a scrape target. Prometheus stamps its own job="pushgateway" on everything it collects, and renames the pushed labels to exported_job and exported_instance. Write the obvious selector (job="msisdn_stock_monitor") and it matches nothing at all. The rule fires after five minutes below fifty thousand (into an ops channel, with a link to the runbook for requesting a fresh batch).

Two things it still gets wrong

curl is called without --fail. It exits 0 on a 4xx or a 5xx; the success branch runs regardless. The tick in the log means the connection succeeded, and nothing more than that.

The count itself is len(allMSISDNs) - len(usedMSISDNs), a subtraction of two list lengths. The allocation path a few lines up builds a set of used numbers to skip, which is the honest version. The two disagree whenever used/ holds an entry that stock/ never did (a returned batch, a legacy file). The metric then reads low (short by exactly the size of that stray file). The alert only triggers on low, so that is the safe way to be wrong — luck, and nothing cleverer.