Repository navigation
feat(telemetry): add sampling and validator tracing spans via hooks - #1488
Conversation
Add SamplingTracingPlugin and ValidationTracingPlugin, emitting `sampling` and `validation` application spans through the existing sampling and validation lifecycle hooks. - Add observe-only correlation-id fields (`sampling_id`, `validation_id`) to the sampling/validation hook payloads, minted at the dispatch sites so the paired open/close hooks correlate. - Add `start/finish_sampling_span`, `start/finish_validation_span`, and a generic `add_span_event` (renamed from `add_streaming_event`) to tracing.py; iterations and repairs are recorded as span events on the sampling span. - Fire the sampling/validation end hooks on the error path (via try/finally with an optional `exception` field) so a raised loop/check still closes its span with ERROR status; a routine budget-exhaustion or failing check closes with default status. Metrics and builtin_debug plugins skip the outcome on the exception path. - Document the new spans and corrected span hierarchy in the observability docs. Assisted-by: Claude Code Signed-off-by: Alex Bozarth <[email protected]>
jakelorocco
left a comment
There was a problem hiding this comment.
lgtm; two non-blocking questions / concerns
| results=rvs, | ||
| all_validations_passed=exception is None and all(bool(r) for r in rvs), | ||
| passed_count=sum(1 for r in rvs if bool(r)), | ||
| failed_count=sum(1 for r in rvs if not bool(r)), |
There was a problem hiding this comment.
Just confirming the docstring is correct about results / counts being None/0 when exceptions are raised. These will be 0 / None since we are using asyncio.gather, so any exception will shortcircuit the for loop, right?
There was a problem hiding this comment.
Confirmed — the docstring is correct. On the exception path results is [] and both counts are 0, and it's independent of which coroutine failed or when. await asyncio.gather(*coroutines) resolves to a single list before the for loop runs even once, so with the default return_exceptions=False the first raise propagates out of the await and the loop body never executes — nothing gets appended to rvs. The counts aren't left at their field defaults; they're actively recomputed in the finally as sum(...) over an empty rvs, which just lands back on 0. all_validations_passed is additionally guarded by the explicit exception is None check.
| if exception is not None: | ||
| end_payload = SamplingLoopEndPayload( | ||
| sampling_id=sampling_id, | ||
| strategy_name=type(self).__name__, | ||
| success=False, | ||
| exception=exception, | ||
| ) |
There was a problem hiding this comment.
Would it be worthwhile to define slices earlier so that even with an exception we can determine the number of iterations used? I think that might be helpful information but I can't think of a real use case right now...
There was a problem hiding this comment.
Even if we hoisted slices to report len(slices) here, it wouldn't be worth much on the exception path. Under concurrency it's a race-dependent snapshot, not a count: when one producer raises we cancel the others, so the value is however many slices happened to be drained before cancellation settled — same crash, different number each run. And on this path the exception object is the diagnostic, not the iteration count; "3 ran before it crashed" doesn't tell you why. The count is a meaningful signal on the success/budget-exhausted paths, which is exactly where it's already populated.
bd2005f
Pull Request
Issue
Fixes #1050
Description
Adds
samplingandvalidationtracing spans, emitted by two new plugins (SamplingTracingPlugin,ValidationTracingPlugin) that subscribe to the sampling and validation lifecycle hooks already dispatched byBaseSamplingStrategy.sample()andavalidate().#1050 also named the formatter, which is intentionally left out: it's a synchronous render step with no duration or place in the trace tree of its own, so it isn't a meaningful span. The data worth capturing from that path — the prompt template and its variables — is component-level information the formatter merely renders, and it belongs as attributes on the backend
chatspan, which is #1067's scope (llm.prompt_template.*).Two new plugins in
mellea/telemetry/tracing_plugins.py:SamplingTracingPlugin— emits asamplingapplication span per sampling loop, opened onsampling_loop_startand closed onsampling_loop_end. Each attempt and repair is recorded as a span event (iteration/repair) rather than a child span, so the markers stay on the one span alongside the backendchatspans the same calls emit.ValidationTracingPlugin— emits avalidationapplication span per requirement-validation batch, opened onvalidation_pre_checkand closed onvalidation_post_check.Supporting changes:
sampling_id/validation_idfields to the sampling and validation hook payloads, minted once at each dispatch site (sample()andavalidate()) so the paired open/close hooks correlate.tracing.py:start/finish_sampling_span,start/finish_validation_span, and a genericadd_span_event(renamed from the streaming-specificadd_streaming_event, now shared).sample()andavalidate()fire their end hook from atry/finallywith an optionalexceptionfield, so a raised loop/check still closes its span. ERROR status is driven only by an exception — a routine budget-exhausted loop or failing check closes with default status. The metrics andbuiltin_debugplugins skip the outcome on the exception path.samplingandvalidationspans and corrected the span-hierarchy diagrams intracing.md/telemetry.md(they referenced arequirement_validationspan that never existed — the real span isvalidation).Notes for reviewers
session → action → sampling → {chat, validation}on Python 3.12+; on ≤3.11 hook-attached spans collapse tosession(same documented degradation as all existing tracing plugins).add_streaming_event→add_span_eventrename updatesStreamingTracingPlugin's call sites; no compat shim.samplingspan covers strategies that use theBaseSamplingStrategyloop. Strategies that overridesample()with a custom loop (SOFAI, budget-forcing, majority-voting) don't emit it — that coupling is tracked as a follow-up in fix(sampling): sampling span missing for strategies that override sample() #1487, out of scope here.loop_budget/requirement_count). Relative order is unchanged: tracing (1040–1045) still precedes metrics (1050–1057).Testing
Attribution
Adding a new component, requirement, sampling strategy, or tool?
If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.