backbone-observability 3.1.1

Backbone Framework Observability - Tracing, logging, and metrics
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
//! Structured logging collection
//!
//! Provides JSON-formatted structured logging with field extraction
//! and log aggregation support for centralized collection.

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use thiserror::Error;

/// Logging initialization error
#[derive(Debug, Error)]
pub enum LoggingError {
    /// Failed to setup logging
    #[error("Failed to setup logging: {0}")]
    SetupError(String),
}

/// Structured log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    /// Timestamp (ISO 8601)
    #[serde(rename = "@timestamp")]
    pub timestamp: String,

    /// Log level
    pub level: String,

    /// Target/module
    pub target: String,

    /// Log message
    pub message: String,

    /// Service name
    pub service: String,

    /// Environment
    pub environment: String,

    /// Hostname
    pub hostname: String,

    /// Span/trace ID if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub span_id: Option<String>,

    /// Trace ID if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trace_id: Option<String>,

    /// Request ID if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,

    /// User ID if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,

    /// Additional fields from the log event
    #[serde(flatten)]
    pub fields: HashMap<String, serde_json::Value>,
}

/// Logging configuration for structured output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Enable JSON structured logging
    pub json_format: bool,

    /// Include timestamp in all logs
    pub include_timestamp: bool,

    /// Include hostname in all logs
    pub include_hostname: bool,

    /// Include span context (trace_id, span_id)
    pub include_spans: bool,

    /// Field names to extract as top-level fields
    pub extract_fields: Vec<String>,

    /// Buffer size for log aggregation
    pub buffer_size: usize,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            json_format: true,
            include_timestamp: true,
            include_hostname: true,
            include_spans: true,
            extract_fields: vec![
                "user_id".to_string(),
                "request_id".to_string(),
                "module".to_string(),
                "action".to_string(),
            ],
            buffer_size: 1000,
        }
    }
}

/// Log buffer for aggregation
pub struct LogBuffer {
    buffer: Arc<Mutex<VecDeque<LogEntry>>>,
    config: LoggingConfig,
}

impl LogBuffer {
    /// Create a new log buffer with the given configuration
    pub fn new(config: LoggingConfig) -> Self {
        Self {
            buffer: Arc::new(Mutex::new(VecDeque::with_capacity(config.buffer_size))),
            config,
        }
    }

    /// Push a log entry to the buffer
    pub fn push(&self, entry: LogEntry) {
        let mut buffer = self.buffer.lock().unwrap();
        // Drop oldest if buffer is full
        if buffer.len() >= self.config.buffer_size {
            buffer.pop_front();
        }
        buffer.push_back(entry);
    }

    /// Get all buffered log entries
    pub fn drain(&self) -> Vec<LogEntry> {
        let mut buffer = self.buffer.lock().unwrap();
        let entries: Vec<LogEntry> = buffer.drain(..).collect();
        entries
    }

    /// Get current buffer length
    pub fn len(&self) -> usize {
        let buffer = self.buffer.lock().unwrap();
        buffer.len()
    }
}

/// Create a structured log entry from current context
pub fn create_log_entry(
    level: &str,
    target: &str,
    message: &str,
    service: &str,
    environment: &str,
) -> LogEntry {
    let timestamp = format_timestamp(SystemTime::now());

    // Extract span context if available
    let current_span = tracing::Span::current();
    let span_id = if current_span.is_none() {
        None
    } else {
        Some(format!("{:?}", current_span.id()))
    };

    // Extract trace_id from span (simplified)
    let trace_id = span_id.clone(); // In real implementation, would extract from span context

    LogEntry {
        timestamp,
        level: level.to_string(),
        target: target.to_string(),
        message: message.to_string(),
        service: service.to_string(),
        environment: environment.to_string(),
        hostname: get_hostname(),
        span_id,
        trace_id,
        request_id: None, // Would be extracted from span fields
        user_id: None,   // Would be extracted from span fields
        fields: HashMap::new(),
    }
}

/// Get hostname safely
fn get_hostname() -> String {
    std::env::var("HOSTNAME")
        .unwrap_or_else(|_| "localhost".to_string())
}

/// Format timestamp as ISO 8601
fn format_timestamp(time: SystemTime) -> String {
    use std::time::UNIX_EPOCH;

    let duration = time
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();

    let secs = duration.as_secs();
    let millis = duration.as_millis();

    // ISO 8601 format with millisecond precision
    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z",
        1970 + (secs / 31536000) % 10000,
        (secs / 2592000) % 12 + 1,
        (secs / 86400) % 31 + 1,
        (secs / 3600) % 24,
        (secs / 60) % 60,
        secs % 60,
        millis % 1000
    )
}

