goosefs-sdk 0.1.7

Goosefs Rust gRPC Client - Direct gRPC client for Goosefs Master/Worker
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
//! Prometheus Pushgateway reporter.
//!
//! Periodically collects all metrics from the global `Registry`
//! and pushes them to a Prometheus Pushgateway endpoint via HTTP POST using the
//! [Prometheus text exposition format](https://prometheus.io/docs/instrumenting/exposition_formats/).
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────┐       HTTP POST        ┌──────────────┐
//! │  PushgatewayTask    │ ───────────────────────►│  Pushgateway │
//! │  (tokio background) │   /metrics/job/{job}    │  :9091       │
//! └─────────────────────┘                         └──────────────┘
//!//!         │ reads
//! ┌───────┴─────────────┐
//! │  Global Registry    │
//! │  (counters, gauges) │
//! └─────────────────────┘
//! ```
//!
//! ## Usage
//!
//! ```rust,no_run
//! use goosefs_sdk::metrics::pushgateway::{PushgatewayConfig, PushgatewayTask};
//!
//! #[tokio::main]
//! async fn main() {
//!     let config = PushgatewayConfig::new("http://127.0.0.1:9091", "goosefs_client")
//!         .with_instance("my-host")
//!         .with_push_interval(std::time::Duration::from_secs(10));
//!     let task = PushgatewayTask::spawn(config);
//!
//!     // ... application logic ...
//!
//!     task.shutdown().await;
//! }
//! ```

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, info, warn};

use crate::metrics::registry::get_registry;

// ── PushgatewayConfig ─────────────────────────────────────────────────────────

/// Configuration for the Pushgateway reporter.
#[derive(Debug, Clone)]
pub struct PushgatewayConfig {
    /// Pushgateway base URL (e.g. `"http://127.0.0.1:9091"`).
    pub endpoint: String,

    /// Job label for the pushed metrics (maps to `/metrics/job/{job}`).
    ///
    /// Typically `"goosefs_client"` or your service name.
    pub job: String,

    /// Optional instance label (adds `/instance/{instance}` to the push URL).
    ///
    /// When `None`, the Pushgateway auto-assigns based on the client IP.
    pub instance: Option<String>,

    /// Interval between metric pushes (default: 10 s).
    pub push_interval: Duration,

    /// Additional grouping labels appended to the push URL.
    ///
    /// Each `(key, value)` pair becomes `/{key}/{value}` in the URL path.
    /// Example: `[("namespace", "production")]` → `.../namespace/production`
    pub extra_labels: Vec<(String, String)>,
}

impl Default for PushgatewayConfig {
    fn default() -> Self {
        Self {
            endpoint: "http://127.0.0.1:9091".to_string(),
            job: "goosefs_client".to_string(),
            instance: None,
            push_interval: Duration::from_secs(10),
            extra_labels: Vec::new(),
        }
    }
}

impl PushgatewayConfig {
    /// Create a new config with the given endpoint and job name.
    pub fn new(endpoint: impl Into<String>, job: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            job: job.into(),
            ..Default::default()
        }
    }

    /// Set the instance label.
    pub fn with_instance(mut self, instance: impl Into<String>) -> Self {
        self.instance = Some(instance.into());
        self
    }

    /// Set the push interval.
    pub fn with_push_interval(mut self, interval: Duration) -> Self {
        self.push_interval = interval;
        self
    }

    /// Add an extra grouping label.
    pub fn with_label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra_labels.push((key.into(), value.into()));
        self
    }

    /// Build the full push URL for the Pushgateway.
    ///
    /// Format: `{endpoint}/metrics/job/{job}[/instance/{instance}][/{key}/{value}...]`
    pub fn push_url(&self) -> String {
        let mut url = format!(
            "{}/metrics/job/{}",
            self.endpoint.trim_end_matches('/'),
            url_encode(&self.job)
        );
        if let Some(ref inst) = self.instance {
            url.push_str(&format!("/instance/{}", url_encode(inst)));
        }
        for (k, v) in &self.extra_labels {
            url.push_str(&format!("/{}/{}", url_encode(k), url_encode(v)));
        }
        url
    }
}

// ── PushgatewayTask ───────────────────────────────────────────────────────────

/// Background task that periodically pushes metrics to a Prometheus Pushgateway.
///
/// ## Lifecycle
///
/// ```text
/// PushgatewayTask::spawn(config)
///   └── tokio::spawn  ← background loop (ticker + flush_rx select)
///
/// PushgatewayTask::shutdown().await
///   ├── closed.store(true)
///   ├── flush_tx.send(())   ← unblocks select immediately
///   └── JoinHandle::await   ← waits for final push
/// ```
pub struct PushgatewayTask {
    handle: tokio::sync::Mutex<Option<JoinHandle<()>>>,
    closed: Arc<AtomicBool>,
    flush_tx: mpsc::Sender<()>,
}

