nu_plugin_secret 0.7.0

Production-grade secret handling plugin for Nushell with secure CustomValue types that prevent accidental exposure of sensitive data
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
//! Performance monitoring system for nu_plugin_secret
//!
//! This module provides real-time performance monitoring capabilities
//! to track plugin performance, detect regressions, and gather metrics.

use std::collections::{HashMap, VecDeque};
use std::sync::RwLock;
use std::time::Instant;

/// Performance metric types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MetricType {
    /// Time to create a secret value
    SecretCreation,
    /// Time to reveal (unwrap) a secret value
    SecretReveal,
    /// Time to display/format a secret value
    SecretDisplay,
    /// Time to serialize a secret value
    SecretSerialization,
    /// Time to deserialize a secret value  
    SecretDeserialization,
    /// Memory usage in bytes
    MemoryUsage,
    /// Plugin startup time
    StartupTime,
    /// Command execution time
    CommandExecution,
}

/// Individual performance measurement
#[derive(Debug, Clone)]
pub struct Measurement {
    pub metric_type: MetricType,
    pub value: f64,
    pub unit: Unit,
    pub timestamp: Instant,
    pub context: Option<String>,
}

/// Measurement units
#[derive(Debug, Clone, PartialEq)]
pub enum Unit {
    Milliseconds,
    Microseconds,
    Nanoseconds,
    Bytes,
    Kilobytes,
    Megabytes,
    Operations,
}

/// Performance statistics
#[derive(Debug, Clone)]
pub struct Statistics {
    pub count: usize,
    pub min: f64,
    pub max: f64,
    pub mean: f64,
    pub median: f64,
    pub std_dev: f64,
    pub p95: f64,
    pub p99: f64,
}

/// Performance monitoring configuration
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
    /// Maximum number of measurements to keep in memory
    pub max_measurements: usize,
    /// Enable detailed timing for all operations
    pub detailed_timing: bool,
    /// Enable memory usage tracking
    pub track_memory: bool,
    /// Performance regression threshold (as percentage)
    pub regression_threshold: f64,
    /// Export metrics to file
    pub export_metrics: bool,
    /// Export file path
    pub export_path: Option<String>,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            max_measurements: 1000,
            detailed_timing: false,
            track_memory: true,
            regression_threshold: 20.0, // 20% regression threshold
            export_metrics: false,
            export_path: None,
        }
    }
}

/// Global performance monitor
pub struct PerformanceMonitor {
    measurements: RwLock<HashMap<MetricType, VecDeque<Measurement>>>,
    config: RwLock<MonitoringConfig>,
    baselines: RwLock<HashMap<MetricType, Statistics>>,
}

impl PerformanceMonitor {
    pub fn new(config: MonitoringConfig) -> Self {
        Self {
            measurements: RwLock::new(HashMap::new()),
            config: RwLock::new(config),
            baselines: RwLock::new(HashMap::new()),
        }
    }

    /// Record a performance measurement
    pub fn record(&self, measurement: Measurement) {
        let config = self.config.read().unwrap();

        if !config.detailed_timing
            && !matches!(
                measurement.metric_type,
                MetricType::MemoryUsage | MetricType::StartupTime
            )
        {
            return;
        }

        let mut measurements = self.measurements.write().unwrap();
        let entries = measurements
            .entry(measurement.metric_type.clone())
            .or_default();

        entries.push_back(measurement);

        // Keep only the most recent measurements
        while entries.len() > config.max_measurements {
            entries.pop_front();
        }
    }

    /// Record timing for a closure
    pub fn time<T, F>(&self, metric_type: MetricType, context: Option<String>, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();

        self.record(Measurement {
            metric_type,
            value: duration.as_nanos() as f64,
            unit: Unit::Nanoseconds,
            timestamp: start,
            context,
        });

        result
    }

