rust-expect 0.1.0

Next-generation Expect-style terminal automation library for Rust
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Prometheus metrics export.
//!
//! This module provides Prometheus-compatible metrics export for
//! terminal automation sessions.
//!
//! # Usage
//!
//! ```rust,ignore
//! use rust_expect::metrics::prometheus_export::{ExpectMetrics, gather_metrics};
//!
//! // Get global metrics instance
//! let metrics = ExpectMetrics::global();
//!
//! // Record operations
//! metrics.session_started();
//! metrics.bytes_sent(1024);
//! metrics.expect_succeeded("prompt", 0.5);
//!
//! // Export metrics in Prometheus format
//! let output = gather_metrics();
//! println!("{}", output);
//! ```
//!
//! # HTTP Server Integration
//!
//! ```rust,ignore
//! use rust_expect::metrics::prometheus_export::gather_metrics;
//!
//! // In your HTTP handler for /metrics endpoint:
//! fn metrics_handler() -> String {
//!     gather_metrics()
//! }
//! ```

use std::sync::OnceLock;

use prometheus::{
    Counter, CounterVec, Encoder, Gauge, Histogram, HistogramOpts, HistogramVec, Opts, Registry,
    TextEncoder,
};

/// Global metrics registry.
static REGISTRY: OnceLock<Registry> = OnceLock::new();

/// Global expect metrics instance.
static METRICS: OnceLock<ExpectMetrics> = OnceLock::new();

/// Get or create the global registry.
fn registry() -> &'static Registry {
    REGISTRY.get_or_init(Registry::new)
}

/// Prometheus metrics for rust-expect.
#[derive(Debug, Clone)]
pub struct ExpectMetrics {
    // Session metrics
    sessions_active: Gauge,
    sessions_total: Counter,
    session_duration: Histogram,

    // I/O metrics
    bytes_sent_total: Counter,
    bytes_received_total: Counter,

    // Expect metrics
    expect_total: CounterVec,
    expect_duration: HistogramVec,
    expect_timeouts: Counter,

    // Command metrics
    commands_total: Counter,
    command_duration: Histogram,

    // Error metrics
    errors_total: CounterVec,

    // Dialog metrics
    dialogs_total: Counter,
    dialog_duration: Histogram,
    dialog_steps: Histogram,
}

