leviath-telemetry 0.2.0

OpenTelemetry export for Leviath's telemetry event stream
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! The `exporter = "stdout"` sink: one readable line per event through the
//! process logger.
//!
//! Named "stdout" in config for familiarity with other OTEL tooling, but the
//! lines go through `tracing` to **stderr** like every other Leviath log:
//! stdout is `lev agent-client`'s JSON-RPC channel and must stay clean. (For
//! the same reason this is hand-rolled rather than `opentelemetry-stdout`,
//! which writes to stdout unconditionally.)

use leviath_core::telemetry::{LaneHealth, LogKind, ProviderHealth, TelemetryEvent, TelemetrySink};

/// One readable line for an event.
pub(crate) fn format_event(event: &TelemetryEvent) -> String {
    match event {
        TelemetryEvent::RunStarted {
            run_id,
            agent_name,
            recovered,
            ..
        } => {
            let suffix = if *recovered { " (recovered)" } else { "" };
            format!("run {run_id} started: agent {agent_name}{suffix}")
        }
        TelemetryEvent::StageEntered {
            run_id,
            stage_index,
            stage_name,
            ..
        } => format!("run {run_id} entered stage {stage_index} ({stage_name})"),
        TelemetryEvent::StageExited {
            run_id,
            stage_index,
            stage_name,
            prompt_tokens,
            completion_tokens,
            ..
        } => format!(
            "run {run_id} exited stage {stage_index} ({stage_name}): \
             {prompt_tokens} in, {completion_tokens} out"
        ),
        TelemetryEvent::InferenceCompleted {
            run_id,
            provider,
            model,
            latency_ms,
            prompt_tokens,
            completion_tokens,
            success,
            ..
        } => format!(
            "run {run_id} inference {}: {provider}/{model} {latency_ms}ms, \
             {prompt_tokens} in, {completion_tokens} out",
            if *success { "ok" } else { "failed" }
        ),
        TelemetryEvent::ToolCallCompleted {
            run_id,
            tool_name,
            batch_latency_ms,
            success,
            ..
        } => format!(
            "run {run_id} tool {tool_name} {}: batch {batch_latency_ms}ms",
            if *success { "ok" } else { "failed" }
        ),
        TelemetryEvent::CompactionCompleted {
            run_id, success, ..
        } => format!(
            "run {run_id} compaction {}",
            if *success { "ok" } else { "failed" }
        ),
        TelemetryEvent::RunCompleted {
            run_id,
            status,
            prompt_tokens,
            completion_tokens,
            tool_calls,
            empty_output,
            ..
        } => format!(
            "run {run_id} {status}: {prompt_tokens} in, {completion_tokens} out, \
             {tool_calls} tool calls{}",
            if *empty_output { " (no output)" } else { "" }
        ),
        TelemetryEvent::Log {
            run_id,
            stage_index,
            kind,
            line,
        } => {
            let kind = match kind {
                LogKind::Output => "output",
                LogKind::Runtime => "log",
            };
            format!("run {run_id} stage {stage_index} {kind}: {line}")
        }
    }
}

/// One readable line for a daemon-wide health sample.
pub(crate) fn format_lane_health(health: &LaneHealth) -> String {
    format!(
        "lanes: agents active={} waiting={}, tools {}/{} busy, {} parked, {} queued, \
         dead cycles {}, relief {}",
        health.agents_active,
        health.agents_waiting,
        health.tools_busy,
        health.tools_workers,
        health.tools_parked,
        health.tools_queued,
        health.dead_cycles,
        health.relief_granted,
    )
}

/// One readable line for the providers currently out of service.
///
/// `None` while everything is serving, so a healthy daemon does not narrate a
/// line saying nothing is wrong on every re-drive.
pub(crate) fn format_providers_down(down: &[ProviderHealth]) -> Option<String> {
    if down.is_empty() {
        return None;
    }
    let each = down
        .iter()
        .map(|p| {
            format!(
                "{} ({}, {} failures, retry in {}s)",
                p.provider, p.reason, p.consecutive_failures, p.retry_in_secs
            )
        })
        .collect::<Vec<_>>()
        .join(", ");
    Some(format!("providers out of service: {each}"))
}

