pf_structured_logging 0.1.1

Enhanced structured logging with performance optimizations and convenience APIs for SpinKube agents
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Enhanced configuration and management for structured logging
//!
//! This module provides core configuration and management functionality for structured logging
//! without external extension system dependencies.

use crate::error::{Result, StructuredLoggingError};
use observability_core::{LogEntry, ObservabilityConfig, ObservabilityManager};
use serde::{Deserialize, Serialize};

/// Enhanced configuration for structured logging with performance and convenience features
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct EnhancedObservabilityConfig {
    /// Base observability configuration
    pub base: ObservabilityConfig,

    /// Performance optimization settings
    #[cfg(feature = "performance-optimized")]
    pub performance: PerformanceConfig,

    /// Correlation enhancement settings
    #[cfg(feature = "correlation-enhanced")]
    pub correlation: CorrelationConfig,

    /// Convenience feature settings
    #[cfg(feature = "convenience")]
    pub convenience: ConvenienceConfig,

    /// Panic handling configuration
    pub panic_handler: crate::panic_handler::PanicHandlerConfig,
}

/// Performance optimization configuration
#[cfg(feature = "performance-optimized")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    /// Enable string interning for common log field values
    pub enable_string_interning: bool,

    /// Initial capacity for string interner
    pub string_interner_capacity: usize,

    /// Enable buffer pooling for log formatting
    pub enable_buffer_pooling: bool,

    /// Initial buffer pool size
    pub buffer_pool_size: usize,

    /// Individual buffer capacity in bytes
    pub buffer_capacity: usize,

    /// Enable zero-allocation fast paths for hot operations
    pub enable_fast_paths: bool,

    /// Enable WASM-specific memory optimizations
    pub enable_wasm_optimizations: bool,
}

#[cfg(feature = "performance-optimized")]
impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_string_interning: true,
            string_interner_capacity: 1024,
            enable_buffer_pooling: true,
            buffer_pool_size: 16,
            buffer_capacity: 2048,
            enable_fast_paths: true,
            enable_wasm_optimizations: cfg!(target_arch = "wasm32"),
        }
    }
}

/// Correlation enhancement configuration
#[cfg(feature = "correlation-enhanced")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorrelationConfig {
    /// Enable W3C baggage support
    pub enable_baggage: bool,

    /// Maximum baggage size in bytes
    pub max_baggage_size: usize,

    /// Enable scoped context management
    pub enable_scoped_context: bool,

    /// Maximum context nesting depth
    pub max_context_depth: usize,

    /// Enable automatic context propagation
    pub enable_auto_propagation: bool,
}

#[cfg(feature = "correlation-enhanced")]
impl Default for CorrelationConfig {
    fn default() -> Self {
        Self {
            enable_baggage: true,
            max_baggage_size: 8192,
            enable_scoped_context: true,
            max_context_depth: 16,
            enable_auto_propagation: true,
        }
    }
}

/// Convenience feature configuration
#[cfg(feature = "convenience")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvenienceConfig {
    /// Enable LLM operation logging
    pub enable_llm_logging: bool,

    /// Enable template rendering logging
    pub enable_template_logging: bool,

    /// Enable A2A message logging
    pub enable_a2a_logging: bool,

    /// Enable convenience macros
    pub enable_convenience_macros: bool,

    /// Enable domain-specific field extraction
    pub enable_domain_fields: bool,
}

#[cfg(feature = "convenience")]
impl Default for ConvenienceConfig {
    fn default() -> Self {
        Self {
            enable_llm_logging: true,
            enable_template_logging: true,
            enable_a2a_logging: true,
            enable_convenience_macros: true,
            enable_domain_fields: true,
        }
    }
}

impl EnhancedObservabilityConfig {
    /// Create enhanced config with specified base config
    pub fn with_base(base: ObservabilityConfig) -> Self {
        Self {
            base,
            ..Default::default()
        }
    }

    /// Set panic handler configuration  
    pub fn with_panic_handler(mut self, config: crate::panic_handler::PanicHandlerConfig) -> Self {
        self.panic_handler = config;
        self
    }

    /// Enable all performance features (feature-gated)
    #[cfg(feature = "performance-optimized")]
    pub fn with_all_performance_features(mut self) -> Self {
        self.performance.enable_string_interning = true;
        self.performance.enable_buffer_pooling = true;
        self.performance.enable_fast_paths = true;
        self.performance.enable_wasm_optimizations = true;
        self
    }

    /// Disable all performance features (feature-gated)
    #[cfg(feature = "performance-optimized")]
    pub fn with_no_performance_features(mut self) -> Self {
        self.performance = PerformanceConfig::default();
        self
    }

    /// Convert to base observability config
    pub fn to_base_config(&self) -> ObservabilityConfig {
        self.base.clone()
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        // Additional validation for enhanced features
        #[cfg(feature = "performance-optimized")]
        {
            if self.performance.buffer_pool_size == 0 {
                return Err(StructuredLoggingError::enhanced_config(
                    "Buffer pool size must be greater than 0",
                ));
            }

            if self.performance.buffer_capacity == 0 {
                return Err(StructuredLoggingError::enhanced_config(
                    "Buffer capacity must be greater than 0",
                ));
            }
        }

        Ok(())
    }

    /// Serialize configuration to JSON
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string_pretty(self).map_err(|e| {
            StructuredLoggingError::enhanced_config(format!("Failed to serialize config: {}", e))
        })
    }

    /// Deserialize configuration from JSON
    pub fn from_json(json: &str) -> Result<Self> {
        serde_json::from_str(json).map_err(|e| {
            StructuredLoggingError::enhanced_config(format!("Failed to deserialize config: {}", e))
        })
    }
}

/// Enhanced observability manager with performance and convenience features
pub struct PerformanceExtension {
    /// Base observability manager
    base: ObservabilityManager,

    /// Enhanced configuration
    config: EnhancedObservabilityConfig,

    /// Performance features
    #[cfg(feature = "performance-optimized")]
    performance: Option<crate::performance::PerformanceManager>,

    /// Correlation features
    #[cfg(feature = "correlation-enhanced")]
    correlation: Option<crate::correlation::CorrelationManager>,

    /// Convenience features
    #[cfg(feature = "convenience")]
    convenience: Option<crate::convenience::ConvenienceManager>,
}

impl PerformanceExtension {
    /// Create new enhanced extension
    pub fn new(config: EnhancedObservabilityConfig) -> Result<Self> {
        let base =
            ObservabilityManager::new(config.base.clone()).map_err(StructuredLoggingError::from)?;

        let mut extension = Self {
            base,
            config: config.clone(),

            #[cfg(feature = "performance-optimized")]
            performance: None,

            #[cfg(feature = "correlation-enhanced")]
            correlation: None,

            #[cfg(feature = "convenience")]
            convenience: None,
        };

        // Initialize optional feature modules
        extension.initialize_features()?;

        Ok(extension)
    }

    /// Initialize all configured features
    pub fn initialize(&mut self) -> Result<()> {
        self.base
            .initialize()
            .map_err(StructuredLoggingError::from)?;
        self.initialize_features()?;
        Ok(())
    }

    /// Initialize feature modules based on configuration
    fn initialize_features(&mut self) -> Result<()> {
        #[cfg(feature = "performance-optimized")]
        {
            if self.is_performance_enabled() {
                let manager =
                    crate::performance::PerformanceManager::new(&self.config.performance)?;
                self.performance = Some(manager);
            }
        }

        #[cfg(feature = "correlation-enhanced")]
        {
            if self.is_correlation_enabled() {
                let manager =
                    crate::correlation::CorrelationManager::new(&self.config.correlation)?;
                self.correlation = Some(manager);
            }
        }

        #[cfg(feature = "convenience")]
        {
            if self.is_convenience_enabled() {
                let manager = crate::convenience::ConvenienceManager::new()?;
                self.convenience = Some(manager);
            }
        }

        // Install panic handler if configured (idempotent — silently succeeds if already installed)
        if self.config.panic_handler.enable_structured_logging {
            let _ = crate::panic_handler::install_panic_handler_with_config(
                self.config.panic_handler.clone(),
            );
        }

        Ok(())
    }

    /// Check if performance features are enabled
    #[cfg(feature = "performance-optimized")]
    pub fn is_performance_enabled(&self) -> bool {
        self.config.performance.enable_string_interning
            || self.config.performance.enable_buffer_pooling
            || self.config.performance.enable_fast_paths
    }

    #[cfg(not(feature = "performance-optimized"))]
    pub fn is_performance_enabled(&self) -> bool {
        false
    }

    /// Check if correlation features are enabled
    #[cfg(feature = "correlation-enhanced")]
    pub fn is_correlation_enabled(&self) -> bool {
        self.config.correlation.enable_baggage || self.config.correlation.enable_scoped_context
    }

    #[cfg(not(feature = "correlation-enhanced"))]
    pub fn is_correlation_enabled(&self) -> bool {
        false
    }

    /// Check if convenience features are enabled
    #[cfg(feature = "convenience")]
    pub fn is_convenience_enabled(&self) -> bool {
        self.config.convenience.enable_llm_logging
            || self.config.convenience.enable_template_logging
            || self.config.convenience.enable_a2a_logging
    }

