Capstone: Debug a Broken App with Open-Source Tools
Release 1.1.0 of the shop went live a few minutes ago, and support forwards a complaint: "checkout feels slow and sometimes fails." Restarting things until the complaints stop would hide the problem, not explain it. In this capstone you debug a broken app with the whole lab: the alert and dashboard say what broke, then traces, logs and a profile say why. You fix one cause at a time and prove each fix.
The complaint, and a lab that reproduces it
The lab course-labs/open-source-observability-stack/08-capstone-broken-app starts everything you built in this course around shop-demo 1.1.0, with the problems already in it. The shop is lesson 14's Flask app sending all four signals: spans through the Collector to Jaeger, metrics to Prometheus, JSON logs through Filebeat and Logstash to Elasticsearch, and CPU profiles to Pyroscope.
Prometheus evaluates two new alerting rules, CheckoutErrorsHigh (more than 5% of checkouts fail) and CheckoutLatencyHigh (checkout p95 above 0.4 s), and sends them to lesson 8's Alertmanager. Grafana loads lesson 10's Shop Overview dashboards. The lab's README lists what changed in 1.1.0: a coupon on every order, and a payment timeout read from a new setting. After a deploy, "what changed?" is always a good question.
Stop the other labs, give Docker 6 to 8 GB of memory, and start this one:
$ cd course-labs/open-source-observability-stack/08-capstone-broken-app
$ docker compose up -d
$ docker compose ps
The first start builds and downloads images for several minutes. You should then see 13 services Up, with shop-demo, prometheus, alertmanager and elasticsearch marked (healthy). "Port is already allocated" means another lab still runs. The flowchart is your plan. Notice that each step asks a narrower question than the one before.
Step 1: which signal broke, and since when
After two minutes, open http://localhost:9093. Alertmanager lists both CheckoutErrorsHigh and CheckoutLatencyHigh. That answers the first question: errors and latency are both wrong, so expect up to two causes.
Open http://localhost:3000 (admin / admin), then Dashboards > Shop Overview (improved), and set Route to /checkout. The error ratio is red, around a third of checkouts, and p95 is near 1 second, depending on your CPU. Pick /products and both look healthy: the problem lives in checkout.
Since when? Each graph begins when 1.1.0 started, already bad: there was never a good minute, so the fault came with the deploy. Requests per second sit below loadgen's 5 too: it waits for each answer before sending the next.
Steps 2 to 4: traces, logs and the profile
Step 2: traces find the slow step
In Jaeger at http://localhost:16686, search as in lesson 15: Service shop-demo, Span Name POST /checkout (method plus route; loadgen sends POST), Min Duration 500ms. Open the top trace. Between inventory.lookup and payment.charge sits a span that is new in 1.1.0, coupon.find_best, filling most of the request with no children. The trace has found the slow step, but not the code inside it.
Step 3: logs explain the failures
In Grafana's Explore, pick the Elasticsearch data source, the Logs query type and the Lucene query level:error, as in lesson 19. Every line says checkout failed with the same error field: payment provider did not answer within 150 ms. One message repeated on every failure is a pattern, not noise.
Now compare that limit with the provider. charge_payment() in shop-demo/app.py says it answers in 50 to 200 ms, and docker-compose.yml sets PAYMENT_TIMEOUT_MS to "150". A 150 ms limit cuts off about a third of normal answers. A failed checkout's trace agrees: payment.charge ends in status Error after 150 ms.
Step 4: a profile names the hot function
Open Pyroscope at http://localhost:4040 and choose the service shop-demo and the process_cpu profile type. As in lesson 20, find the widest box with nothing below it: check_value, called by is_valid, called by best_coupon.
In app.py, best_coupon() loops over COUPONS, all 500,000 codes ever issued, and checks each one on every checkout. ACTIVE_COUPONS, the 20 valid codes found once at start-up, sits unused. On the evidence board, notice that the clues point at two causes: the trace and the flame graph at the code, the log line at the configuration.
Treat one cause at a time, then confirm
Change one thing, redeploy, measure, then change the next. If you change both at once and something new breaks, you cannot tell which change did it. In production, rolling back 1.1.0 is often the fastest way to stop the harm, and the stores keep the evidence. Here you fix forward, repairing 1.1.0 in place, starting with the failures: a failed order loses a sale.
Fix 1 is configuration: set the timeout back to the 2 seconds 1.0.0 used. docker compose restart would keep the old environment; up -d recreates a service whose configuration changed. Then run the check script, which sends 40 checkouts and tests the alerts' two limits on them at once, without waiting for the dashboards:
PAYMENT_TIMEOUT_MS: "2000" # in docker-compose.yml, was "150"
$ docker compose up -d shop-demo
$ ./check-fix.sh
Sending 40 checkouts to http://localhost:8000 ...
errors: 0 of 40 checkouts failed (limit 2): ok
latency: p95 0.871 s (limit 0.4 s): FAIL
FAIL
Your numbers will differ; the verdicts should match. "Permission denied" means the file has no executable bit: run chmod +x check-fix.sh, or bash check-fix.sh. Fix 2 is code: in best_coupon(), loop over the valid coupons only. It finds the same coupon among 20 codes, and a restart loads the mounted code:
for code in ACTIVE_COUPONS: # in shop-demo/app.py, was COUPONS
$ docker compose restart shop-demo
$ ./check-fix.sh
...
latency: p95 0.236 s (limit 0.4 s): ok
PASS
Then confirm on the dashboards. Their queries look back 5 minutes, so the error ratio and p95 fall over about five minutes, with an orange "shop-demo restarted" marker for each redeploy. Alertmanager drops both alerts, and docker compose logs webhook shows status=resolved.
Write a short postmortem
A postmortem is a short written account of an incident, so the team learns from it. Keep it blameless: describe what the system and the process allowed, not who typed the number. The timeline is the lab's incident with the tool of each step. Notice the lag after each fix: graphs and alerts trail check-fix.sh by up to five minutes.
Four parts are enough:
- Timeline: deploy, alert, each finding, each fix and the recovery, with times.
- Root causes: a 150 ms payment timeout, below the provider's normal answer time, failed a third of checkouts;
best_coupon()re-checked 500,000 coupons on each one. - Fixes: the timeout back to 2000 ms, and the loop over
ACTIVE_COUPONS. - Caught sooner by: running
check-fix.shagainst a test copy before release; a canary, a small share of traffic on the new version, compared byservice.versionin traces and theversiontag in profiles; and a payment-timeout counter with its own alert, so that the alert names the cause.
Check yourself
After the timeout fix, check-fix.sh prints errors ok and latency FAIL. Did the fix fail?
No. It fixed its own cause, the failures. Latency has a second, separate cause, the coupon loop, which is why you fix and measure one cause at a time.
Right after fix 2, check-fix.sh prints PASS, but the p95 panel is still high and the alert still fires. Is the fix wrong?
No. The panel and the rule look back 5 minutes, which still include slow checkouts from before the fix. The script measures only new requests, so wait about five minutes.
check_value is the hot box in the flame graph. Why change the loop in best_coupon() rather than make check_value faster?
It is hot only because best_coupon() calls it for 500,000 codes per checkout. Removing almost all the calls saves far more than speeding up each one.
Go deeper
- Incident Management: Post-Mortem Culture That Works: how to run blameless postmortems like this one in a real team.