impl ExpectMetrics {
    /// Get the global metrics instance, initializing if needed.
    ///
    /// # Panics
    ///
    /// Panics if metrics registration fails (should only happen on first call).
    pub fn global() -> &'static Self {
        METRICS.get_or_init(|| Self::new(registry()).expect("Failed to register metrics"))
    }

    /// Create new metrics registered with the given registry.
    ///
    /// # Errors
    ///
    /// Returns an error if metric registration fails.
    pub fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
        // Session metrics
        let sessions_active = Gauge::with_opts(Opts::new(
            "expect_sessions_active",
            "Number of currently active expect sessions",
        ))?;
        registry.register(Box::new(sessions_active.clone()))?;

        let sessions_total = Counter::with_opts(Opts::new(
            "expect_sessions_total",
            "Total number of expect sessions started",
        ))?;
        registry.register(Box::new(sessions_total.clone()))?;

        let session_duration = Histogram::with_opts(
            HistogramOpts::new(
                "expect_session_duration_seconds",
                "Duration of expect sessions in seconds",
            )
            .buckets(vec![0.1, 0.5, 1.0, 5.0, 10.0, 30.0, 60.0, 300.0, 600.0]),
        )?;
        registry.register(Box::new(session_duration.clone()))?;

        // I/O metrics
        let bytes_sent_total = Counter::with_opts(Opts::new(
            "expect_bytes_sent_total",
            "Total bytes sent to sessions",
        ))?;
        registry.register(Box::new(bytes_sent_total.clone()))?;

        let bytes_received_total = Counter::with_opts(Opts::new(
            "expect_bytes_received_total",
            "Total bytes received from sessions",
        ))?;
        registry.register(Box::new(bytes_received_total.clone()))?;

        // Expect metrics
        let expect_total = CounterVec::new(
            Opts::new("expect_operations_total", "Total expect operations"),
            &["status"],
        )?;
        registry.register(Box::new(expect_total.clone()))?;

        let expect_duration = HistogramVec::new(
            HistogramOpts::new(
                "expect_operation_duration_seconds",
                "Duration of expect operations in seconds",
            )
            .buckets(vec![0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0]),
            &["pattern_type"],
        )?;
        registry.register(Box::new(expect_duration.clone()))?;

        let expect_timeouts = Counter::with_opts(Opts::new(
            "expect_timeouts_total",
            "Total number of expect timeout errors",
        ))?;
        registry.register(Box::new(expect_timeouts.clone()))?;

        // Command metrics
        let commands_total = Counter::with_opts(Opts::new(
            "expect_commands_total",
            "Total commands sent to sessions",
        ))?;
        registry.register(Box::new(commands_total.clone()))?;

        let command_duration = Histogram::with_opts(
            HistogramOpts::new(
                "expect_command_duration_seconds",
                "Duration of command execution in seconds",
            )
            .buckets(vec![0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0]),
        )?;
        registry.register(Box::new(command_duration.clone()))?;

        // Error metrics
        let errors_total = CounterVec::new(
            Opts::new("expect_errors_total", "Total errors by type"),
            &["error_type"],
        )?;
        registry.register(Box::new(errors_total.clone()))?;

        // Dialog metrics
        let dialogs_total =
            Counter::with_opts(Opts::new("expect_dialogs_total", "Total dialog executions"))?;
        registry.register(Box::new(dialogs_total.clone()))?;

        let dialog_duration = Histogram::with_opts(
            HistogramOpts::new(
                "expect_dialog_duration_seconds",
                "Duration of dialog execution in seconds",
            )
            .buckets(vec![0.1, 0.5, 1.0, 5.0, 10.0, 30.0, 60.0]),
        )?;
        registry.register(Box::new(dialog_duration.clone()))?;

        let dialog_steps = Histogram::with_opts(
            HistogramOpts::new("expect_dialog_steps", "Number of steps in executed dialogs")
                .buckets(vec![1.0, 2.0, 5.0, 10.0, 20.0, 50.0]),
        )?;
        registry.register(Box::new(dialog_steps.clone()))?;

        Ok(Self {
            sessions_active,
            sessions_total,
            session_duration,
            bytes_sent_total,
            bytes_received_total,
            expect_total,
            expect_duration,
            expect_timeouts,
            commands_total,
            command_duration,
            errors_total,
            dialogs_total,
            dialog_duration,
            dialog_steps,
        })
    }

    // Session methods

    /// Record a new session starting.
    pub fn session_started(&self) {
        self.sessions_total.inc();
        self.sessions_active.inc();
    }

    /// Record a session ending.
    pub fn session_ended(&self, duration_seconds: f64) {
        self.sessions_active.dec();
        self.session_duration.observe(duration_seconds);
    }

    // I/O methods

    /// Record bytes sent.
    pub fn bytes_sent(&self, count: u64) {
        self.bytes_sent_total.inc_by(count as f64);
    }

    /// Record bytes received.
    pub fn bytes_received(&self, count: u64) {
        self.bytes_received_total.inc_by(count as f64);
    }

    // Expect methods

    /// Record a successful expect operation.
    pub fn expect_succeeded(&self, pattern_type: &str, duration_seconds: f64) {
        self.expect_total.with_label_values(&["success"]).inc();
        self.expect_duration
            .with_label_values(&[pattern_type])
            .observe(duration_seconds);
    }

    /// Record a failed expect operation.
    pub fn expect_failed(&self, pattern_type: &str, duration_seconds: f64) {
        self.expect_total.with_label_values(&["failure"]).inc();
        self.expect_duration
            .with_label_values(&[pattern_type])
            .observe(duration_seconds);
    }

    /// Record an expect timeout.
    pub fn expect_timeout(&self) {
        self.expect_timeouts.inc();
        self.expect_total.with_label_values(&["timeout"]).inc();
    }

    // Command methods

    /// Record a command sent.
    pub fn command_sent(&self) {
        self.commands_total.inc();
    }

    /// Record command completion with duration.
    pub fn command_completed(&self, duration_seconds: f64) {
        self.command_duration.observe(duration_seconds);
    }

    // Error methods

    /// Record an error by type.
    pub fn error(&self, error_type: &str) {
        self.errors_total.with_label_values(&[error_type]).inc();
    }

    /// Record an I/O error.
    pub fn io_error(&self) {
        self.error("io");
    }

    /// Record a timeout error.
    pub fn timeout_error(&self) {
        self.error("timeout");
    }

    /// Record a pattern error.
    pub fn pattern_error(&self) {
        self.error("pattern");
    }

    /// Record an EOF error.
    pub fn eof_error(&self) {
        self.error("eof");
    }

    // Dialog methods

    /// Record a dialog started.
    pub fn dialog_started(&self, step_count: usize) {
        self.dialogs_total.inc();
        self.dialog_steps.observe(step_count as f64);
    }

    /// Record a dialog completed.
    pub fn dialog_completed(&self, duration_seconds: f64) {
        self.dialog_duration.observe(duration_seconds);
    }

    // Getters for current values

    /// Get current active session count.
    #[must_use]
    pub fn active_sessions(&self) -> u64 {
        self.sessions_active.get() as u64
    }

    /// Get total sessions started.
    #[must_use]
    pub fn total_sessions(&self) -> u64 {
        self.sessions_total.get() as u64
    }

    /// Get total bytes sent.
    #[must_use]
    pub fn total_bytes_sent(&self) -> u64 {
        self.bytes_sent_total.get() as u64
    }

    /// Get total bytes received.
    #[must_use]
    pub fn total_bytes_received(&self) -> u64 {
        self.bytes_received_total.get() as u64
    }

    /// Get total timeout count.
    #[must_use]
    pub fn total_timeouts(&self) -> u64 {
        self.expect_timeouts.get() as u64
    }
}

