• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

payjoin / rust-payjoin / 21486039766

29 Jan 2026 04:23PM UTC coverage: 83.673% (+0.5%) from 83.203%
21486039766

Pull #1296

github

web-flow
Merge 741fb9cbc into 1b6cc38b5
Pull Request #1296: Add standalone metrics service to Payjoin-service

130 of 144 new or added lines in 7 files covered. (90.28%)

18 existing lines in 3 files now uncovered.

10224 of 12219 relevant lines covered (83.67%)

431.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.14
/payjoin-service/src/metrics.rs
1
use std::sync::Arc;
2

3
use prometheus::{Encoder, IntCounter, IntCounterVec, IntGauge, Opts, Registry, TextEncoder};
4

5
#[derive(Clone)]
6
pub struct MetricsService {
7
    registry: Arc<Registry>,
8

9
    pub http_requests_total: IntCounterVec,
10

11
    // Connection metrics
12
    pub total_connections: IntCounter,
13
    pub active_connections: IntGauge,
14
}
15

16
impl MetricsService {
17
    pub fn new() -> anyhow::Result<Self> {
24✔
18
        const TOTAL_CONNECTIONS: &str = "total_connections";
19
        const ACTIVE_CONNECTIONS: &str = "active_connections";
20
        const HTTP_REQUESTS: &str = "http_request_total";
21
        #[allow(dead_code)]
22
        const DB_ENTRIES: &str = "db_entries";
23
        let registry = Registry::new();
24✔
24
        // Total number of HTTP requests by endpoint type, method, and status code
25
        let http_requests_total = IntCounterVec::new(
24✔
26
            Opts::new(HTTP_REQUESTS, "Total number of HTTP requests"),
24✔
27
            &["endpoint", "method", "status_code"],
24✔
NEW
28
        )?;
×
29
        registry.register(Box::new(http_requests_total.clone()))?;
24✔
30

31
        // Total number of connections seen so far
32
        let total_connections = IntCounter::new(TOTAL_CONNECTIONS, "Total number of connections")?;
24✔
33
        registry.register(Box::new(total_connections.clone()))?;
24✔
34

35
        // Total number of active connections right now
36
        let active_connections = IntGauge::new(ACTIVE_CONNECTIONS, "Number of active connections")?;
24✔
37
        registry.register(Box::new(active_connections.clone()))?;
24✔
38

39
        Ok(Self {
24✔
40
            registry: Arc::new(registry),
24✔
41
            http_requests_total,
24✔
42
            total_connections,
24✔
43
            active_connections,
24✔
44
        })
24✔
45
    }
24✔
46

47
    pub fn record_http_request(&self, endpoint: &str, method: &str, status_code: u16) {
129✔
48
        self.http_requests_total
129✔
49
            .with_label_values(&[endpoint, method, &status_code.to_string()])
129✔
50
            .inc();
129✔
51
    }
129✔
52

53
    pub fn record_connection_open(&self) {
129✔
54
        self.total_connections.inc();
129✔
55
        self.active_connections.inc();
129✔
56
    }
129✔
57
    pub fn record_connection_close(&self) { self.active_connections.dec(); }
129✔
58

59
    pub(crate) fn encode_metrics(&self) -> Result<Vec<u8>, anyhow::Error> {
1✔
60
        let encode = TextEncoder::new();
1✔
61
        let all_metrics = self.registry.gather();
1✔
62
        let mut buffer = Vec::new();
1✔
63
        encode.encode(&all_metrics, &mut buffer)?;
1✔
64
        Ok(buffer)
1✔
65
    }
1✔
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc