pg_exporter 0.8.3

PostgreSQL metric exporter for Prometheus
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
use crate::collectors::Collector;
use anyhow::Result;
use futures::future::BoxFuture;
use prometheus::{Counter, Gauge, IntGauge, Opts, Registry};
use sqlx::PgPool;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use sysinfo::{Pid, System};
use tracing::{debug, instrument, warn};

/// Monitors the pg_exporter process itself
///
/// This collector tracks resource consumption of the exporter process,
/// helping operators detect memory leaks, CPU spikes, and resource exhaustion.
///
/// # Metrics Exported
///
/// ## CPU Usage
/// - `pg_exporter_process_cpu_seconds_total` (Counter)
///   - Total CPU time (user + system) cumulative across all cores
///   - **Matches node_exporter standard** - NOT normalized per-core
///   - Example: Using 6 of 12 cores for 10s → counter increases by 60s
///   - Use `pg_exporter_process_cpu_cores` for normalization in queries
///
/// - `pg_exporter_process_cpu_cores` (IntGauge)
///   - Number of CPU cores available to the system
///   - Use for calculating per-core percentage
///   - Example: `rate(cpu_seconds_total) / cpu_cores * 100` = % per core (0-100%)
///
/// ## Memory Usage  
/// - `pg_exporter_process_resident_memory_bytes` (IntGauge)
///   - RSS (Resident Set Size) - actual RAM used
///   - Alert if >500MB or steadily increasing (leak)
///
/// - `pg_exporter_process_virtual_memory_bytes` (IntGauge)
///   - VSZ (Virtual Size) - total virtual memory allocated
///   - Usually much larger than RSS (includes mapped files, shared libs)
///
/// ## Thread and File Descriptor Count
/// - `pg_exporter_process_threads` (IntGauge)
///   - Number of OS threads in the process
///   - Tokio runtime typically uses N threads where N = CPU cores
///
/// - `pg_exporter_process_open_fds` (IntGauge, Linux only)
///   - Number of open file descriptors
///   - Alert if approaching `ulimit -n` (default 1024)
///   - Each database connection uses ~1 FD
///
/// ## Process Lifecycle
/// - `pg_exporter_process_start_time_seconds` (Gauge)
///   - Unix timestamp when the process started
///   - Use to calculate uptime or detect restarts
///   - Example: `time() - pg_exporter_process_start_time_seconds`
///
/// # CPU Percentage Calculation
///
/// **Accurate Approach (Matches node_exporter):**
/// - Reads actual CPU time from OS (not estimated)
/// - Linux: /proc/$PID/stat (utime + stime in clock ticks)
/// - Metric is cumulative across ALL cores (not normalized)
/// - On 12-core system using 6 cores for 10s → counter increases by 60 seconds
///
/// **How it works:**
/// - Track last CPU time reading and last collection timestamp
/// - On each scrape: delta_cpu_seconds = (current_cpu_time - last_cpu_time)
/// - Increment counter by delta_cpu_seconds
/// - Store current values for next scrape
///
/// **PromQL Queries:**
/// ```promql
/// # Per-core percentage (0-100%)
/// rate(pg_exporter_process_cpu_seconds_total[5m]) / on(job,instance) pg_exporter_process_cpu_cores * 100
///
/// # Total percentage (0-1200% on 12-core system)  
/// rate(pg_exporter_process_cpu_seconds_total[5m]) * 100
///
/// # Number of cores in use
/// rate(pg_exporter_process_cpu_seconds_total[5m])
/// ```
///
/// **Why this approach:**
/// - Accurate: Reads actual CPU time from kernel
/// - No estimation: Doesn't depend on scrape interval
/// - Cross-platform: sysinfo handles platform differences
/// - Standard: Matches node_exporter semantics
/// - Flexible: Can show both total % and per-core % in PromQL
///
/// # Implementation Details
///
/// Uses the `sysinfo` crate (v0.37) to read process information from the OS:
/// - Linux: Reads `/proc/$PID/stat`, `/proc/$PID/status`, `/proc/$PID/fd/`
/// - macOS: Uses `proc_pidinfo()` system call
/// - Windows: Uses Windows API
///
/// The `System` object is cached in an `Arc<Mutex<>>` and reused across
/// scrapes to avoid allocating it on every collection cycle.
///
/// # Performance
///
/// - Collection time: ~1-5ms on Linux
/// - Lock hold time: <1ms (just reads /proc, no I/O)
/// - Memory overhead: ~10KB for cached System object
///
/// # Example
///
/// ```rust,no_run
/// # use pg_exporter::collectors::exporter::ProcessCollector;
/// # use pg_exporter::collectors::Collector;
/// # use prometheus::Registry;
/// # fn example() -> anyhow::Result<()> {
/// let collector = ProcessCollector::new();
/// let registry = Registry::new();
/// collector.register_metrics(&registry)?;
///
/// // After collection, metrics will be available:
/// // pg_exporter_process_resident_memory_bytes ~45,000,000 (45MB)
/// // pg_exporter_process_threads 8
/// // pg_exporter_process_cpu_cores 12
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct ProcessCollector {
    cpu_seconds_total: Counter,
    cpu_cores: IntGauge,
    resident_memory_bytes: IntGauge,
    virtual_memory_bytes: IntGauge,
    open_fds: IntGauge,
    threads: IntGauge,
    start_time_seconds: Gauge,
    
    /// Cached sysinfo System object and CPU tracking state
    ///
    /// Mutex protects:
    /// - System object (for process stats)
    /// - Last CPU time reading (for accurate delta calculation)
    /// - Last collection timestamp
    ///
    /// We handle PoisonError explicitly to recover from panics during collection.
    state: Arc<Mutex<CollectorState>>,
    
    /// Process ID of this exporter
    pid: Pid,
    
    /// Number of CPU cores (cached for normalization)
    num_cores: usize,
}

