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
//! OpenTelemetry tracing integration.
//!
//! This module provides integration with OpenTelemetry for distributed
//! tracing of terminal automation sessions.
//!
//! # Setup
//!
//! ```rust,ignore
//! use rust_expect::metrics::otel::{init_tracing, shutdown_tracing, TracingConfig};
//!
//! // Basic setup with defaults
//! init_tracing("my-service", "http://localhost:4317")?;
//!
//! // Or with custom configuration
//! let config = TracingConfig::new("my-service")
//!     .with_endpoint("http://localhost:4317")
//!     .with_sampling_ratio(0.5)
//!     .with_resource_attribute("environment", "production");
//!
//! init_tracing_with_config(config)?;
//!
//! // Run your application...
//!
//! // Clean shutdown
//! shutdown_tracing();
//! ```
//!
//! # Span Helpers
//!
//! ```rust,ignore
//! use rust_expect::metrics::otel::{session_span, expect_span, send_span};
//! use tracing::info_span;
//!
//! // Create a session span
//! let _guard = session_span("my-session", "bash", 12345);
//!
//! // Create an expect span
//! let _guard = expect_span("waiting for prompt", r"\$");
//!
//! // Create a send span
//! let _guard = send_span("ls -la");
//! ```

use std::collections::HashMap;
use std::sync::OnceLock;
use std::time::Duration;

use opentelemetry::KeyValue;
use opentelemetry::trace::TracerProvider;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::Resource;
use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, SdkTracerProvider};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

/// Global tracer provider for clean shutdown.
static TRACER_PROVIDER: OnceLock<SdkTracerProvider> = OnceLock::new();

/// Configuration for OpenTelemetry tracing.
#[derive(Debug, Clone)]
pub struct TracingConfig {
    /// Service name for traces.
    pub service_name: String,
    /// OTLP endpoint URL.
    pub endpoint: String,
    /// Sampling ratio (0.0 to 1.0).
    pub sampling_ratio: f64,
    /// Additional resource attributes.
    pub resource_attributes: HashMap<String, String>,
    /// Export timeout.
    pub export_timeout: Duration,
    /// Whether to also log to console.
    pub console_output: bool,
}

impl TracingConfig {
    /// Create a new tracing configuration.
    #[must_use]
    pub fn new(service_name: impl Into<String>) -> Self {
        Self {
            service_name: service_name.into(),
            endpoint: "http://localhost:4317".to_string(),
            sampling_ratio: 1.0,
            resource_attributes: HashMap::new(),
            export_timeout: Duration::from_secs(30),
            console_output: false,
        }
    }

    /// Set the OTLP endpoint.
    #[must_use]
    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = endpoint.into();
        self
    }

    /// Set the sampling ratio.
    #[must_use]
    pub const fn with_sampling_ratio(mut self, ratio: f64) -> Self {
        self.sampling_ratio = ratio.clamp(0.0, 1.0);
        self
    }

    /// Add a resource attribute.
    #[must_use]
    pub fn with_resource_attribute(
        mut self,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        self.resource_attributes.insert(key.into(), value.into());
        self
    }

    /// Set export timeout.
    #[must_use]
    pub const fn with_export_timeout(mut self, timeout: Duration) -> Self {
        self.export_timeout = timeout;
        self
    }

    /// Enable console output alongside OTLP.
    #[must_use]
    pub const fn with_console_output(mut self, enabled: bool) -> Self {
        self.console_output = enabled;
        self
    }
}

