Course contents · Lesson 21 of 21

Capstone: Debug a Broken App with Open-Source Tools

Putting It Together · Lesson 21 of 21 · 11 min Hands-on

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.

Flowchart in eight steps. 1: the alert fires in Alertmanager on port 9093, two checkout alerts. 2: Grafana on port 3000 shows which signal broke, errors and p95, since the deploy. 3: traces in Jaeger on port 16686 show the slow span, coupon.find_best. 4: logs in Elasticsearch show the error pattern, no answer within 150 ms. 5: the profile in Pyroscope on port 4040 shows the hot function, check_value. 6: fix one cause in the compose file or app.py, then redeploy only that. 7: verify with ./check-fix.sh, then watch the dashboards. Decision PASS? No: fix the next cause, back to step 6. Yes: 8, write it up, including what would catch it sooner.
Each step narrows the question, and the loop allows one fix per pass.

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.

Evidence board with three pinned clue cards. Clue 1, a trace in Jaeger: POST /checkout 0.80 s with inventory.lookup 40 ms, coupon.find_best 0.62 s highlighted and payment.charge 0.12 s; the slow span has no children. Clue 2, a log line found in Elasticsearch with level:error: level error, msg checkout failed, status 500, error payment provider did not answer within 150 ms; every failed checkout says this. Clue 3, a flame graph in Pyroscope: total, Flask frames, checkout, best_coupon, is_valid and check_value, the widest box with nothing below. Threads from clues 1 and 3 lead to cause A, slow checkouts, a code fault: best_coupon() checks all 500,000 coupons on every checkout and only 20 are valid; fix: loop over ACTIVE_COUPONS. A thread from clue 2 leads to cause B, failed checkouts, a config fault: PAYMENT_TIMEOUT_MS is 150 while the provider answers in 50 to 200 ms; fix: set it to 2000. Durations illustrative.
Two clues convict the coupon code and one convicts the timeout: two symptoms, two causes, two fixes.

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.

Timeline in minutes after the 1.1.0 deploy, times illustrative. Top strips: the checkout error ratio sits near 34 percent until fix 1 at minute 12, then falls to zero over 5 minutes; checkout p95 sits near 1 second until fix 2 at minute 15, then falls to about 0.25 s by minute 20; dashed lines mark the 5 percent and 0.4 s limits. Events: 0, deploy 1.1.0 with compose up; 2, alerts fire, Alertmanager; 4, which signal, Grafana; 6, slow span, Jaeger; 8, timeouts, Elasticsearch; 10, hot function, Pyroscope; 12, fix 1, the timeout, check-fix FAIL; 15, fix 2, the coupons, check-fix PASS; 20, alerts resolved, dashboards green. Phases: detect, investigate, fix and verify, recover.
check-fix.sh confirms each fix at once; the 5-minute windows make the graphs catch up later.

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.sh against a test copy before release; a canary, a small share of traffic on the new version, compared by service.version in traces and the version tag 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