BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / MLOps26 of 26 in topic

796. On-call tracing: find the root-cause span of a failed request

MediumMLOps Pro

A checkout request failed. Your distributed tracing (OpenTelemetry style) recorded one span per service call, and as usual the error bubbled up: the gateway, the order service and the payment client all report an error, but only the deepest one in that chain is the real cause. The on-call engineer needs that span, fast.

Write root_cause(spans). Each span is a dict with keys 'id', 'parent' (None for the root), 'service', 'start' and 'error' (bool). Return [id, service] of the root-cause span, or None if the root span has no error.

Rule: start at the root. While the current span has at least one child with an error, move to the erroring child with the smallest start time (ties: smallest id). The span where you stop is the root cause.

Real traces are messy:
- Spans arrive in any order, not parent first.
- Some spans have a parent that was never exported (sampling dropped it). Ignore them.
- A child can fail and be retried successfully, so a failed span can sit under a span with no error. Only follow the error chain from the root: an error under a healthy span is not the cause.
- Traces can be thousands of spans deep (long agent loops), so avoid recursion that hits Python's recursion limit.
- There is exactly one span with parent None.

Functiondef root_cause(spans)

Example 1

Input: spans = [{'id': 1, 'parent': None, 'service': 'gateway', 'start': 0, 'error': True}, {'id': 2, 'parent': 1, 'service': 'auth', 'start': 1, 'error': False}, {'id': 3, 'parent': 1, 'service': 'search', 'start': 2, 'error': True}, {'id': 4, 'parent': 3, 'service': 'index', 'start': 3, 'error': True}]

Output: [4, 'index']

Example 2

Input: (root_cause(T2), root_cause(T1))

Output: (None, [4, 'index'])

Constraints

  • At most 10,000 spans.
  • Exactly one span has parent None.
Hint 1

Map parent id to list of children.

Hint 2

Pick the erroring child by (start, id).

Hint 3

Stop when no child has an error.

Common mistakes
  • Returning the first erroring span in list order instead of walking the tree.
  • Following non-erroring children.
  • Crashing on orphan spans.
Understand the idea

A distributed trace is a tree: the root span is the user request and each child is a call to another service. Errors bubble up, so a timeout deep in an index service makes search fail, which makes the gateway fail. The span that matters is the deepest erroring one whose own children all succeeded. Walk down from the root, always following an erroring child, and stop when none is left. In the example gateway, search and index all error; index has no erroring children, so it is the root cause. On-call engineers do this by eye in tracing dashboards every day.

Topics
MLOps
Similar questions
  • Catch train-serving skew in feature pipelinesMedium
  • Drive a staged canary rollout from per-window error ratesMedium
  • Pick a champion model with guardrail metricsMedium
  • Measure feature drift with smoothed KL divergence on quantile binsMedium
All MLOps problems
Report
796 / 903
Python 3solution.pyPython idle
1
2
3
4
def root_cause(spans):
# return [id, service] or None
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS