Skip to content

Commit 9fb1960

Browse files
calebedenCopilot
andauthored
feat: add opt-in telemetry for native chat lifecycle (#998)
* Add chat OpenTelemetry instrumentation Co-authored-by: Copilot App <[email protected]> Copilot-Session: c22819d0-eb50-4f50-9a60-c76ff17856af * Add chat response phase telemetry Co-authored-by: Copilot App <[email protected]> Copilot-Session: c22819d0-eb50-4f50-9a60-c76ff17856af * Fix chat telemetry completion boundaries Co-authored-by: Copilot App <[email protected]> Copilot-Session: c22819d0-eb50-4f50-9a60-c76ff17856af * Add chat queue wait spans Co-authored-by: Copilot App <[email protected]> Copilot-Session: c22819d0-eb50-4f50-9a60-c76ff17856af * Align chat terminal run ownership Co-authored-by: Copilot App <[email protected]> Copilot-Session: c22819d0-eb50-4f50-9a60-c76ff17856af --------- Co-authored-by: Copilot App <[email protected]>
1 parent 4706ca7 commit 9fb1960

8 files changed

Lines changed: 2412 additions & 43 deletions

File tree

‎docs/TELEMETRY.md‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,121 @@ changing connection behavior.
140140
The phase spans intentionally do not trace signing, serialization, response
141141
parsing, capability details, or token persistence as separate operations.
142142

143+
## Chat lifecycle
144+
145+
The tray exports native chat lifecycle diagnostics when an endpoint is configured:
146+
147+
- traces: `openclaw.chat.turn`, `openclaw.chat.queue.wait`, `openclaw.chat.send`,
148+
`openclaw.chat.response.wait`, `openclaw.chat.response.receive`,
149+
`openclaw.chat.history.load`, and `openclaw.chat.history.backfill`
150+
- counters: `openclaw.chat.turns`, `openclaw.chat.send.attempts`,
151+
`openclaw.chat.history.loads`, `openclaw.chat.history.backfills`, and
152+
`openclaw.chat.remote_turns.dropped`, and
153+
`openclaw.chat.terminal_events.dropped`
154+
- duration histograms: `openclaw.chat.turn.duration`,
155+
`openclaw.chat.queue.wait.duration`, `openclaw.chat.send.duration`,
156+
`openclaw.chat.response.wait.duration`,
157+
`openclaw.chat.response.receive.duration`,
158+
`openclaw.chat.history.load.duration`, and
159+
`openclaw.chat.history.backfill.duration`
160+
161+
A local turn starts when the tray admits a valid request to direct dispatch or its
162+
local queue. An observed remote turn starts at a gateway lifecycle start carrying
163+
a run ID. Turn correlation uses message, run, and thread identifiers only inside
164+
the process; those identifiers are never attached to exported signals. Each turn
165+
span is explicitly created as a root, while each sampled local send attempt is
166+
explicitly parented to its turn.
167+
168+
Turn completion is exactly once. Assistant final, lifecycle end, lifecycle error,
169+
send rejection, queue cancellation, explicit abort, reset/supersession,
170+
disconnect, and disposal race through an atomic tracker; the first applicable
171+
terminal transition removes correlation state, and later duplicate signals are
172+
ignored. Assistant-final events do not contain a run ID, so the provider captures
173+
the active run under its existing state lock before completing telemetry. A remote
174+
lifecycle start without a run ID is not traced using an unsafe thread fallback;
175+
it increments `openclaw.chat.remote_turns.dropped` with the finite reason
176+
`missing_run_id`. A missing-run start for an already-dispatched local turn is not
177+
misclassified as a dropped remote turn.
178+
179+
Terminal lifecycle events are never matched by thread alone. If a terminal event
180+
has no run ID or conflicts with the active run, it cannot complete a potentially
181+
newer turn. The provider drops malformed lifecycle and legacy job terminals
182+
before they can clear active-run, timeline, queue, or telemetry state, logs a
183+
content-free warning, and increments `openclaw.chat.terminal_events.dropped`.
184+
A later terminal carrying the exact active run ID can still complete the turn;
185+
otherwise unresolved turns remain eligible for safe reset, disconnect, or
186+
disposal cleanup. Assistant-final chat events are separate: their protocol shape
187+
does not carry a run ID, so the provider captures the authoritative active run
188+
under its state lock rather than accepting a thread-only agent terminal.
189+
190+
Each `openclaw.chat.send` span represents one `chat.send` RPC attempt. A valid
191+
retryable deferral is `outcome=success` with admission status `deferred`, because
192+
the RPC completed and returned a recognized decision. Local requeue is not a
193+
separate exported admission status. Accepted responses use `accepted`; terminal
194+
rejection, cancellation, and exceptions use `rejected`, `canceled`, and
195+
`exception`. Unknown values map to `other`.
196+
197+
Response timing is split into two sibling child spans under the turn:
198+
199+
- `openclaw.chat.response.wait` starts at accepted local admission or observed
200+
lifecycle start and ends at the first recognized assistant, reasoning, or tool
201+
output.
202+
- `openclaw.chat.response.receive` starts at that first inbound event and ends
203+
with the turn's authoritative terminal transition.
204+
205+
If a turn terminates before visible output, the wait span closes with
206+
`openclaw.chat.response.first_output=none` and no receive span is emitted.
207+
Repeated chunks do not create additional spans. Phase duration metrics are
208+
recorded at turn completion so they carry the final bounded turn outcome. A wait
209+
span that reaches first output reports its own phase outcome as `success`; its
210+
duration metric still uses the enclosing turn's final outcome for aggregation.
211+
Output received before accepted admission or lifecycle start does not synthesize
212+
a wait or receive phase.
213+
Unknown admission statuses, routine status/error events, and unknown future
214+
event types do not start or transition response phases. The `other` output value
215+
is reserved for future event types only after they are explicitly reviewed and
216+
classified as visible response output.
217+
218+
Each contiguous local queue or requeue period emits an
219+
`openclaw.chat.queue.wait` sibling span under the turn. A segment that reaches
220+
dispatch completes with `outcome=success`; a segment still queued when the turn
221+
terminates uses the turn's final outcome. Deferred sends therefore show multiple
222+
queue-wait spans rather than one span that incorrectly includes intervening send
223+
attempts.
224+
225+
The queue-wait duration metric remains cumulative across all queue/retry segments.
226+
The tray adds each segment when dispatch begins and emits the total when the turn
227+
completes so it can carry the final outcome. Direct sends accepted on their first
228+
attempt emit neither queue-wait spans nor queue-wait measurements. Consequently,
229+
the metric timestamp is the turn completion time, not the instant queue congestion
230+
occurred.
231+
232+
Full transcript loads and targeted remote-message backfills are separate
233+
operations. Full loads use source `initial` or `forced`. Backfills use the finite
234+
reason `remote_turn` or `reset_reconciliation`.
235+
236+
Chat attributes are restricted to:
237+
238+
- `openclaw.source`: `local`, `remote`, `initial`, or `forced`, as applicable
239+
- `openclaw.outcome`: `success`, `failure`, or `canceled`
240+
- `openclaw.reason`: `assistant_final`, `lifecycle_end`, `lifecycle_error`,
241+
`send_rejected`, `queued_canceled`, `abort_requested`, `reset`, `superseded`,
242+
`disconnected`, `disposed`, or `other`
243+
- `openclaw.chat.admission.status`: `accepted`, `deferred`, `rejected`,
244+
`canceled`, `exception`, or `other`
245+
- `openclaw.chat.backfill.reason`: `remote_turn` or `reset_reconciliation`
246+
- `openclaw.chat.remote_turn.drop.reason`: `missing_run_id`
247+
- `openclaw.chat.terminal_event.drop.reason`: `missing_run_id` or
248+
`mismatched_run_id`
249+
- `openclaw.chat.response.first_output`: `none`, `assistant`, `reasoning`,
250+
`tool`, or `other`
251+
- `error.type`: exception type only, never the exception message
252+
253+
Chat telemetry does not export prompts, responses, transcript contents, IDs,
254+
model/provider names, attachment metadata, filenames, tool names, token usage,
255+
URLs, error messages, or local chat log text. No chat log category is added to
256+
the OpenTelemetry log allowlist.
257+
143258
## Endpoint handling
144259

145260
The endpoint setting is a collector endpoint, not a credential or request-parameter store. Accept plain `http` and `https` collector URLs with optional path prefixes. Reject URLs with embedded user info, query strings, or fragments.

‎src/OpenClaw.Shared/Telemetry/OpenClawTelemetry.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static class OpenClawTelemetry
8282
var previous = Activity.Current;
8383
try
8484
{
85+
Activity.Current = null;
8586
var activity = source.ToActivitySource().StartActivity(spanName, kind, parentContext);
8687
ApplyTags(activity, tags);
8788
return activity;

0 commit comments

Comments
 (0)