impl PushgatewayTask {
    /// Spawn the pushgateway reporter background task.
    ///
    /// The task will perform its first push after one full `push_interval`,
    /// then repeat at that interval.
    pub fn spawn(config: PushgatewayConfig) -> Self {
        let (flush_tx, mut flush_rx) = mpsc::channel::<()>(1);
        let flush_tx_for_task = flush_tx.clone();
        let closed = Arc::new(AtomicBool::new(false));
        let closed_for_task = closed.clone();

        let push_url = config.push_url();
        let interval = config.push_interval;

        info!(
            push_url = %push_url,
            interval_secs = interval.as_secs(),
            "pushgateway reporter starting"
        );

        let handle = tokio::spawn(async move {
            let closed = closed_for_task;
            let client = reqwest::Client::new();

            let mut ticker = tokio::time::interval(interval);
            ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
            // Skip first immediate tick
            ticker.tick().await;

            loop {
                tokio::select! {
                    biased;
                    _ = flush_rx.recv() => {
                        debug!("pushgateway: flush signal received");
                    }
                    _ = ticker.tick() => {}
                }

                // Collect and push metrics
                let body = collect_metrics_text();
                if !body.is_empty() {
                    match client
                        .post(&push_url)
                        .header("Content-Type", "text/plain; version=0.0.4")
                        .body(body)
                        .send()
                        .await
                    {
                        Ok(resp) => {
                            if resp.status().is_success() {
                                debug!(status = %resp.status(), "pushgateway: metrics pushed successfully");
                            } else {
                                let status = resp.status();
                                let text = resp.text().await.unwrap_or_default();
                                warn!(
                                    status = %status,
                                    body = %text,
                                    "pushgateway: push failed with non-2xx status"
                                );
                            }
                        }
                        Err(e) => {
                            warn!(error = %e, "pushgateway: HTTP request failed");
                        }
                    }
                } else {
                    debug!("pushgateway: no metrics to push, skipping");
                }

                if closed.load(Ordering::SeqCst) {
                    break;
                }
            }

            drop(flush_tx_for_task);
            debug!("pushgateway reporter task exited");
        });

        Self {
            handle: tokio::sync::Mutex::new(Some(handle)),
            closed,
            flush_tx,
        }
    }

    /// Gracefully shut down the pushgateway task.
    ///
    /// Performs one final push before exiting.
    pub async fn shutdown(&self) {
        self.closed.store(true, Ordering::SeqCst);
        let _ = self.flush_tx.send(()).await;

        if let Some(h) = self.handle.lock().await.take() {
            let _ = tokio::time::timeout(Duration::from_secs(5), h).await;
        }
    }
}

impl Drop for PushgatewayTask {
    fn drop(&mut self) {
        self.closed.store(true, Ordering::SeqCst);
        let _ = self.flush_tx.try_send(());
    }
}

// ── Metrics collection ────────────────────────────────────────────────────────

/// Collect all metrics from the global registry and format them in the
/// Prometheus text exposition format.
///
/// Counter metrics are reported as `counter` type with their **current cumulative value**
/// (Prometheus expects monotonically increasing counters — the Pushgateway/Prometheus
/// handles rate/delta calculation).
///
/// Gauge metrics are reported as `gauge` type with their current value.
fn collect_metrics_text() -> String {
    let registry = get_registry();
    let mut lines = Vec::new();

    // Counters (reported as _total per Prometheus naming convention)
    for entry in registry.counters.iter() {
        let raw_name = entry.key();
        let value = entry.value().get();
        let prom_name = sanitize_metric_name(raw_name);

        // Add HELP and TYPE annotations
        lines.push(format!(
            "# HELP {} GooseFS client counter: {}",
            prom_name, raw_name
        ));
        lines.push(format!("# TYPE {} counter", prom_name));
        lines.push(format!("{} {}", prom_name, value));
    }

    // Gauges
    for entry in registry.gauges.iter() {
        let raw_name = entry.key();
        let value = entry.value().get();
        let prom_name = sanitize_metric_name(raw_name);

        lines.push(format!(
            "# HELP {} GooseFS client gauge: {}",
            prom_name, raw_name
        ));
        lines.push(format!("# TYPE {} gauge", prom_name));
        lines.push(format!("{} {}", prom_name, value));
    }

    if lines.is_empty() {
        return String::new();
    }

    // Prometheus text format requires a trailing newline
    lines.push(String::new());
    lines.join("\n")
}

