I built a URL shortener for my capstone project and added Prometheus to collect metrics. I took over the Grafana part of the project, the dashboards and the alerts. Then the alerts stopped working. The cause was smaller than I expected.

What went wrong

When I opened Grafana on my localhost, it showed the metrics as unhealthy and no data was coming back from Prometheus. The alert rules failed with "data source not found".

How I found it

I figured the problem was the ID, because that ID is the link between Grafana and the metrics. Grafana had created the Prometheus data source with an auto-generated ID, so the alert rules couldn't find it.

The fix

I set a fixed ID in grafana/provisioning/datasources/prometheus.yml, so it doesn't change every time I restart the project.

How I checked it

I waited a few minutes, then checked again, and the metrics arrived. I also checked the metrics in Prometheus and compared them, because Grafana is just the UI and the real data comes from Prometheus.

Two more alert problems

The ID was not the only alert problem I hit in that project.

The alert that never fired

My latency alert was set to fire when P95 latency went over 100 ms. During a load test, Prometheus showed 520 ms, well over the threshold. The alert stayed Normal, green, and no notification came.

The rule had a for: 2m condition, so the problem has to last two minutes before the alert fires. My load test started at 00:00 and I stopped it at 02:00. At 01:00 the condition was met and the alert went to Pending. At 02:00 the load stopped and P95 dropped to 45 ms. At 02:15 the next check found the condition gone, so the alert went back to Normal.

The alert was working as designed. The test was too short. I changed the load test to run for 4 minutes, twice the alert duration.

The label that did not match

An alert sat in Pending for over 10 minutes and never went to Firing, even though the condition was clearly met in Prometheus. The query was returning no data. One cause was a label mismatch: the alert rule asked for route="/api/shorten", and the metric was labelled route="/shorten". Different name, so the query found nothing.

I checked the real labels on the backend with curl http://localhost:3000/metrics | grep http_request_duration_seconds, and changed the rule to the label the metric actually had. I also added OR on() vector(0) to the query, so an empty result returns 0 instead of leaving the alert pending.

What I learned

When a problem takes me too long, the cause is often something small and easy that I missed, because I was busy thinking about complicated, hard explanations.