/// Initialize structured logging with JSON output
///
/// # Arguments
/// * `config` - Logging configuration
/// * `service_name` - Name of the service for log labels
/// * `environment` - Environment name (dev, staging, prod)
///
/// # Returns
/// Returns a log buffer handle for accessing aggregated logs
///
/// # Example
/// ```no_run
/// use backbone_observability::logging::{init_structured_logging, LoggingConfig};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = LoggingConfig {
///     json_format: true,
///     ..Default::default()
/// };
///
/// let buffer = init_structured_logging(&config, "my-service", "production", "info")?;
/// # Ok(())
/// # }
/// ```
pub fn init_structured_logging(
    config: &LoggingConfig,
    service_name: &str,
    environment: &str,
    log_level: &str,
) -> Result<LogBuffer, LoggingError> {
    use tracing_subscriber::{Registry, EnvFilter, layer::SubscriberExt};
    use tracing_subscriber::fmt;
    use tracing_subscriber::Layer;

    let filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(log_level));

    if config.json_format {
        // JSON formatter for production
        let json_layer = fmt::layer()
            .json()
            .with_target(true)
            .with_thread_ids(false)
            .with_thread_names(false)
            .with_file(false)
            .with_line_number(false)
            .with_span_events(fmt::format::FmtSpan::CLOSE)
            .boxed();

        let subscriber = Registry::default()
            .with(filter)
            .with(json_layer);

        tracing::subscriber::set_global_default(subscriber)
            .map_err(|e| LoggingError::SetupError(e.to_string()))?;
    } else {
        // Pretty formatter for development
        let fmt_layer = fmt::layer()
            .with_target(true)
            .with_thread_ids(false)
            .with_span_events(fmt::format::FmtSpan::CLOSE)
            .boxed();

        let subscriber = Registry::default()
            .with(filter)
            .with(fmt_layer);

        tracing::subscriber::set_global_default(subscriber)
            .map_err(|e| LoggingError::SetupError(e.to_string()))?;
    }

    tracing::info!(
        service = service_name,
        environment = environment,
        format = if config.json_format { "json" } else { "pretty" },
        "Structured logging initialized"
    );

    Ok(LogBuffer::new(config.clone()))
}

/// Get aggregated logs from a buffer
///
/// Convenience function for accessing logs from a buffer handle.
pub fn get_aggregated_logs(buffer: &LogBuffer) -> Vec<LogEntry> {
    buffer.drain()
}

/// Create a new request ID
///
/// Generates a UUID v4 for request tracking
pub fn create_request_id() -> String {
    use std::time::SystemTime;
    // Simple nanosecond-based ID
    format!("{:x}", SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos())
}

/// Request ID extension for tracing spans
///
/// Helper to add request_id to spans
pub trait WithRequestId {
    /// Add request ID to the current span
    fn with_request_id(self, request_id: &str) -> Self;
}

/// Helper macro to create a request-scoped span with request ID
#[macro_export]
macro_rules! request_span {
    ($name:expr, $request_id:expr) => {
        tracing::info_span!(
            $name,
            request_id = %$request_id,
        )
    };
}

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

    #[test]
    fn test_log_entry_serialization() {
        let entry = LogEntry {
            timestamp: "2024-01-01T00:00:00.000Z".to_string(),
            level: "INFO".to_string(),
            target: "test".to_string(),
            message: "Test message".to_string(),
            service: "test-service".to_string(),
            environment: "test".to_string(),
            hostname: "localhost".to_string(),
            span_id: None,
            trace_id: None,
            request_id: None,
            user_id: None,
            fields: HashMap::new(),
        };

        let json = serde_json::to_string(&entry).unwrap();
        assert!(json.contains("Test message"));
        assert!(json.contains("INFO"));
        assert!(json.contains("@timestamp"));
    }

    #[test]
    fn test_logging_config_defaults() {
        let config = LoggingConfig::default();
        assert!(config.json_format);
        assert!(config.include_timestamp);
        assert!(config.include_hostname);
        assert!(config.include_spans);
        assert_eq!(config.buffer_size, 1000);
    }

    #[test]
    fn test_log_buffer() {
        let config = LoggingConfig::default();
        let buffer = LogBuffer::new(config);

        assert_eq!(buffer.len(), 0);

        let entry = LogEntry {
            timestamp: "2024-01-01T00:00:00.000Z".to_string(),
            level: "INFO".to_string(),
            target: "test".to_string(),
            message: "Test message".to_string(),
            service: "test-service".to_string(),
            environment: "test".to_string(),
            hostname: "localhost".to_string(),
            span_id: None,
            trace_id: None,
            request_id: None,
            user_id: None,
            fields: HashMap::new(),
        };

        buffer.push(entry.clone());
        assert_eq!(buffer.len(), 1);

        let entries = buffer.drain();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].message, "Test message");
        assert_eq!(buffer.len(), 0);
    }

    #[test]
    fn test_create_request_id() {
        let id = create_request_id();
        assert!(!id.is_empty());
    }

    #[test]
    fn test_timestamp_format() {
        let time = SystemTime::UNIX_EPOCH;
        let timestamp = format_timestamp(time);
        assert!(timestamp.starts_with("1970-01-01T00:00:00."));
    }

    #[test]
    fn test_create_log_entry_with_defaults() {
        let entry = create_log_entry("INFO", "test_target", "test message", "test-service", "dev");

        assert_eq!(entry.level, "INFO");
        assert_eq!(entry.target, "test_target");
        assert_eq!(entry.message, "test message");
        assert_eq!(entry.service, "test-service");
        assert_eq!(entry.environment, "dev");
        assert_eq!(entry.hostname, "localhost");
        assert!(entry.fields.is_empty());
    }

    #[test]
    fn test_log_buffer_overflow() {
        let config = LoggingConfig {
            buffer_size: 3,
            ..Default::default()
        };
        let buffer = LogBuffer::new(config);

        for i in 0..5 {
            buffer.push(LogEntry {
                timestamp: format!("2024-01-01T00:00:{}.000Z", i),
                level: "INFO".to_string(),
                target: "test".to_string(),
                message: format!("Message {}", i),
                service: "test-service".to_string(),
                environment: "test".to_string(),
                hostname: "localhost".to_string(),
                span_id: None,
                trace_id: None,
                request_id: None,
                user_id: None,
                fields: HashMap::new(),
            });
        }

        // Buffer should only contain last 3 entries
        assert_eq!(buffer.len(), 3);

        let entries = buffer.drain();
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].message, "Message 2");
        assert_eq!(entries[1].message, "Message 3");
        assert_eq!(entries[2].message, "Message 4");
    }
}