aranet-service 0.2.0

Background collector and HTTP REST API for Aranet sensors
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
//! Prometheus push gateway integration.
//!
//! This module provides a client that pushes metrics to a Prometheus push gateway
//! at configurable intervals.
//!
//! # Example Configuration
//!
//! ```toml
//! [prometheus]
//! enabled = true
//! push_gateway = "http://localhost:9091"
//! push_interval = 60  # seconds
//! ```
//!
//! # Metrics Pushed
//!
//! The same metrics exposed by `/metrics` endpoint are pushed:
//! - `aranet_collector_running`
//! - `aranet_collector_uptime_seconds`
//! - `aranet_ws_messages_dropped_total`
//! - `aranet_device_poll_success_total`
//! - `aranet_device_poll_failure_total`
//! - `aranet_device_poll_duration_ms`
//! - `aranet_co2_ppm`
//! - `aranet_temperature_celsius`
//! - `aranet_humidity_percent`
//! - `aranet_pressure_hpa`
//! - `aranet_battery_percent`
//! - `aranet_reading_age_seconds`
//! - `aranet_radon_bqm3`
//! - `aranet_radiation_rate_usvh`
//! - `aranet_radiation_total_msv`

use std::sync::Arc;
use std::time::Duration;

use reqwest::Client;
use time::OffsetDateTime;
use tracing::{debug, info, warn};

use crate::config::PrometheusConfig;
use crate::state::AppState;

/// Prometheus push gateway client.
pub struct PrometheusPusher {
    state: Arc<AppState>,
}

impl PrometheusPusher {
    /// Create a new Prometheus pusher.
    pub fn new(state: Arc<AppState>) -> Self {
        Self { state }
    }

    /// Start the Prometheus push gateway client.
    ///
    /// This spawns a background task that periodically pushes metrics to the
    /// configured push gateway URL.
    ///
    /// Returns immediately; pushing happens in the background.
    pub async fn start(&self) {
        let config = self.state.config.read().await;
        let prometheus_config = config.prometheus.clone();
        drop(config);

        if !prometheus_config.enabled {
            debug!("Prometheus metrics endpoint is enabled but push gateway is not configured");
            return;
        }

        let push_gateway = match &prometheus_config.push_gateway {
            Some(url) if !url.is_empty() => url.clone(),
            _ => {
                debug!("Prometheus push gateway URL not configured");
                return;
            }
        };

        info!(
            "Starting Prometheus pusher to {} (interval: {}s)",
            push_gateway, prometheus_config.push_interval
        );

        let state = Arc::clone(&self.state);
        let shutdown_rx = self.state.subscribe_shutdown();

        tokio::spawn(async move {
            run_prometheus_pusher(state, prometheus_config, push_gateway, shutdown_rx).await;
        });
    }
}

/// Run the Prometheus push loop.
async fn run_prometheus_pusher(
    state: Arc<AppState>,
    config: PrometheusConfig,
    push_gateway: String,
    mut shutdown_rx: tokio::sync::watch::Receiver<bool>,
) {
    let client = match Client::builder().timeout(Duration::from_secs(30)).build() {
        Ok(c) => c,
        Err(e) => {
            tracing::error!("Failed to create HTTP client for Prometheus pusher: {e}");
            return;
        }
    };

    let push_interval = Duration::from_secs(config.push_interval);
    let mut interval = tokio::time::interval(push_interval);

    // Job name for the push gateway
    let job_name = "aranet-service";

    loop {
        tokio::select! {
            _ = interval.tick() => {
                let metrics = generate_metrics(&state).await;
                if let Err(e) = push_metrics(&client, &push_gateway, job_name, &metrics).await {
                    warn!("Failed to push metrics to Prometheus: {}", e);
                } else {
                    debug!("Pushed metrics to Prometheus push gateway");
                }
            }
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("Prometheus pusher received stop signal");
                    break;
                }
            }
        }
    }

    info!("Prometheus pusher stopped");
}

