Every five minutes, a number travels from a running service to a file I can analyse, along the path above. For three months, a third of those numbers fell off it and nothing noticed.
I am building infra-brain, a tool that answers questions about my infrastructure, and part of that is learning to build a model that spots anomalies in it - my indirect road into AI infrastructure. A model needs history, so I built a small cluster on Oracle Cloud Infrastructure (OCI) - one Arm virtual machine running k3s, a lightweight Kubernetes - that records how its twelve services behave under traffic and under faults I inject on purpose. I ran my first PCA on three months of that data in the companion post. This post is about how the dataset is built, and the bug that turned up by doing nothing cleverer than counting.
Why the data has to leave Prometheus#
Prometheus records the metrics: on a schedule it asks the cluster how much CPU, memory and network each container uses. Mine keeps 90 days, then deletes the oldest - fine for dashboards, fatal for machine learning, which wants all the history it can get.
So a collector runs hourly as a Kubernetes CronJob, copying recent metrics into Parquet files - a compact table format pandas reads directly - in OCI Object Storage, where nothing expires. Three choices shape the files:
| Choice | Why |
|---|---|
| One file per hour, rewritten whole each time | Running the job twice gives the same result, so a retry is always safe |
Long format: one row per time, service, pod, metric, value | A new metric adds rows, not columns, so old files never need rewriting |
Pseudonymised names like svc_4a5b72045f2f | Notebooks and charts can be published without a review pass first |
The faults in it are on purpose#
The cluster is not left alone. Chaos Mesh, a fault-injection tool for Kubernetes, runs eight schedules - about 16 faults a week across seven types: container kill, pod made unready, memory pressure, CPU saturation, network delay, packet loss and DNS failure. A recorder logs each with its start, end and target, and checks which alerts fired. So the data has labelled anomalies in it by design, and a model can be graded against what really happened. Killing a whole pod fires no alert, so I do not schedule it.
The part I got wrong: “writing twice is harmless”#
Each run asks for the last two hours, so a skipped run is covered by the next. Every hour is written twice, and since each file is rewritten whole, I reasoned the second write would replace the first with the same thing.
It is only harmless if both writes are complete. The job runs at 23 minutes past the hour, and Prometheus lines its five-minute samples up with the start of the window you ask for - not with the clock. Follow hour 13:00 through two runs:
| Run at | Asks for | Hour 13 gets | |
|---|---|---|---|
| 14:23 | 12:23 to 14:23 | samples at 13:03, 13:08 … 13:58 - all 12 | complete |
| 15:23 | 13:23 to 15:23 | samples at 13:23 … 13:58 - only 8 | written last, so it wins |
Every hour was written complete, then overwritten with only its last eight samples, :23 to :58. The partial copy always survived.
How it was found: a routine count#
Nothing failed. Every job succeeded and every file existed. The lab has a verifier that warns when a source goes quiet or drops below its usual row count - but a source a third short every day never drops below its own usual. A consistent loss looks exactly like health.
It turned up while I was building the PCA matrix, using Claude to walk me through it step by step. Claude counted five-minute buckets per day as a routine check. A day has 288; every day I had came out at 192, exactly two thirds, with no sample in the first 20 minutes of any hour. Counting first is the habit I took from it.
The fix, and the safety net I forgot I had#
The collector now asks for whole completed hours only, aligned to :00, so both writes of an hour
are complete. Two guards came with it: a file is never overwritten with fewer rows than it already has,
and long backfills are split into one-day requests, because Prometheus refuses more than 11,000 points
per series in one request.
The backfill rewrote 2,160 hours, 1,556,511 rows - every hour Prometheus still remembered - so everything from 2026-08-23 onwards was complete.
The first six days were older than Prometheus’s 90 days, and I wrote them off as gone for good. Checking that claim before publishing saved them. I had turned on versioning when I set up the storage, so every overwrite keeps the previous copy. The complete first write of every hour was still there, one version back.
A small script compared every version of each hour and promoted the most complete, after a dry run that changed nothing. It restored 118 hours:
| Dates | Five-minute buckets per day, before | After |
|---|---|---|
| 2026-08-17 | 106 | 154 - complete from the first sample, at 11:13 |
| 2026-08-18 to 2026-08-21 | 192 | 288 |
| 2026-08-22 | 248 | 288 |
Nothing was lost in the end. One version was not even Parquet - an earlier restore drill wrote a corrupt copy on purpose - so check every version rather than trusting the newest.
What I would tell you if you are building one#
- Count rows per time bucket before you trust any chart. Shape is easy to fake; counts are not.
- “Idempotent” is not “correct”. Rewriting a file is only safe if every version of it is complete.
- Know where your source aligns its timestamps. Prometheus aligns to your query, not the clock.
- Turn on versioning, and remember that you did. Retention is a deadline; a versioned bucket let six of my days survive past it.
What I do not know yet#
I counted the other sources the same way after the fix. Alerts are complete. Chaos events are complete except Sundays, when nothing is scheduled. The load log had a gap that its backfill closed. Deploys stop on 11-04 because a stale pull request blocked the deploy bot; it is closed now, but that hole needs accounting for before I train on it. I have also not checked that the chaos timestamps line up with the metrics, which I must do before trusting them as labels.
If you are further along and something above is wrong, please tell me. I will be glad to hear from you if you have any question(s) or feedback, have fun!