    /// Get statistics for a metric type
    pub fn get_statistics(&self, metric_type: &MetricType) -> Option<Statistics> {
        let measurements = self.measurements.read().unwrap();
        let entries = measurements.get(metric_type)?;

        if entries.is_empty() {
            return None;
        }

        let mut values: Vec<f64> = entries.iter().map(|m| m.value).collect();
        values.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let count = values.len();
        let min = values[0];
        let max = values[count - 1];
        let mean = values.iter().sum::<f64>() / count as f64;

        let median = if count.is_multiple_of(2) {
            (values[count / 2 - 1] + values[count / 2]) / 2.0
        } else {
            values[count / 2]
        };

        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / count as f64;
        let std_dev = variance.sqrt();

        let p95_idx = ((count as f64 * 0.95) as usize).min(count - 1);
        let p99_idx = ((count as f64 * 0.99) as usize).min(count - 1);

        Some(Statistics {
            count,
            min,
            max,
            mean,
            median,
            std_dev,
            p95: values[p95_idx],
            p99: values[p99_idx],
        })
    }

    /// Set baseline statistics for regression detection
    pub fn set_baseline(&self, metric_type: MetricType, baseline: Statistics) {
        let mut baselines = self.baselines.write().unwrap();
        baselines.insert(metric_type, baseline);
    }

    /// Check for performance regressions
    pub fn check_regressions(&self) -> Vec<RegressionAlert> {
        let mut alerts = Vec::new();
        let config = self.config.read().unwrap();
        let baselines = self.baselines.read().unwrap();

        for (metric_type, baseline) in baselines.iter() {
            if let Some(current) = self.get_statistics(metric_type) {
                let regression_pct = ((current.mean - baseline.mean) / baseline.mean) * 100.0;

                if regression_pct > config.regression_threshold {
                    alerts.push(RegressionAlert {
                        metric_type: metric_type.clone(),
                        regression_percentage: regression_pct,
                        baseline_mean: baseline.mean,
                        current_mean: current.mean,
                        threshold: config.regression_threshold,
                    });
                }
            }
        }

        alerts
    }

    /// Export current metrics to a file
    pub fn export_metrics(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
        use std::fs::File;
        use std::io::Write;

        let measurements = self.measurements.read().unwrap();
        let mut file = File::create(path)?;

        writeln!(file, "timestamp,metric_type,value,unit,context")?;

        for (metric_type, entries) in measurements.iter() {
            for measurement in entries.iter() {
                writeln!(
                    file,
                    "{},{:?},{},{:?},{}",
                    measurement.timestamp.elapsed().as_millis(),
                    metric_type,
                    measurement.value,
                    measurement.unit,
                    measurement.context.as_deref().unwrap_or("")
                )?;
            }
        }

        Ok(())
    }

    /// Generate performance report
    pub fn generate_report(&self) -> PerformanceReport {
        let config = self.config.read().unwrap();
        let measurements = self.measurements.read().unwrap();

        let mut stats_by_metric = HashMap::new();
        for metric_type in measurements.keys() {
            if let Some(stats) = self.get_statistics(metric_type) {
                stats_by_metric.insert(metric_type.clone(), stats);
            }
        }

        let regressions = self.check_regressions();

        PerformanceReport {
            timestamp: Instant::now(),
            statistics: stats_by_metric,
            regressions,
            total_measurements: measurements.values().map(|v| v.len()).sum(),
            monitoring_config: config.clone(),
        }
    }
}

/// Performance regression alert
#[derive(Debug, Clone)]
pub struct RegressionAlert {
    pub metric_type: MetricType,
    pub regression_percentage: f64,
    pub baseline_mean: f64,
    pub current_mean: f64,
    pub threshold: f64,
}

/// Performance report
#[derive(Debug, Clone)]
pub struct PerformanceReport {
    pub timestamp: Instant,
    pub statistics: HashMap<MetricType, Statistics>,
    pub regressions: Vec<RegressionAlert>,
    pub total_measurements: usize,
    pub monitoring_config: MonitoringConfig,
}

/// Global performance monitor instance
static GLOBAL_MONITOR: std::sync::OnceLock<PerformanceMonitor> = std::sync::OnceLock::new();

/// Initialize the global performance monitor
pub fn init_monitoring(config: MonitoringConfig) {
    GLOBAL_MONITOR
        .set(PerformanceMonitor::new(config))
        .map_err(|_| "Monitor already initialized")
        .unwrap();
}

/// Get the global performance monitor
pub fn get_monitor() -> &'static PerformanceMonitor {
    GLOBAL_MONITOR.get().unwrap_or_else(|| {
        init_monitoring(MonitoringConfig::default());
        GLOBAL_MONITOR.get().unwrap()
    })
}

/// Convenience macros for performance monitoring
///
/// Time a block of code
#[macro_export]
macro_rules! time_block {
    ($metric_type:expr, $context:expr, $block:block) => {
        $crate::performance_monitoring::get_monitor().time(
            $metric_type,
            Some($context.to_string()),
            || $block,
        )
    };
    ($metric_type:expr, $block:block) => {
        $crate::performance_monitoring::get_monitor().time($metric_type, None, || $block)
    };
}

/// Record a measurement
#[macro_export]
macro_rules! record_metric {
    ($metric_type:expr, $value:expr, $unit:expr) => {
        $crate::performance_monitoring::get_monitor().record(
            $crate::performance_monitoring::Measurement {
                metric_type: $metric_type,
                value: $value,
                unit: $unit,
                timestamp: std::time::Instant::now(),
                context: None,
            },
        );
    };
    ($metric_type:expr, $value:expr, $unit:expr, $context:expr) => {
        $crate::performance_monitoring::get_monitor().record(
            $crate::performance_monitoring::Measurement {
                metric_type: $metric_type,
                value: $value,
                unit: $unit,
                timestamp: std::time::Instant::now(),
                context: Some($context.to_string()),
            },
        );
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_performance_monitor() {
        let config = MonitoringConfig {
            detailed_timing: true,
            ..MonitoringConfig::default()
        };
        let monitor = PerformanceMonitor::new(config);

        // Record some measurements
        monitor.record(Measurement {
            metric_type: MetricType::SecretCreation,
            value: 100.0,
            unit: Unit::Nanoseconds,
            timestamp: Instant::now(),
            context: Some("test".to_string()),
        });

        monitor.record(Measurement {
            metric_type: MetricType::SecretCreation,
            value: 150.0,
            unit: Unit::Nanoseconds,
            timestamp: Instant::now(),
            context: Some("test".to_string()),
        });

        // Get statistics
        let stats = monitor.get_statistics(&MetricType::SecretCreation).unwrap();
        assert_eq!(stats.count, 2);
        assert_eq!(stats.min, 100.0);
        assert_eq!(stats.max, 150.0);
        assert_eq!(stats.mean, 125.0);
    }

    #[test]
    fn test_timing_function() {
        let config = MonitoringConfig {
            detailed_timing: true,
            ..MonitoringConfig::default()
        };
        let monitor = PerformanceMonitor::new(config);

        let result = monitor.time(
            MetricType::SecretCreation,
            Some("test_timing".to_string()),
            || {
                thread::sleep(Duration::from_millis(1));
                42
            },
        );

        assert_eq!(result, 42);

        let stats = monitor.get_statistics(&MetricType::SecretCreation).unwrap();
        assert_eq!(stats.count, 1);
        assert!(stats.mean > 0.0);
    }

    #[test]
    fn test_regression_detection() {
        let config = MonitoringConfig {
            regression_threshold: 10.0,
            detailed_timing: true,
            ..MonitoringConfig::default()
        };
        let monitor = PerformanceMonitor::new(config);

        // Set baseline
        let baseline = Statistics {
            count: 100,
            min: 90.0,
            max: 110.0,
            mean: 100.0,
            median: 100.0,
            std_dev: 5.0,
            p95: 108.0,
            p99: 109.0,
        };
        monitor.set_baseline(MetricType::SecretCreation, baseline);

        // Add measurements that exceed threshold
        for _ in 0..10 {
            monitor.record(Measurement {
                metric_type: MetricType::SecretCreation,
                value: 120.0, // 20% increase
                unit: Unit::Nanoseconds,
                timestamp: Instant::now(),
                context: None,
            });
        }

        let regressions = monitor.check_regressions();
        assert_eq!(regressions.len(), 1);
        assert!(regressions[0].regression_percentage > 10.0);
    }
}