/// Generate metrics in Prometheus text format.
async fn generate_metrics(state: &AppState) -> String {
    let mut output = String::with_capacity(4096);

    // Collector status metrics
    let running = state.collector.is_running();
    let uptime = state.collector.started_at().map(|s| {
        let now = OffsetDateTime::now_utc();
        (now - s).whole_seconds().max(0)
    });

    output.push_str("# HELP aranet_collector_running Whether the collector is running\n");
    output.push_str("# TYPE aranet_collector_running gauge\n");
    output.push_str(&format!(
        "aranet_collector_running {}\n",
        if running { 1 } else { 0 }
    ));

    if let Some(uptime_secs) = uptime {
        output.push_str("# HELP aranet_collector_uptime_seconds Collector uptime\n");
        output.push_str("# TYPE aranet_collector_uptime_seconds gauge\n");
        output.push_str(&format!(
            "aranet_collector_uptime_seconds {}\n",
            uptime_secs
        ));
    }

    // WebSocket broadcast lag metric
    let dropped = state
        .ws_messages_dropped
        .load(std::sync::atomic::Ordering::Relaxed);
    output.push_str(
        "# HELP aranet_ws_messages_dropped_total Broadcast messages dropped due to slow WebSocket subscribers\n",
    );
    output.push_str("# TYPE aranet_ws_messages_dropped_total counter\n");
    output.push_str(&format!("aranet_ws_messages_dropped_total {}\n", dropped));

    // Device collection stats
    let device_stats = state.collector.device_stats.read().await;
    if !device_stats.is_empty() {
        output.push_str("# HELP aranet_device_poll_success_total Successful polls\n");
        output.push_str("# TYPE aranet_device_poll_success_total counter\n");
        for stat in device_stats.iter() {
            let alias = stat.alias.as_deref().unwrap_or(&stat.device_id);
            output.push_str(&format!(
                "aranet_device_poll_success_total{{device=\"{}\",address=\"{}\"}} {}\n",
                escape_label_value(alias),
                escape_label_value(&stat.device_id),
                stat.success_count
            ));
        }

        output.push_str("# HELP aranet_device_poll_failure_total Failed polls\n");
        output.push_str("# TYPE aranet_device_poll_failure_total counter\n");
        for stat in device_stats.iter() {
            let alias = stat.alias.as_deref().unwrap_or(&stat.device_id);
            output.push_str(&format!(
                "aranet_device_poll_failure_total{{device=\"{}\",address=\"{}\"}} {}\n",
                escape_label_value(alias),
                escape_label_value(&stat.device_id),
                stat.failure_count
            ));
        }

        output.push_str(
            "# HELP aranet_device_poll_duration_ms Duration of the last poll in milliseconds\n",
        );
        output.push_str("# TYPE aranet_device_poll_duration_ms gauge\n");
        for stat in device_stats.iter() {
            if let Some(duration_ms) = stat.last_poll_duration_ms {
                let alias = stat.alias.as_deref().unwrap_or(&stat.device_id);
                output.push_str(&format!(
                    "aranet_device_poll_duration_ms{{device=\"{}\",address=\"{}\"}} {}\n",
                    escape_label_value(alias),
                    escape_label_value(&stat.device_id),
                    duration_ms
                ));
            }
        }
    }
    drop(device_stats);

    // Get latest readings for all devices in a single pass
    // This avoids O(N²) behavior from calling get_latest_reading for each metric type
    let device_readings = state
        .with_store_read(|store| store.list_latest_readings())
        .await
        .unwrap_or_default();

    if !device_readings.is_empty() {
        // Build per-device metrics, filtering by device capabilities
        let mut co2_lines = Vec::new();
        let mut temp_lines = Vec::new();
        let mut humidity_lines = Vec::new();
        let mut pressure_lines = Vec::new();
        let mut battery_lines = Vec::new();
        let mut radon_lines = Vec::new();
        let mut reading_age_lines = Vec::new();
        let mut radiation_rate_lines = Vec::new();
        let mut radiation_total_lines = Vec::new();

        let now = OffsetDateTime::now_utc();
        for (device, reading) in &device_readings {
            let device_name = device.name.as_deref().unwrap_or(&device.id);
            let labels = format!(
                "device=\"{}\",address=\"{}\"",
                escape_label_value(device_name),
                escape_label_value(&device.id)
            );

            let device_type = resolve_device_type(device);

            if device_type.is_none_or(|dt| dt.has_co2()) && reading.co2 > 0 {
                co2_lines.push(format!("aranet_co2_ppm{{{}}} {}\n", labels, reading.co2));
            }
            if device_type.is_none_or(|dt| dt.has_temperature()) {
                temp_lines.push(format!(
                    "aranet_temperature_celsius{{{}}} {:.2}\n",
                    labels, reading.temperature
                ));
            }
            if device_type.is_none_or(|dt| dt.has_humidity()) {
                humidity_lines.push(format!(
                    "aranet_humidity_percent{{{}}} {}\n",
                    labels, reading.humidity
                ));
            }
            if device_type.is_none_or(|dt| dt.has_pressure()) {
                pressure_lines.push(format!(
                    "aranet_pressure_hpa{{{}}} {:.2}\n",
                    labels, reading.pressure
                ));
            }
            battery_lines.push(format!(
                "aranet_battery_percent{{{}}} {}\n",
                labels, reading.battery
            ));
            let age_seconds = (now - reading.captured_at).whole_seconds();
            if age_seconds > 0 {
                reading_age_lines.push(format!(
                    "aranet_reading_age_seconds{{{}}} {}\n",
                    labels, age_seconds
                ));
            }
            if let Some(radon) = reading.radon {
                radon_lines.push(format!("aranet_radon_bqm3{{{}}} {}\n", labels, radon));
            }
            if let Some(rate) = reading.radiation_rate {
                radiation_rate_lines.push(format!(
                    "aranet_radiation_rate_usvh{{{}}} {:.4}\n",
                    labels, rate
                ));
            }
            if let Some(total) = reading.radiation_total {
                radiation_total_lines.push(format!(
                    "aranet_radiation_total_msv{{{}}} {:.6}\n",
                    labels, total
                ));
            }
        }

        if !co2_lines.is_empty() {
            output.push_str("# HELP aranet_co2_ppm CO2 concentration in ppm\n");
            output.push_str("# TYPE aranet_co2_ppm gauge\n");
            for line in &co2_lines {
                output.push_str(line);
            }
        }
        if !temp_lines.is_empty() {
            output.push_str("# HELP aranet_temperature_celsius Temperature\n");
            output.push_str("# TYPE aranet_temperature_celsius gauge\n");
            for line in &temp_lines {
                output.push_str(line);
            }
        }
        if !humidity_lines.is_empty() {
            output.push_str("# HELP aranet_humidity_percent Relative humidity\n");
            output.push_str("# TYPE aranet_humidity_percent gauge\n");
            for line in &humidity_lines {
                output.push_str(line);
            }
        }
        if !pressure_lines.is_empty() {
            output.push_str("# HELP aranet_pressure_hpa Atmospheric pressure\n");
            output.push_str("# TYPE aranet_pressure_hpa gauge\n");
            for line in &pressure_lines {
                output.push_str(line);
            }
        }
        if !battery_lines.is_empty() {
            output.push_str("# HELP aranet_battery_percent Battery level\n");
            output.push_str("# TYPE aranet_battery_percent gauge\n");
            for line in &battery_lines {
                output.push_str(line);
            }
        }
        if !reading_age_lines.is_empty() {
            output.push_str(
                "# HELP aranet_reading_age_seconds Time since the last measurement in seconds\n",
            );
            output.push_str("# TYPE aranet_reading_age_seconds gauge\n");
            for line in &reading_age_lines {
                output.push_str(line);
            }
        }
        if !radon_lines.is_empty() {
            output.push_str("# HELP aranet_radon_bqm3 Radon concentration in Bq/m³\n");
            output.push_str("# TYPE aranet_radon_bqm3 gauge\n");
            for line in &radon_lines {
                output.push_str(line);
            }
        }
        if !radiation_rate_lines.is_empty() {
            output.push_str("# HELP aranet_radiation_rate_usvh Radiation rate in µSv/h\n");
            output.push_str("# TYPE aranet_radiation_rate_usvh gauge\n");
            for line in &radiation_rate_lines {
                output.push_str(line);
            }
        }
        if !radiation_total_lines.is_empty() {
            output.push_str("# HELP aranet_radiation_total_msv Total radiation dose in mSv\n");
            output.push_str("# TYPE aranet_radiation_total_msv gauge\n");
            for line in &radiation_total_lines {
                output.push_str(line);
            }
        }
    }

    output
}