/// Gather all metrics in Prometheus text format.
///
/// # Returns
///
/// A string containing all metrics in Prometheus exposition format.
#[must_use]
pub fn gather_metrics() -> String {
    let encoder = TextEncoder::new();
    let metric_families = registry().gather();
    let mut buffer = Vec::new();
    encoder
        .encode(&metric_families, &mut buffer)
        .unwrap_or_default();
    String::from_utf8(buffer).unwrap_or_default()
}

/// Get the global Prometheus registry.
///
/// Useful for integrating with existing Prometheus setups.
#[must_use]
pub fn global_registry() -> &'static Registry {
    registry()
}

/// Create a new isolated registry for testing.
#[must_use]
pub fn new_registry() -> Registry {
    Registry::new()
}

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

    #[test]
    fn metrics_registration() {
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).expect("Failed to create metrics");

        // Basic operations shouldn't panic
        metrics.session_started();
        metrics.bytes_sent(100);
        metrics.expect_succeeded("string", 0.1);
        metrics.error("test");
    }

    #[test]
    fn session_tracking() {
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).unwrap();

        assert_eq!(metrics.active_sessions(), 0);
        assert_eq!(metrics.total_sessions(), 0);

        metrics.session_started();
        assert_eq!(metrics.active_sessions(), 1);
        assert_eq!(metrics.total_sessions(), 1);

        metrics.session_started();
        assert_eq!(metrics.active_sessions(), 2);
        assert_eq!(metrics.total_sessions(), 2);

        metrics.session_ended(1.0);
        assert_eq!(metrics.active_sessions(), 1);
        assert_eq!(metrics.total_sessions(), 2);
    }

    #[test]
    fn byte_counters() {
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).unwrap();

        metrics.bytes_sent(100);
        metrics.bytes_sent(50);
        assert_eq!(metrics.total_bytes_sent(), 150);

        metrics.bytes_received(200);
        assert_eq!(metrics.total_bytes_received(), 200);
    }

    #[test]
    fn gather_output() {
        // Use a fresh registry for isolated test
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).unwrap();

        metrics.session_started();
        metrics.bytes_sent(1000);

        let encoder = TextEncoder::new();
        let metric_families = registry.gather();
        let mut buffer = Vec::new();
        encoder.encode(&metric_families, &mut buffer).unwrap();
        let output = String::from_utf8(buffer).unwrap();

        // Check that metrics are present
        assert!(output.contains("expect_sessions_total"));
        assert!(output.contains("expect_bytes_sent_total"));
    }

    #[test]
    fn error_types() {
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).unwrap();

        metrics.io_error();
        metrics.timeout_error();
        metrics.pattern_error();
        metrics.eof_error();
        metrics.error("custom");

        // Verify no panics and metrics are recorded
    }

    #[test]
    fn dialog_metrics() {
        let registry = new_registry();
        let metrics = ExpectMetrics::new(&registry).unwrap();

        metrics.dialog_started(5);
        metrics.dialog_completed(2.5);

        // Verify no panics
    }
}