    #[cfg(not(feature = "convenience"))]
    pub fn is_convenience_enabled(&self) -> bool {
        false
    }

    /// Get base observability manager
    pub fn base(&self) -> &ObservabilityManager {
        &self.base
    }

    /// Get enhanced configuration
    pub fn config(&self) -> &EnhancedObservabilityConfig {
        &self.config
    }

    /// Process log entry through all enabled features
    pub fn process_log_entry(&self, entry: LogEntry) -> Result<LogEntry> {
        let mut processed_entry = entry;

        // Apply performance optimizations
        #[cfg(feature = "performance-optimized")]
        if let Some(ref perf) = self.performance {
            processed_entry = perf.process_entry(processed_entry)?;
        }

        // Apply correlation enhancements
        #[cfg(feature = "correlation-enhanced")]
        if let Some(ref corr) = self.correlation {
            processed_entry = corr.process_entry(processed_entry)?;
        }

        // Apply convenience enhancements
        #[cfg(feature = "convenience")]
        if let Some(ref conv) = self.convenience {
            processed_entry = conv.process_entry(processed_entry)?;
        }

        Ok(processed_entry)
    }

    /// Get performance statistics (if enabled)
    #[cfg(feature = "performance-optimized")]
    pub fn get_performance_stats(&self) -> Option<crate::performance::PerformanceStats> {
        self.performance.as_ref().map(|p| p.get_stats())
    }

    /// Reset performance statistics (if enabled)
    #[cfg(feature = "performance-optimized")]
    pub fn reset_performance_stats(&self) -> Result<()> {
        if let Some(ref perf) = self.performance {
            perf.reset_stats()?;
        }
        Ok(())
    }

    /// Get panic statistics if panic handler is enabled
    pub fn get_panic_stats(&self) -> Result<crate::panic_handler::PanicStats> {
        crate::panic_handler::get_panic_stats()
    }

    /// Reset panic statistics if panic handler is enabled
    pub fn reset_panic_stats(&self) -> Result<()> {
        crate::panic_handler::reset_panic_stats()
    }

    /// Get manager capabilities
    pub fn capabilities(&self) -> Vec<&'static str> {
        let mut caps = vec!["enhanced_logging"];

        if self.is_performance_enabled() {
            caps.push("performance_optimization");
        }
        if self.is_correlation_enabled() {
            caps.push("correlation_enhancement");
        }
        if self.is_convenience_enabled() {
            caps.push("convenience_apis");
        }
        if self.config.panic_handler.enable_structured_logging {
            caps.push("panic_handling");
        }

        caps
    }
}

/// Create performance extension from configuration
pub fn create_performance_extension(
    config: EnhancedObservabilityConfig,
) -> Result<PerformanceExtension> {
    PerformanceExtension::new(config)
}

/// Create performance extension from configuration file
pub fn create_performance_extension_from_config(config_path: &str) -> Result<PerformanceExtension> {
    let config_str = std::fs::read_to_string(config_path).map_err(|e| {
        StructuredLoggingError::enhanced_config(format!("Failed to read config file: {}", e))
    })?;
    let config = EnhancedObservabilityConfig::from_json(&config_str)?;
    PerformanceExtension::new(config)
}

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

    #[test]
    fn test_enhanced_config_default() {
        let config = EnhancedObservabilityConfig::default();

        // Verify base config exists
        assert_eq!(config.base.level, "info");
        assert_eq!(config.base.format, "compact");
        assert!(config.base.structured);
    }

    #[test]
    fn test_enhanced_config_with_base() {
        let base = ObservabilityConfig {
            level: "debug".to_string(),
            format: "json".to_string(),
            structured: true,
            context_enrichment: true,
            default_context: std::collections::HashMap::new(),
        };

        let config = EnhancedObservabilityConfig::with_base(base);
        assert_eq!(config.base.level, "debug");
        assert_eq!(config.base.format, "json");
    }

    #[cfg(feature = "performance-optimized")]
    #[test]
    fn test_performance_config_default() {
        let config = PerformanceConfig::default();
        assert!(config.enable_string_interning);
        assert!(config.enable_buffer_pooling);
        assert!(config.enable_fast_paths);
    }

    #[test]
    fn test_performance_extension_creation() {
        let config = EnhancedObservabilityConfig::default();
        let result = PerformanceExtension::new(config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_config_serialization() {
        let config = EnhancedObservabilityConfig::default();
        let json = config.to_json().unwrap();
        let restored = EnhancedObservabilityConfig::from_json(&json).unwrap();

        assert_eq!(config.base.level, restored.base.level);
        assert_eq!(config.base.format, restored.base.format);
    }
}