/// Push metrics to the Prometheus push gateway.
async fn push_metrics(
    client: &Client,
    push_gateway: &str,
    job_name: &str,
    metrics: &str,
) -> Result<(), PushError> {
    // Prometheus push gateway URL format: {gateway}/metrics/job/{job_name}
    let url = format!(
        "{}/metrics/job/{}",
        push_gateway.trim_end_matches('/'),
        job_name
    );

    let response = client
        .post(&url)
        .header("Content-Type", "text/plain; version=0.0.4")
        .body(metrics.to_string())
        .send()
        .await
        .map_err(PushError::Request)?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return Err(PushError::Response {
            status: status.as_u16(),
            body,
        });
    }

    Ok(())
}

/// Resolve a device's type from stored metadata, falling back to name-based detection.
fn resolve_device_type(device: &aranet_store::StoredDevice) -> Option<aranet_types::DeviceType> {
    device.device_type.or_else(|| {
        device
            .name
            .as_deref()
            .and_then(aranet_types::DeviceType::from_name)
            .or_else(|| aranet_types::DeviceType::from_name(&device.id))
    })
}

/// Escape special characters in Prometheus label values.
fn escape_label_value(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

/// Errors that can occur when pushing metrics.
#[derive(Debug, thiserror::Error)]
pub enum PushError {
    #[error("Request failed: {0}")]
    Request(#[from] reqwest::Error),
    #[error("Push gateway returned error {status}: {body}")]
    Response { status: u16, body: String },
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use aranet_store::Store;
    use aranet_types::{CurrentReading, Status};

    #[test]
    fn test_escape_label_value() {
        assert_eq!(escape_label_value("hello"), "hello");
        assert_eq!(escape_label_value("hello\"world"), "hello\\\"world");
        assert_eq!(escape_label_value("hello\\world"), "hello\\\\world");
        assert_eq!(escape_label_value("hello\nworld"), "hello\\nworld");
    }

    /// Verify that metrics are filtered by device capabilities:
    /// - An Aranet2 device should NOT emit `aranet_co2_ppm` or `aranet_pressure_hpa`.
    /// - An Aranet4 device should emit all sensor metrics.
    #[tokio::test]
    async fn test_metrics_filtered_by_device_capability() {
        let store = Store::open_in_memory().unwrap();
        let config = Config::default();
        let state = AppState::new(store, config);

        // Insert an Aranet4 device with a reading.
        {
            let store = state.store.lock().await;
            store
                .upsert_device("Aranet4 AAAAA", Some("Aranet4 AAAAA"))
                .unwrap();
            let reading = CurrentReading {
                co2: 800,
                temperature: 22.5,
                pressure: 1013.2,
                humidity: 45,
                battery: 85,
                status: Status::Green,
                interval: 300,
                age: 60,
                ..Default::default()
            };
            store.insert_reading("Aranet4 AAAAA", &reading).unwrap();
        }

        // Insert an Aranet2 device with a reading (no CO2 or pressure).
        {
            let store = state.store.lock().await;
            store
                .upsert_device("Aranet2 BBBBB", Some("Aranet2 BBBBB"))
                .unwrap();
            let reading = CurrentReading {
                co2: 0,
                temperature: 21.0,
                pressure: 0.0,
                humidity: 55,
                battery: 90,
                status: Status::Green,
                interval: 300,
                age: 60,
                ..Default::default()
            };
            store.insert_reading("Aranet2 BBBBB", &reading).unwrap();
        }

        let metrics = generate_metrics(&state).await;

        // Aranet4 should have CO2 and pressure metrics.
        assert!(
            metrics.contains("aranet_co2_ppm{device=\"Aranet4 AAAAA\""),
            "Aranet4 should emit CO2 metric"
        );
        assert!(
            metrics.contains("aranet_pressure_hpa{device=\"Aranet4 AAAAA\""),
            "Aranet4 should emit pressure metric"
        );

        // Aranet2 should NOT have CO2 or pressure metrics.
        assert!(
            !metrics.contains("aranet_co2_ppm{device=\"Aranet2 BBBBB\""),
            "Aranet2 should not emit CO2 metric"
        );
        assert!(
            !metrics.contains("aranet_pressure_hpa{device=\"Aranet2 BBBBB\""),
            "Aranet2 should not emit pressure metric"
        );

        // Aranet2 should still have temperature and humidity.
        assert!(
            metrics.contains("aranet_temperature_celsius{device=\"Aranet2 BBBBB\""),
            "Aranet2 should emit temperature metric"
        );
        assert!(
            metrics.contains("aranet_humidity_percent{device=\"Aranet2 BBBBB\""),
            "Aranet2 should emit humidity metric"
        );

        // Both should have battery.
        assert!(
            metrics.contains("aranet_battery_percent{device=\"Aranet4 AAAAA\""),
            "Aranet4 should emit battery metric"
        );
        assert!(
            metrics.contains("aranet_battery_percent{device=\"Aranet2 BBBBB\""),
            "Aranet2 should emit battery metric"
        );
    }

    /// Verify that poll duration metric is emitted when stats have a value.
    #[tokio::test]
    async fn test_poll_duration_metric_emitted() {
        let store = Store::open_in_memory().unwrap();
        let config = Config::default();
        let state = AppState::new(store, config);

        // Add device stats with a poll duration.
        {
            let mut stats = state.collector.device_stats.write().await;
            stats.push(crate::state::DeviceCollectionStats {
                device_id: "test-device".to_string(),
                alias: Some("Test".to_string()),
                poll_interval: 60,
                last_poll_at: None,
                last_error_at: None,
                last_error: None,
                last_poll_duration_ms: Some(1234),
                success_count: 1,
                failure_count: 0,
                polling: false,
            });
        }

        let metrics = generate_metrics(&state).await;
        assert!(
            metrics.contains("aranet_device_poll_duration_ms{device=\"Test\""),
            "Should emit poll duration metric"
        );
        assert!(
            metrics.contains("1234"),
            "Poll duration should contain the value"
        );
    }
}