/// Error type for tracing initialization.
#[derive(Debug, thiserror::Error)]
pub enum TracingError {
    /// OpenTelemetry trace error.
    #[error("OpenTelemetry trace error: {0}")]
    Trace(#[from] opentelemetry_sdk::trace::TraceError),

    /// OTLP exporter build error.
    #[error("OTLP exporter build error: {0}")]
    ExporterBuild(#[from] opentelemetry_otlp::ExporterBuildError),

    /// Already initialized.
    #[error("Tracing already initialized")]
    AlreadyInitialized,

    /// Subscriber initialization failed.
    #[error("Failed to initialize tracing subscriber")]
    SubscriberInit,
}

/// Initialize OpenTelemetry tracing with default configuration.
///
/// # Errors
///
/// Returns an error if initialization fails or tracing is already initialized.
pub fn init_tracing(service_name: &str, endpoint: &str) -> Result<(), TracingError> {
    let config = TracingConfig::new(service_name).with_endpoint(endpoint);
    init_tracing_with_config(&config)
}

/// Initialize OpenTelemetry tracing with custom configuration.
///
/// # Errors
///
/// Returns an error if initialization fails or tracing is already initialized.
pub fn init_tracing_with_config(config: &TracingConfig) -> Result<(), TracingError> {
    // Build resource attributes
    let mut attributes = vec![KeyValue::new("service.name", config.service_name.clone())];
    for (key, value) in &config.resource_attributes {
        attributes.push(KeyValue::new(key.clone(), value.clone()));
    }

    let resource = Resource::builder().with_attributes(attributes).build();

    // Configure OTLP exporter
    let exporter = opentelemetry_otlp::SpanExporter::builder()
        .with_tonic()
        .with_endpoint(&config.endpoint)
        .with_timeout(config.export_timeout)
        .build()?;

    // Build tracer provider
    let provider = SdkTracerProvider::builder()
        .with_batch_exporter(exporter)
        .with_sampler(Sampler::TraceIdRatioBased(config.sampling_ratio))
        .with_id_generator(RandomIdGenerator::default())
        .with_resource(resource)
        .build();

    // Store provider for shutdown
    if TRACER_PROVIDER.set(provider.clone()).is_err() {
        return Err(TracingError::AlreadyInitialized);
    }

    // Create tracer
    let tracer = provider.tracer("rust-expect");

    // Create OpenTelemetry layer
    let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer);

    // Build subscriber
    if config.console_output {
        let fmt_layer = tracing_subscriber::fmt::layer()
            .with_target(true)
            .with_level(true);

        tracing_subscriber::registry()
            .with(otel_layer)
            .with(fmt_layer)
            .try_init()
            .map_err(|_| TracingError::SubscriberInit)?;
    } else {
        tracing_subscriber::registry()
            .with(otel_layer)
            .try_init()
            .map_err(|_| TracingError::SubscriberInit)?;
    }

    Ok(())
}

/// Shutdown OpenTelemetry tracing.
///
/// This should be called before application exit to ensure all spans
/// are exported.
pub fn shutdown_tracing() {
    if let Some(provider) = TRACER_PROVIDER.get() {
        // Force flush all pending spans - ignore errors during shutdown
        let _ = provider.force_flush();
    }
}

/// Create a span for a session operation.
///
/// Returns a span guard that will end the span when dropped.
///
/// # Example
///
/// ```rust,ignore
/// let _span = session_span("login-session", "bash", 12345);
/// // Session operations...
/// // Span ends when _span is dropped
/// ```
pub fn session_span(session_id: &str, command: &str, pid: u32) -> tracing::span::EnteredSpan {
    tracing::info_span!(
        "session",
        session.id = session_id,
        session.command = command,
        session.pid = pid,
        otel.kind = "client"
    )
    .entered()
}

/// Create a span for an expect operation.
///
/// Returns a span guard that will end the span when dropped.
///
/// # Example
///
/// ```rust,ignore
/// let _span = expect_span("waiting for login", "Password:");
/// // Expect operation...
/// // Span ends when _span is dropped
/// ```
pub fn expect_span(description: &str, pattern: &str) -> tracing::span::EnteredSpan {
    tracing::info_span!(
        "expect",
        expect.description = description,
        expect.pattern = pattern,
        otel.kind = "internal"
    )
    .entered()
}

/// Create a span for a send operation.
///
/// Returns a span guard that will end the span when dropped.
///
/// # Example
///
/// ```rust,ignore
/// let _span = send_span("ls -la");
/// // Send operation...
/// // Span ends when _span is dropped
/// ```
pub fn send_span(data: &str) -> tracing::span::EnteredSpan {
    // Truncate long data for span name
    let display_data = if data.len() > 50 {
        format!("{}...", &data[..47])
    } else {
        data.to_string()
    };

    tracing::info_span!(
        "send",
        send.data = display_data.as_str(),
        send.bytes = data.len(),
        otel.kind = "internal"
    )
    .entered()
}

/// Create a span for a dialog execution.
///
/// Returns a span guard that will end the span when dropped.
pub fn dialog_span(dialog_name: &str, step_count: usize) -> tracing::span::EnteredSpan {
    tracing::info_span!(
        "dialog",
        dialog.name = dialog_name,
        dialog.steps = step_count,
        otel.kind = "internal"
    )
    .entered()
}

/// Create a span for a transcript recording.
///
/// Returns a span guard that will end the span when dropped.
pub fn transcript_span(session_id: &str, format: &str) -> tracing::span::EnteredSpan {
    tracing::info_span!(
        "transcript",
        transcript.session = session_id,
        transcript.format = format,
        otel.kind = "internal"
    )
    .entered()
}

/// Record an error on the current span.
pub fn record_error(error: &dyn std::error::Error) {
    tracing::error!(
        exception.type_ = std::any::type_name_of_val(error),
        exception.message = %error,
    );
}

/// Record a successful match on the current span.
pub fn record_match(pattern: &str, matched_text: &str, duration_ms: u64) {
    tracing::info!(
        match.pattern = pattern,
        match.text = matched_text,
        match.duration_ms = duration_ms,
    );
}

/// Record bytes transferred on the current span.
pub fn record_bytes(sent: u64, received: u64) {
    tracing::info!(bytes.sent = sent, bytes.received = received,);
}

/// Span attribute constants for consistency.
pub mod attributes {
    /// Session ID attribute key.
    pub const SESSION_ID: &str = "session.id";
    /// Session command attribute key.
    pub const SESSION_COMMAND: &str = "session.command";
    /// Session PID attribute key.
    pub const SESSION_PID: &str = "session.pid";
    /// Expect pattern attribute key.
    pub const EXPECT_PATTERN: &str = "expect.pattern";
    /// Expect timeout attribute key.
    pub const EXPECT_TIMEOUT_MS: &str = "expect.timeout_ms";
    /// Send data attribute key.
    pub const SEND_DATA: &str = "send.data";
    /// Send bytes attribute key.
    pub const SEND_BYTES: &str = "send.bytes";
    /// Match text attribute key.
    pub const MATCH_TEXT: &str = "match.text";
    /// Match duration attribute key.
    pub const MATCH_DURATION_MS: &str = "match.duration_ms";
    /// Error type attribute key.
    pub const ERROR_TYPE: &str = "error.type";
    /// Error message attribute key.
    pub const ERROR_MESSAGE: &str = "error.message";
}

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

    #[test]
    fn tracing_config_default() {
        let config = TracingConfig::new("test-service");
        assert_eq!(config.service_name, "test-service");
        assert_eq!(config.endpoint, "http://localhost:4317");
        assert!((config.sampling_ratio - 1.0).abs() < 0.001);
    }

    #[test]
    fn tracing_config_builder() {
        let config = TracingConfig::new("test")
            .with_endpoint("http://custom:4317")
            .with_sampling_ratio(0.5)
            .with_resource_attribute("env", "test")
            .with_console_output(true);

        assert_eq!(config.endpoint, "http://custom:4317");
        assert!((config.sampling_ratio - 0.5).abs() < 0.001);
        assert_eq!(
            config.resource_attributes.get("env"),
            Some(&"test".to_string())
        );
        assert!(config.console_output);
    }

    #[test]
    fn sampling_ratio_clamped() {
        let config = TracingConfig::new("test").with_sampling_ratio(2.0);
        assert!((config.sampling_ratio - 1.0).abs() < 0.001);

        let config = TracingConfig::new("test").with_sampling_ratio(-0.5);
        assert!(config.sampling_ratio.abs() < 0.001);
    }

    // Note: Full integration tests require a running OTLP collector
    // and are typically run manually or in CI with proper setup.
}