A daily stock count, and the three ways it lies

One scheduled workflow (61 lines, August 2025) counts the phone numbers left in a file-based inventory and pushes the total to a Pushgateway.

DevOps

What it does

on:
  schedule:
    - cron: '0 8 * * *'
  workflow_dispatch:

Daily at 08:00 UTC, plus a manual trigger (workflow_dispatch) for the days somebody wants the number now. It checks the repository out with the action pinned to a commit SHA and ref: master set explicitly. Then it runs the allocation tool and scrapes the figure out of stdout:

MSISDN_COUNT=$(./msisdn-calculator-linux-amd64 -remaining | grep "Remaining MSISDNs" | awk '{print $3}')

The tool prints Remaining MSISDNs %d (no colon in the format string), so awk takes field three. One gauge then goes to the Pushgateway over plain HTTP on 9091:

msisdn_stock_available{job="msisdn_stock_monitor",instance="wireless_fulfillment_scripts"}

That is the whole workflow. The calculator is a colleague's Go program and so is the inventory it reads; what I added was the cron, the gauge, and two labels.

The number is a subtraction

-remaining does one thing:

remainingCount := len(allMSISDNs) - len(usedMSISDNs)

Two directory loads (stock/ and used/), subtracted. It is not a set difference — an entry in used/ with no matching entry in stock/ decrements the total anyway, and duplicates across batch files count twice. For a stockpile that only ever gets allocated out of, in large round batches, the subtraction is close enough to be useful and wrong in a direction nobody checks.

The checkout pins ref: master, so the gauge measures stock as of the default branch. Allocations arrive as pull requests, which makes an unmerged batch invisible to the number. That is the right choice for a daily trend (and the wrong one if you are about to allocate 25,000 numbers).

The error check that does not check

curl -X POST ... "$PUSHGATEWAY_URL/metrics/job/$JOB_NAME/instance/$INSTANCE_NAME"
if [ $? -eq 0 ]; then
  echo "Successfully pushed metrics"

curl without -f exits 0 for an HTTP 500. The success branch therefore prints on any response the gateway gives. The one piece of error handling in the file reports success for a rejected push; the step fails only when curl cannot reach the host at all. The fix is one flag (--fail) and it is still not applied.

The third flaw is the Pushgateway behaving exactly as designed. A pushed gauge persists until something deletes it, so a run that never happens leaves yesterday's count sitting there looking current. A daily cron needs a staleness check alongside it — time() - push_time_seconds for the job — or the metric is worth least on the day it matters most.

What it was worth

One file, 61 lines, untouched since (one commit, 14 August 2025). It turned a question somebody had to go and ask into a line on a graph, which is most of the value of this kind of automation and all of the value here. The defects are small, real, and cheaper to list than to dress up: a missing --fail, a subtraction standing in for a set difference, and no staleness alert.