/// [`TelemetrySink`] that narrates events as `tracing` lines (→ stderr).
pub struct LogSink;

impl TelemetrySink for LogSink {
    fn emit(&self, event: TelemetryEvent) {
        tracing::info!(target: "leviath::telemetry", "{}", format_event(&event));
    }

    fn observe_lanes(&self, health: LaneHealth) {
        tracing::info!(target: "leviath::telemetry", "{}", format_lane_health(&health));
    }

    fn observe_providers(&self, down: &[ProviderHealth]) {
        if let Some(line) = format_providers_down(down) {
            tracing::warn!(target: "leviath::telemetry", "{line}");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_event_formats_to_one_line() {
        let events = [
            (
                TelemetryEvent::RunStarted {
                    run_id: "r1".to_string(),
                    agent_name: "coder".to_string(),
                    model: None,
                    parent_run_id: None,
                    recovered: false,
                    at_ms: 0,
                },
                "run r1 started: agent coder",
            ),
            (
                TelemetryEvent::RunStarted {
                    run_id: "r1".to_string(),
                    agent_name: "coder".to_string(),
                    model: None,
                    parent_run_id: None,
                    recovered: true,
                    at_ms: 0,
                },
                "run r1 started: agent coder (recovered)",
            ),
            (
                TelemetryEvent::StageEntered {
                    run_id: "r1".to_string(),
                    stage_index: 1,
                    stage_name: "build".to_string(),
                    at_ms: 0,
                },
                "run r1 entered stage 1 (build)",
            ),
            (
                TelemetryEvent::StageExited {
                    run_id: "r1".to_string(),
                    stage_index: 1,
                    stage_name: "build".to_string(),
                    prompt_tokens: 10,
                    completion_tokens: 4,
                    at_ms: 0,
                },
                "run r1 exited stage 1 (build): 10 in, 4 out",
            ),
            (
                TelemetryEvent::InferenceCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    provider: "anthropic".to_string(),
                    model: "m".to_string(),
                    latency_ms: 120,
                    prompt_tokens: 10,
                    completion_tokens: 4,
                    cached_tokens: 0,
                    success: true,
                },
                "run r1 inference ok: anthropic/m 120ms, 10 in, 4 out",
            ),
            (
                TelemetryEvent::InferenceCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    provider: "anthropic".to_string(),
                    model: "m".to_string(),
                    latency_ms: 120,
                    prompt_tokens: 0,
                    completion_tokens: 0,
                    cached_tokens: 0,
                    success: false,
                },
                "run r1 inference failed: anthropic/m 120ms, 0 in, 0 out",
            ),
            (
                TelemetryEvent::ToolCallCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    tool_name: "read_file".to_string(),
                    batch_latency_ms: 30,
                    success: true,
                },
                "run r1 tool read_file ok: batch 30ms",
            ),
            (
                TelemetryEvent::ToolCallCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    tool_name: "shell".to_string(),
                    batch_latency_ms: 30,
                    success: false,
                },
                "run r1 tool shell failed: batch 30ms",
            ),
            (
                TelemetryEvent::CompactionCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    success: true,
                },
                "run r1 compaction ok",
            ),
            (
                TelemetryEvent::CompactionCompleted {
                    run_id: "r1".to_string(),
                    stage_name: "build".to_string(),
                    success: false,
                },
                "run r1 compaction failed",
            ),
            (
                TelemetryEvent::RunCompleted {
                    run_id: "r1".to_string(),
                    status: "complete".to_string(),
                    prompt_tokens: 10,
                    completion_tokens: 4,
                    tool_calls: 2,
                    empty_output: false,
                    at_ms: 0,
                },
                "run r1 complete: 10 in, 4 out, 2 tool calls",
            ),
            (
                // Same tallies, but the run changed nothing: the line has to
                // say so, or it reads as a success (issue #192).
                TelemetryEvent::RunCompleted {
                    run_id: "r1".to_string(),
                    status: "complete".to_string(),
                    prompt_tokens: 10,
                    completion_tokens: 4,
                    tool_calls: 2,
                    empty_output: true,
                    at_ms: 0,
                },
                "run r1 complete: 10 in, 4 out, 2 tool calls (no output)",
            ),
            (
                TelemetryEvent::Log {
                    run_id: "r1".to_string(),
                    stage_index: 0,
                    kind: LogKind::Output,
                    line: "hello".to_string(),
                },
                "run r1 stage 0 output: hello",
            ),
            (
                TelemetryEvent::Log {
                    run_id: "r1".to_string(),
                    stage_index: 0,
                    kind: LogKind::Runtime,
                    line: "[Tokens: 1 in, 1 out]".to_string(),
                },
                "run r1 stage 0 log: [Tokens: 1 in, 1 out]",
            ),
        ];
        for (event, expected) in events {
            assert_eq!(format_event(&event), expected);
        }
    }

    #[test]
    fn emit_routes_through_tracing_without_panicking() {
        let _guard = leviath_testkit::tracing_guard();
        LogSink.emit(TelemetryEvent::RunCompleted {
            run_id: "r1".to_string(),
            status: "complete".to_string(),
            prompt_tokens: 0,
            completion_tokens: 0,
            tool_calls: 0,
            empty_output: false,
            at_ms: 0,
        });
    }

    /// The daemon-wide line reads as one sentence, with the numbers an operator
    /// asks for in the order they ask for them.
    #[test]
    fn lane_health_formats_to_one_line() {
        let line = format_lane_health(&LaneHealth {
            agents_active: 6,
            agents_waiting: 2,
            tools_busy: 8,
            tools_queued: 12,
            tools_parked: 3,
            tools_workers: 8,
            dead_cycles: 4,
            relief_granted: 2,
        });
        assert_eq!(
            line,
            "lanes: agents active=6 waiting=2, tools 8/8 busy, 3 parked, 12 queued, \
             dead cycles 4, relief 2"
        );
        assert!(!line.contains('\n'), "one line: {line}");
    }

    #[test]
    fn observe_lanes_routes_through_tracing_without_panicking() {
        let _guard = leviath_testkit::tracing_guard();
        LogSink.observe_lanes(LaneHealth::default());
    }

    #[test]
    fn providers_down_formats_to_one_line() {
        let line = format_providers_down(&[
            ProviderHealth {
                provider: "openrouter".to_string(),
                reason: "credits-exhausted".to_string(),
                consecutive_failures: 3,
                retry_in_secs: 240,
            },
            ProviderHealth {
                provider: "anthropic".to_string(),
                reason: "auth-failed".to_string(),
                consecutive_failures: 5,
                retry_in_secs: 30,
            },
        ])
        .expect("something is down");
        assert_eq!(
            line,
            "providers out of service: openrouter (credits-exhausted, 3 failures, retry in 240s), \
             anthropic (auth-failed, 5 failures, retry in 30s)"
        );
        assert!(!line.contains('\n'), "one line: {line}");
    }

    /// A healthy daemon must not narrate "nothing is wrong" every 30 seconds.
    #[test]
    fn nothing_down_is_no_line_at_all() {
        assert_eq!(format_providers_down(&[]), None);
        let _guard = leviath_testkit::tracing_guard();
        LogSink.observe_providers(&[]);
    }

    #[test]
    fn observe_providers_routes_through_tracing_without_panicking() {
        let _guard = leviath_testkit::tracing_guard();
        LogSink.observe_providers(&[ProviderHealth {
            provider: "openrouter".to_string(),
            reason: "credits-exhausted".to_string(),
            consecutive_failures: 3,
            retry_in_secs: 240,
        }]);
    }
}