Engineering note · Flexi-Ride
Prometheus alerts with unit tests
- Project
- Flexi-Ride — Internal project, Zone01 Kisumu
- Issue
- #89 OPS-005: Set up Prometheus and Grafana monitoring dashboards
- My role
- Author of PR #219
- Stack
- Prometheus · Grafana · Alertmanager · Micrometer · Docker Compose
- Status
- Open, in review
The problem
After OBS-001, every Flexi-Ride application exposed /actuator/prometheus. Nothing useful was built on top of it yet. The Prometheus config listed 15 hand-written jobs, there were only three alert rules, and there were no dashboards. The issue asked for alerts on error rate, downtime, p99 latency and disk space, four Grafana dashboards, Slack and email alerting, and 15-day retention, all runnable with docker-compose.
What I did
I replaced the 15 static jobs with one job that uses file-based service discovery. Targets live in monitoring/prometheus/targets/flexiride.yml with service and tier labels. A relabel rule turns service into job. Prometheus re-reads the file every 30 seconds, so adding a service doesn't need a restart.
- 12 alert rules in five groups: availability, errors, latency, resources, and host. The HTTP rules ignore
/actuatortraffic, so frequent scrapes don't dilute the error ratio of a quiet service. The gRPC error rule only counts server-side codes such asINTERNALandUNAVAILABLE, notNOT_FOUND. - Latency histograms turned on in every application, so the p99 rules have buckets to work with.
- Four provisioned dashboards: service health, latency and errors, business metrics, and infrastructure.
- Business counters in booking and auth, for bookings placed, status changes, completed fare value, registrations and logins. Booking counters only increment after the database transaction commits, so rolled-back bookings are never counted.
- Alertmanager config rendered from environment variables. Critical alerts go to Slack and email, warnings to Slack. Each channel is only enabled when its variables are set.
One detail took some thought. Every counter is registered at zero on startup. Prometheus increase() ignores the first sample of a new series, so a counter created lazily by its first event would never count that event.
I also fixed audits along the way. Spring Boot 4 moved Flyway auto-configuration into spring-boot-starter-flyway, so audits never ran its migrations and couldn't start under docker-compose. That also blocked both gateways.
How I checked it
The alert rules have unit tests in monitoring/prometheus/tests/alerts.test.yml. Each test feeds synthetic series through the real rules and checks that the alert fires above its threshold and stays quiet below it.
# booking: 10% of API calls fail. Heavy actuator traffic
# would dilute that to under 1% if it were counted.
- series: 'http_server_requests_seconds_count{job="booking", uri="/actuator/prometheus", status="200"}'
values: '0+1000x40'
make lint-monitoring runs promtool check config and promtool test rules. It is part of make lint, make ci-local and a new CI step. In a full compose run, all 15 targets were up. Stopping leads fired ServiceDown after the two-minute window. Alertmanager sent it to local stand-in Slack and SMTP receivers, then sent a resolved message when leads came back.
The first review asked for payment metrics, which the issue lists. I added PaymentMetrics counters and dashboard panels. The payment service has no payment flows yet, so recording real outcomes depends on a separate payment issue. The PR is still in review.
What I learned
Alert rules are code, and they can be wrong in quiet ways. Without the actuator exclusion, the booking test case would never have fired, because 1,000 actuator requests per step hide a 10% failure rate. A dashboard would not have shown me that. A promtool test with a deliberately skewed series did.