pg_exporter 0.8.2

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
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::{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
///
/// **Node Exporter Approach (Standard):**
/// - Metric is cumulative across ALL cores (not normalized)
/// - On 12-core system using 6 cores for 10s → counter increases by 60 seconds
/// - We integrate sysinfo's `cpu_usage()` over time for simplicity
///
/// **How it works:**
/// - `cpu_usage()` returns instantaneous CPU% (e.g., 600% = 6 cores)
/// - We convert to cores: 600% / 100 = 6.0 cores
/// - Multiply by time interval: 6.0 cores × 5 seconds = 30 CPU-seconds
/// - Accumulate in counter for cumulative total
///
/// **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:**
/// - Simple: Uses only sysinfo crate (no /proc parsing)
/// - Cross-platform: Works on Linux, macOS, BSD
/// - 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, protected by std::sync::Mutex
    ///
    /// Mutex allows safe concurrent access to the System object. We handle
    /// PoisonError explicitly to recover from panics during collection.
    ///
    /// If a panic occurs while holding the lock:
    /// - The lock becomes "poisoned"
    /// - We detect this and recover via `into_inner()`
    /// - A warning is logged, but collection continues
    /// - This prevents one bad scrape from breaking all future scrapes
    system: Arc<Mutex<System>>,
    
    /// Process ID of this exporter
    pid: Pid,
    
    /// Number of CPU cores (cached for normalization)
    num_cores: usize,
}

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 system = Arc::new(Mutex::new(system));
        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,
            system,
            pid,
            num_cores,
        }
    }

    /// Get current process statistics
    ///
    /// Reads process information from the operating system:
    /// - Linux: /proc/$PID/stat, /proc/$PID/status, /proc/$PID/fd/
    /// - macOS: proc_pidinfo() system call
    /// - Windows: Windows API
    ///
    /// This method:
    /// 1. Acquires a lock on the cached System object (~0.1ms)
    /// 2. Handles PoisonError if a previous panic occurred
    /// 3. Refreshes process data from OS (~1-5ms) using sysinfo 0.37 API
    /// 4. Extracts metrics (memory, CPU, threads, FDs)
    /// 5. Normalizes CPU time by number of cores
    /// 6. Updates Prometheus gauges/counters
    /// 7. Releases lock
    ///
    /// Total execution time: ~1-5ms on Linux, may be slower on other platforms.
    fn collect_stats(&self) {
        // Acquire lock, handling poison errors gracefully
        let mut system = match self.system.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                // Lock was poisoned by a panic, but we can recover
                warn!("System mutex was poisoned, recovering");
                poisoned.into_inner()
            }
        };
        
        // Refresh process data (sysinfo 0.37 API)
        // Note: refresh_all() is simpler and works across all versions
        system.refresh_all();

        if let Some(process) = system.process(self.pid) {
            // Memory metrics (sysinfo 0.37 returns bytes directly)
            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 (cumulative across all cores, like node_exporter)
            // 
            // sysinfo 0.37 cpu_usage() returns instantaneous CPU% (total across all cores)
            // - Can exceed 100% on multi-core (e.g., 600% = using 6 cores)
            // - We need to integrate over time to get cumulative CPU seconds
            //
            // Simple approach: Integrate cpu_usage() over scrape intervals
            let cpu_percent = process.cpu_usage() as f64;  // Total CPU% across all cores
            
            // Convert percentage to cores in use
            // Example: 600% = 6.0 cores
            let cores_in_use = cpu_percent / 100.0;
            
            // Assume Prometheus scrapes every 15-30 seconds
            // We refresh process stats on each scrape, so use a conservative estimate
            // TODO: Track actual elapsed time for better accuracy
            let estimated_interval_seconds = 5.0;
            
            // Calculate CPU seconds consumed in this interval
            // Example: 6 cores × 5 seconds = 30 CPU-seconds
            let cpu_seconds_delta = cores_in_use * estimated_interval_seconds;
            
            // Only increment if we have meaningful CPU usage
            if cpu_seconds_delta > 0.0 {
                self.cpu_seconds_total.inc_by(cpu_seconds_delta);
            }

            // 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::*;

    #[test]
    fn test_process_collector_new() {
        let collector = ProcessCollector::new();
        assert!(collector.start_time_seconds.get() > 0.0);
    }

    #[test]
    fn test_process_collector_registers_without_error() {
        let collector = ProcessCollector::new();
        let registry = Registry::new();
        assert!(collector.register_metrics(&registry).is_ok());
    }

    #[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);
        
        // Should have at least 1 thread
        assert!(collector.threads.get() >= 1);
        
        // FDs should be > 0 (we have stdin/stdout/stderr at minimum)
        #[cfg(target_os = "linux")]
        assert!(collector.open_fds.get() >= 3);
    }
}