/// Convert a GooseFS metric name to a valid Prometheus metric name.
///
/// Prometheus metric names must match `[a-zA-Z_:][a-zA-Z0-9_:]*`.
/// We convert `.` to `_`, add a `goosefs_` prefix, and convert to lowercase.
///
/// Examples:
/// - `"Client.BytesReadLocal"` → `"goosefs_client_bytes_read_local"`
/// - `"Client.BytesWrittenUfs"` → `"goosefs_client_bytes_written_ufs"`
fn sanitize_metric_name(name: &str) -> String {
    let mut result = String::with_capacity(name.len() + 8);
    result.push_str("goosefs_");

    // Track the previous character (the one already pushed onto `result`)
    // so we can do an O(1) "is preceded by '.' / uppercase" check, instead
    // of `name.chars().nth(i - 1)` which scans from the start of the input
    // every iteration → O(n²).
    let mut prev_was_upper = false;
    let mut prev_char: Option<char> = None;
    for ch in name.chars() {
        if ch == '.' {
            result.push('_');
            prev_was_upper = false;
            prev_char = Some('.');
        } else if ch.is_uppercase() {
            // Insert underscore before uppercase letter if not at start of a word
            // and not following another uppercase or a '.'.
            if prev_char.is_some() && !prev_was_upper && prev_char != Some('.') {
                result.push('_');
            }
            let lowered = ch.to_lowercase().next().unwrap_or(ch);
            result.push(lowered);
            prev_was_upper = true;
            prev_char = Some(ch);
        } else if ch.is_alphanumeric() || ch == '_' || ch == ':' {
            result.push(ch);
            prev_was_upper = false;
            prev_char = Some(ch);
        } else {
            result.push('_');
            prev_was_upper = false;
            prev_char = Some(ch);
        }
    }

    result
}

/// Simple percent-encoding for URL path segments.
///
/// Encodes characters that are not valid in URL path segments.
fn url_encode(s: &str) -> String {
    let mut encoded = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            'A'..='Z' | 'a'..='z' | '0'..='9' | '-' | '_' | '.' | '~' => encoded.push(ch),
            _ => {
                for b in ch.to_string().as_bytes() {
                    encoded.push_str(&format!("%{:02X}", b));
                }
            }
        }
    }
    encoded
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn sanitize_metric_name_basic() {
        assert_eq!(
            sanitize_metric_name("Client.BytesReadLocal"),
            "goosefs_client_bytes_read_local"
        );
        assert_eq!(
            sanitize_metric_name("Client.BytesWrittenUfs"),
            "goosefs_client_bytes_written_ufs"
        );
        assert_eq!(
            sanitize_metric_name("Client.BytesWrittenLocal"),
            "goosefs_client_bytes_written_local"
        );
    }

    #[test]
    fn sanitize_metric_name_custom() {
        assert_eq!(
            sanitize_metric_name("Client.DemoOpsCount"),
            "goosefs_client_demo_ops_count"
        );
    }

    #[test]
    fn push_url_basic() {
        let config = PushgatewayConfig::new("http://localhost:9091", "my_job");
        assert_eq!(
            config.push_url(),
            "http://localhost:9091/metrics/job/my_job"
        );
    }

    #[test]
    fn push_url_with_instance() {
        let config =
            PushgatewayConfig::new("http://localhost:9091", "my_job").with_instance("host1");
        assert_eq!(
            config.push_url(),
            "http://localhost:9091/metrics/job/my_job/instance/host1"
        );
    }

    #[test]
    fn push_url_with_extra_labels() {
        let config = PushgatewayConfig::new("http://localhost:9091", "my_job")
            .with_instance("host1")
            .with_label("namespace", "prod")
            .with_label("cluster", "us-east");
        assert_eq!(
            config.push_url(),
            "http://localhost:9091/metrics/job/my_job/instance/host1/namespace/prod/cluster/us-east"
        );
    }

    #[test]
    fn push_url_trailing_slash_stripped() {
        let config = PushgatewayConfig::new("http://localhost:9091/", "my_job");
        assert_eq!(
            config.push_url(),
            "http://localhost:9091/metrics/job/my_job"
        );
    }

    #[test]
    fn collect_metrics_text_includes_counters() {
        // Register a counter and ensure it shows up in the text output
        let c = crate::metrics::counter("Client.PushgatewayTestCounter");
        c.inc(42);

        let text = collect_metrics_text();
        assert!(
            text.contains("goosefs_client_pushgateway_test_counter"),
            "expected metric name in output, got:\n{}",
            text
        );
        assert!(text.contains("42"), "expected value 42 in output");
        assert!(text.contains("# TYPE"), "expected TYPE annotation");
    }

    #[test]
    fn collect_metrics_text_includes_gauges() {
        let g = crate::metrics::gauge("Client.PushgatewayTestGauge");
        g.set(99);

        let text = collect_metrics_text();
        assert!(
            text.contains("goosefs_client_pushgateway_test_gauge"),
            "expected gauge metric name in output, got:\n{}",
            text
        );
        assert!(text.contains("99"), "expected gauge value 99 in output");
        assert!(text.contains("# TYPE"), "expected TYPE annotation");
    }

    #[test]
    fn url_encode_basic() {
        assert_eq!(url_encode("hello"), "hello");
        assert_eq!(url_encode("hello world"), "hello%20world");
        assert_eq!(url_encode("a/b"), "a%2Fb");
    }
}