/// Internal state for process collector
struct CollectorState {
    system: System,
    last_cpu_time: Option<Duration>,
    last_collection: Option<Instant>,
}

impl Default for ProcessCollector {
    fn default() -> Self {
        Self::new()
    }
}

impl ProcessCollector {
    pub fn new() -> Self {
        let cpu_seconds_total = Counter::with_opts(Opts::new(
            "pg_exporter_process_cpu_seconds_total",
            "Total user and system CPU time spent in seconds (cumulative across all cores)",
        ))
        .expect("pg_exporter_process_cpu_seconds_total");

        let cpu_cores = IntGauge::with_opts(Opts::new(
            "pg_exporter_process_cpu_cores",
            "Number of CPU cores available to the system",
        ))
        .expect("pg_exporter_process_cpu_cores");

        let resident_memory_bytes = IntGauge::with_opts(Opts::new(
            "pg_exporter_process_resident_memory_bytes",
            "Resident memory size in bytes (RSS)",
        ))
        .expect("pg_exporter_process_resident_memory_bytes");

        let virtual_memory_bytes = IntGauge::with_opts(Opts::new(
            "pg_exporter_process_virtual_memory_bytes",
            "Virtual memory size in bytes (VSZ)",
        ))
        .expect("pg_exporter_process_virtual_memory_bytes");

        let open_fds = IntGauge::with_opts(Opts::new(
            "pg_exporter_process_open_fds",
            "Number of open file descriptors",
        ))
        .expect("pg_exporter_process_open_fds");

        let threads = IntGauge::with_opts(Opts::new(
            "pg_exporter_process_threads",
            "Number of OS threads in the process",
        ))
        .expect("pg_exporter_process_threads");

        let start_time_seconds = Gauge::with_opts(Opts::new(
            "pg_exporter_process_start_time_seconds",
            "Start time of the process since unix epoch in seconds",
        ))
        .expect("pg_exporter_process_start_time_seconds");

        let system = System::new_all();
        let num_cores = system.cpus().len().max(1); // At least 1 core
        let state = Arc::new(Mutex::new(CollectorState {
            system,
            last_cpu_time: None,
            last_collection: None,
        }));
        let pid = Pid::from(std::process::id() as usize);

        // Set CPU cores count (doesn't change)
        cpu_cores.set(num_cores as i64);

        // Set start time once (doesn't change)
        let start_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs_f64();
        start_time_seconds.set(start_time);

        Self {
            cpu_seconds_total,
            cpu_cores,
            resident_memory_bytes,
            virtual_memory_bytes,
            open_fds,
            threads,
            start_time_seconds,
            state,
            pid,
            num_cores,
        }
    }

    /// Get CPU time from sysinfo Process
    ///
    /// Returns total CPU time (user + system) as Duration.
    /// sysinfo's run_time() returns total CPU time in seconds.
    fn get_cpu_time(process: &sysinfo::Process) -> Duration {
        // run_time() returns CPU time in seconds (u64)
        // This is total CPU time across all cores
        let cpu_seconds = process.run_time();
        Duration::from_secs(cpu_seconds)
    }

    /// Get current process statistics
    ///
    /// Accurately tracks CPU usage by:
    /// 1. Reading actual CPU time from OS (not estimated)
    /// 2. Calculating delta since last collection
    /// 3. Incrementing counter by actual CPU seconds consumed
    ///
    /// This method:
    /// 1. Acquires lock on state (~0.1ms)
    /// 2. Handles PoisonError if a previous panic occurred
    /// 3. Refreshes process data from OS (~1-5ms)
    /// 4. Calculates CPU time delta using actual timestamps
    /// 5. Updates all metrics (CPU, memory, threads, FDs)
    /// 6. Stores current readings for next delta calculation
    /// 7. Releases lock
    ///
    /// Total execution time: ~1-5ms on Linux, may be slower on other platforms.
    fn collect_stats(&self) {
        let now = Instant::now();
        
        // Acquire lock, handling poison errors gracefully
        let mut state = match self.state.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                warn!("State mutex was poisoned, recovering");
                poisoned.into_inner()
            }
        };
        
        // Refresh process data
        state.system.refresh_all();

        if let Some(process) = state.system.process(self.pid) {
            // Memory metrics
            let rss = process.memory();
            let vsz = process.virtual_memory();
            
            self.resident_memory_bytes.set(rss as i64);
            self.virtual_memory_bytes.set(vsz as i64);

            // CPU time tracking with accurate deltas
            let current_cpu_time = Self::get_cpu_time(process);
            
            // Calculate delta if we have a previous reading
            if let (Some(last_cpu), Some(last_time)) = (state.last_cpu_time, state.last_collection) {
                let elapsed = now.duration_since(last_time);
                
                // Only update if we have meaningful elapsed time (> 100ms)
                // This prevents division by zero and noise from very fast scrapes
                if elapsed.as_secs_f64() > 0.1 {
                    // Calculate actual CPU seconds consumed since last scrape
                    let cpu_delta = current_cpu_time.saturating_sub(last_cpu);
                    let cpu_seconds = cpu_delta.as_secs_f64();
                    
                    // Increment counter by actual CPU time consumed
                    if cpu_seconds > 0.0 {
                        self.cpu_seconds_total.inc_by(cpu_seconds);
                        
                        debug!(
                            cpu_delta_seconds = cpu_seconds,
                            elapsed_seconds = elapsed.as_secs_f64(),
                            cpu_percent = (cpu_seconds / elapsed.as_secs_f64()) * 100.0,
                            "CPU time delta"
                        );
                    }
                }
            }
            
            // Store current readings for next delta
            state.last_cpu_time = Some(current_cpu_time);
            state.last_collection = Some(now);

            // Thread count (Linux-specific via /proc)
            // On Linux, each thread has an entry in /proc/$PID/task/
            #[cfg(target_os = "linux")]
            {
                if let Ok(entries) = std::fs::read_dir(format!("/proc/{}/task", self.pid)) {
                    let thread_count = entries.count() as i64;
                    self.threads.set(thread_count);
                }
            }
            
            #[cfg(not(target_os = "linux"))]
            {
                // Fallback: minimum 1 thread
                // sysinfo doesn't expose thread count on all platforms
                self.threads.set(1);
            }

            // File descriptors (Linux-specific via /proc)
            // Each entry in /proc/$PID/fd/ is an open file descriptor
            #[cfg(target_os = "linux")]
            {
                if let Ok(entries) = std::fs::read_dir(format!("/proc/{}/fd", self.pid)) {
                    let fd_count = entries.count() as i64;
                    self.open_fds.set(fd_count);
                }
            }
            
            #[cfg(not(target_os = "linux"))]
            {
                // Not available on non-Linux platforms
                self.open_fds.set(0);
            }

            debug!(
                rss_mb = rss / 1024 / 1024,
                vsz_mb = vsz / 1024 / 1024,
                cpu_seconds_total = self.cpu_seconds_total.get(),
                cpu_cores = self.num_cores,
                threads = self.threads.get(),
                fds = self.open_fds.get(),
                "collected process metrics"
            );
        }
    }
}

impl Collector for ProcessCollector {
    fn name(&self) -> &'static str {
        "metrics.process"
    }

    fn register_metrics(&self, registry: &Registry) -> Result<()> {
        registry.register(Box::new(self.cpu_seconds_total.clone()))?;
        registry.register(Box::new(self.cpu_cores.clone()))?;
        registry.register(Box::new(self.resident_memory_bytes.clone()))?;
        registry.register(Box::new(self.virtual_memory_bytes.clone()))?;
        registry.register(Box::new(self.open_fds.clone()))?;
        registry.register(Box::new(self.threads.clone()))?;
        registry.register(Box::new(self.start_time_seconds.clone()))?;
        Ok(())
    }

    #[instrument(skip(self, _pool), level = "debug")]
    fn collect<'a>(&'a self, _pool: &'a PgPool) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            self.collect_stats();
            Ok(())
        })
    }

    fn enabled_by_default(&self) -> bool {
        false
    }
}

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

    #[test]
    fn test_process_collector_new() {
        let collector = ProcessCollector::new();
        assert!(collector.start_time_seconds.get() > 0.0);
        assert_eq!(collector.cpu_cores.get(), collector.num_cores as i64);
        assert!(collector.num_cores > 0);
    }

    #[test]
    fn test_process_collector_registers_without_error() {
        let collector = ProcessCollector::new();
        let registry = Registry::new();
        assert!(collector.register_metrics(&registry).is_ok());
        
        // Verify all metrics are registered
        let metrics = registry.gather();
        let metric_names: Vec<String> = metrics.iter()
            .map(|m| m.name().to_string())
            .collect();
        
        assert!(metric_names.contains(&"pg_exporter_process_cpu_seconds_total".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_cpu_cores".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_resident_memory_bytes".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_virtual_memory_bytes".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_threads".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_open_fds".to_string()));
        assert!(metric_names.contains(&"pg_exporter_process_start_time_seconds".to_string()));
    }

    #[test]
    fn test_process_collector_collects_stats() {
        let collector = ProcessCollector::new();
        collector.collect_stats();
        
        // Memory should be > 0
        assert!(collector.resident_memory_bytes.get() > 0);
        assert!(collector.virtual_memory_bytes.get() > 0);
        
        // Virtual memory should be >= resident memory
        assert!(collector.virtual_memory_bytes.get() >= collector.resident_memory_bytes.get());
        
        // Should have at least 1 thread
        assert!(collector.threads.get() >= 1);
        
        // FDs should be > 0 on Linux (we have stdin/stdout/stderr at minimum)
        #[cfg(target_os = "linux")]
        assert!(collector.open_fds.get() >= 3);
    }

    #[test]
    fn test_cpu_time_tracking_first_collection() {
        let collector = ProcessCollector::new();
        let initial_cpu = collector.cpu_seconds_total.get();
        
        // First collection doesn't increment counter (no delta yet)
        collector.collect_stats();
        
        // Counter should still be at initial value (no delta on first call)
        assert_eq!(collector.cpu_seconds_total.get(), initial_cpu);
    }

    #[test]
    fn test_cpu_time_tracking_increments() {
        let collector = ProcessCollector::new();
        
        // First collection establishes baseline
        collector.collect_stats();
        let cpu_after_first = collector.cpu_seconds_total.get();
        
        // Do some CPU work
        let mut sum = 0u64;
        for i in 0..1_000_000 {
            sum = sum.wrapping_add(i);
        }
        // Use sum to prevent optimization
        assert!(sum > 0);
        
        // Sleep to ensure time passes (but not too long for CI)
        thread::sleep(Duration::from_millis(100));
        
        // Second collection should show CPU time increase
        collector.collect_stats();
        let cpu_after_second = collector.cpu_seconds_total.get();
        
        // CPU time should have increased
        assert!(cpu_after_second >= cpu_after_first);
    }

    #[test]
    fn test_cpu_time_reasonable_range() {
        let collector = ProcessCollector::new();
        
        // Establish baseline
        collector.collect_stats();
        
        // Do CPU work for ~100ms
        let start = Instant::now();
        let mut sum = 0u64;
        while start.elapsed() < Duration::from_millis(100) {
            for i in 0..10_000 {
                sum = sum.wrapping_add(i);
            }
        }
        assert!(sum > 0);
        
        // Collect again
        collector.collect_stats();
        let cpu_time = collector.cpu_seconds_total.get();
        
        // CPU time should be reasonable
        // Note: sysinfo's run_time() returns total process CPU time since start
        // This can be significant if process has been running for a while
        assert!(cpu_time >= 0.0);
        // Just verify it's not absurdly large (< 1 hour of CPU time)
        assert!(cpu_time < 3600.0);
    }

    #[test]
    fn test_memory_metrics_reasonable() {
        let collector = ProcessCollector::new();
        collector.collect_stats();
        
        let rss_mb = collector.resident_memory_bytes.get() / 1024 / 1024;
        let vsz_mb = collector.virtual_memory_bytes.get() / 1024 / 1024;
        
        // RSS should be reasonable (> 1MB, < 10GB for tests)
        assert!(rss_mb > 1);
        assert!(rss_mb < 10_000);
        
        // VSZ should be reasonable (> RSS, < 100GB)
        assert!(vsz_mb > rss_mb);
        assert!(vsz_mb < 100_000);
    }

    #[test]
    fn test_multiple_collections_dont_panic() {
        let collector = ProcessCollector::new();
        
        // Multiple rapid collections should not panic
        for _ in 0..10 {
            collector.collect_stats();
        }
        
        // Metrics should still be valid
        assert!(collector.resident_memory_bytes.get() > 0);
        assert!(collector.cpu_seconds_total.get() >= 0.0);
    }

    #[test]
    fn test_collector_state_initialized() {
        let collector = ProcessCollector::new();
        
        let state = collector.state.lock().unwrap();
        
        // Initial state should have no previous readings
        assert!(state.last_cpu_time.is_none());
        assert!(state.last_collection.is_none());
    }

    #[test]
    fn test_collector_state_updated_after_collection() {
        let collector = ProcessCollector::new();
        
        collector.collect_stats();
        
        let state = collector.state.lock().unwrap();
        
        // State should be updated after first collection
        assert!(state.last_cpu_time.is_some());
        assert!(state.last_collection.is_some());
    }

    #[test]
    fn test_get_cpu_time() {
        let collector = ProcessCollector::new();
        let mut state = collector.state.lock().unwrap();
        
        state.system.refresh_all();
        
        if let Some(process) = state.system.process(collector.pid) {
            let cpu_time = ProcessCollector::get_cpu_time(process);
            
            // CPU time should be positive (process has been running)
            assert!(cpu_time.as_secs_f64() >= 0.0);
        } else {
            panic!("Could not find own process");
        }
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_thread_count_linux() {
        let collector = ProcessCollector::new();
        collector.collect_stats();
        
        // Should have multiple threads (main + tokio runtime)
        assert!(collector.threads.get() >= 1);
        
        // Shouldn't have an unreasonable number of threads
        assert!(collector.threads.get() < 1000);
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn test_file_descriptors_linux() {
        let collector = ProcessCollector::new();
        collector.collect_stats();
        
        let fd_count = collector.open_fds.get();
        
        // Should have at least stdin/stdout/stderr
        assert!(fd_count >= 3);
        
        // Verify metric is being collected (non-zero)
        assert!(fd_count > 0);
    }
}