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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Centralized Health Monitoring
//!
//! Provides centralized health monitoring and aggregation for all nodes in a Mecha10 system.
//!
//! # Features
//!
//! - Automatic health reporting from nodes
//! - Centralized health aggregation in Redis
//! - System-wide health status
//! - Health history storage (24 hours, using Redis sorted sets)
//! - Health history queries by time range
//! - Alerting on health degradation
//! - Dashboard/CLI integration
//!
//! # Architecture
//!
//! ```text
//! ┌─────────┐ Health ┌───────────────┐
//! │ Node A │────Status──────▶│ │
//! └─────────┘ │ │
//! │ Health │ ┌──────────┐
//! ┌─────────┐ Health │ Monitor │────▶│Dashboard │
//! │ Node B │────Status──────▶│ (Redis) │ └──────────┘
//! └─────────┘ │ │
//! │ │ ┌──────────┐
//! ┌─────────┐ Health │ │────▶│Alerting │
//! │ Node C │────Status──────▶│ │ └──────────┘
//! └─────────┘ └───────────────┘
//! ```
//!
//! # Example
//!
//! ```rust
//! use mecha10_core::prelude::*;
//! use mecha10_core::health::{HealthMonitor, HealthReporter};
//! use mecha10_core::messages::HealthStatus;
//!
//! # async fn example() -> Result<()> {
//! // In your node
//! let ctx = Context::new("my-node").await?;
//! let reporter = HealthReporter::new(&ctx).await?;
//!
//! // Report health periodically
//! let mut interval = interval_at_hz(1.0); // 1 Hz
//! loop {
//! interval.tick().await;
//!
//! let status = HealthStatus::healthy()
//! .with_diagnostic("cpu_usage", "25%")
//! .with_diagnostic("memory_mb", "128");
//!
//! reporter.report(status).await?;
//! }
//!
//! // Monitor system health
//! let monitor = HealthMonitor::connect("redis://localhost:6379").await?;
//!
//! // Get overall system health
//! let system_health = monitor.system_health().await?;
//! println!("System: {:?}", system_health.level);
//!
//! // Get health by node
//! let node_health = monitor.node_health("my-node").await?;
//!
//! // List unhealthy nodes
//! let unhealthy = monitor.unhealthy_nodes().await?;
//! # Ok(())
//! # }
//! ```
use crate::context::Context;
use crate::error::{Mecha10Error, Result};
use crate::messages::{HealthLevel, HealthStatus, Message};
use crate::node::{HealthPriority, NodeTag, NodeType};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::RwLock;
#[derive(Debug, Clone, Default)]
pub struct InvalidInputTracker {
count: Arc<AtomicU64>,
last_reason: Arc<Mutex<Option<String>>>,
}
impl InvalidInputTracker {
/// Create a fresh tracker with nothing recorded yet.
pub fn new() -> Self {
Self::default()
}
/// Record one instance of invalid/unrecognized runtime input. Call this
/// alongside - never instead of - a `warn!` log at the call site.
pub fn record(&self, reason: impl Into<String>) {
self.count.fetch_add(1, Ordering::Relaxed);
if let Ok(mut last) = self.last_reason.lock() {
*last = Some(reason.into());
}
}
/// Total number of invalid inputs recorded since this tracker was created.
pub fn total(&self) -> u64 {
self.count.load(Ordering::Relaxed)
}
/// The most recently recorded reason, if any. Most call sites should prefer
/// [`InvalidInputTracker::fold_into`], which builds on this; this is exposed
/// directly for nodes reporting health through a different `HealthStatus`
/// type (e.g. `NodeImpl::health_check`'s `crate::types::HealthStatus`,
/// distinct from `crate::messages::HealthStatus`) that can't take
/// `fold_into`'s output directly.
pub fn last_reason(&self) -> Option<String> {
self.last_reason.lock().ok().and_then(|guard| guard.clone())
}
/// Fold this tracker's state into a health status: if any invalid input has
/// been recorded, downgrade to [`HealthStatus::degraded`] naming the most
/// recent bad input and the running total; otherwise pass `healthy` through
/// unchanged.
pub fn fold_into(&self, healthy: HealthStatus) -> HealthStatus {
let total = self.total();
if total == 0 {
return healthy;
}
let reason = self.last_reason().unwrap_or_else(|| "unknown".to_string());
HealthStatus::degraded(format!(
"{total} invalid runtime input(s) received; most recent: {reason}"
))
}
}
#[cfg(test)]
mod invalid_input_tracker_tests {
use super::*;
#[test]
fn fresh_tracker_passes_healthy_status_through_unchanged() {
let tracker = InvalidInputTracker::new();
let folded = tracker.fold_into(HealthStatus::healthy());
assert_eq!(folded.level, HealthLevel::Ok);
}
#[test]
fn recording_degrades_and_names_the_most_recent_reason_and_total() {
let tracker = InvalidInputTracker::new();
tracker.record("unknown format 'foo'");
tracker.record("unknown format 'bar'");
let folded = tracker.fold_into(HealthStatus::healthy());
assert_eq!(folded.level, HealthLevel::Degraded);
assert!(
folded.message.contains('2'),
"message should mention the total: {}",
folded.message
);
assert!(
folded.message.contains("bar"),
"message should mention the most recent reason: {}",
folded.message
);
}
#[test]
fn total_reflects_number_of_records() {
let tracker = InvalidInputTracker::new();
assert_eq!(tracker.total(), 0);
tracker.record("x");
tracker.record("y");
assert_eq!(tracker.total(), 2);
}
}
pub struct HealthReporter {
node_id: String,
redis_client: redis::Client,
ctx: Arc<Context>,
priority: HealthPriority,
ttl_seconds: u64,
store_history: bool,
node_type: Option<NodeType>,
tags: Vec<NodeTag>,
}
impl HealthReporter {
/// Create a new health reporter with default settings (Normal priority)
pub async fn new(ctx: &Context) -> Result<Self> {
Self::with_config(ctx, HealthPriority::Normal, 60, true).await
}
/// Create a new health reporter with custom configuration
///
/// # Arguments
///
/// * `ctx` - Node context
/// * `priority` - Health priority level (affects timeout calculations)
/// * `ttl_seconds` - TTL for the Redis health key
/// * `store_history` - Whether to store health history in sorted sets
pub async fn with_config(
ctx: &Context,
priority: HealthPriority,
ttl_seconds: u64,
store_history: bool,
) -> Result<Self> {
Self::with_config_and_type_tags(ctx, priority, ttl_seconds, store_history, None, &[]).await
}
/// Create a new health reporter with custom configuration and an
/// explicit `node_type`/`tags` pair, included in every [`HealthReport`]
/// this reporter constructs.
///
/// # Arguments
///
/// * `ctx` - Node context
/// * `priority` - Health priority level (affects timeout calculations)
/// * `ttl_seconds` - TTL for the Redis health key
/// * `store_history` - Whether to store health history in sorted sets
/// * `node_type` - Built-in node type to report, if any (see [`crate::node::NodeImpl::NODE_TYPE`])
/// * `tags` - Capability tags to report (see [`crate::node::NodeImpl::NODE_TAGS`])
pub async fn with_config_and_type_tags(
ctx: &Context,
priority: HealthPriority,
ttl_seconds: u64,
store_history: bool,
node_type: Option<NodeType>,
tags: &[NodeTag],
) -> Result<Self> {
let redis_url = Self::get_redis_url()?;
let redis_client = redis::Client::open(redis_url.as_str())
.map_err(|e| Mecha10Error::Other(format!("Failed to connect to Redis for health reporting: {}", e)))?;
Ok(Self {
node_id: ctx.node_id().to_string(),
redis_client,
ctx: Arc::new(ctx.clone()),
priority,
ttl_seconds,
store_history,
node_type,
tags: tags.to_vec(),
})
}
/// Report health status
///
/// Stores the health status in Redis with a TTL. If the node stops reporting,
/// the health status will expire and the node will be marked as unavailable.
pub async fn report(&self, status: HealthStatus) -> Result<()> {
use redis::AsyncCommands;
let mut conn = self
.redis_client
.get_multiplexed_async_connection()
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to get Redis connection: {}", e)))?;
let report = HealthReport {
node_id: self.node_id.clone(),
status,
priority: self.priority,
reported_at: crate::prelude::now_micros(),
node_type: self.node_type,
tags: self.tags.clone(),
instance_id: self.ctx.instance().map(str::to_string),
};
let key = format!("mecha10:health:{}", self.node_id);
let value = serde_json::to_string(&report)
.map_err(|e| Mecha10Error::Other(format!("Failed to serialize health report: {}", e)))?;
conn.set_ex::<_, _, ()>(&key, value.clone(), self.ttl_seconds)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to store health status: {}", e)))?;
if self.store_history {
let history_key = format!("mecha10:health:history:{}", self.node_id);
let score = report.reported_at as f64;
conn.zadd::<_, _, _, ()>(&history_key, &value, score)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to store health history: {}", e)))?;
let cutoff_time = (report.reported_at - 86_400_000_000) as f64;
conn.zrembyscore::<_, _, _, ()>(&history_key, f64::MIN, cutoff_time)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to clean old health history: {}", e)))?;
}
self.ctx.publish_raw("/system/health", &report).await?;
Ok(())
}
/// Report healthy status
pub async fn report_healthy(&self) -> Result<()> {
self.report(HealthStatus::healthy()).await
}
/// Report degraded status
pub async fn report_degraded(&self, message: impl Into<String>) -> Result<()> {
self.report(HealthStatus::degraded(message)).await
}
/// Report unhealthy status
pub async fn report_unhealthy(&self, message: impl Into<String>) -> Result<()> {
self.report(HealthStatus::unhealthy(message)).await
}
fn get_redis_url() -> Result<String> {
let url = Context::get_redis_url()?;
tracing::debug!("HealthReporter using Redis URL: {}", url);
Ok(url)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
pub node_id: String,
pub status: HealthStatus,
#[serde(default)]
pub priority: HealthPriority,
pub reported_at: u64,
#[serde(default)]
pub node_type: Option<NodeType>,
#[serde(default)]
pub tags: Vec<NodeTag>,
/// Instance id, for a node running as one of several keyed instances of the same node
/// type under one target (e.g. two `camera` nodes - one hardware, one simulated - both
/// configured via `mecha10.json`'s `{"name": "@mecha10/camera", "instance_id": "real"}`
/// node-list entry shape). `None` for an ordinary single-instance node. This is already
/// encoded in `node_id` (e.g. `"camera/real"`, via [`crate::context::Context::with_instance`]),
/// but is surfaced as its own field so downstream consumers (e.g. the dashboard) don't need
/// to parse it back out of `node_id`. `#[serde(default)]` so reports from nodes that
/// haven't redeployed yet still deserialize.
#[serde(default)]
pub instance_id: Option<String>,
}
impl Message for HealthReport {}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemHealth {
pub level: HealthLevel,
pub total_nodes: usize,
pub healthy_nodes: usize,
pub degraded_nodes: usize,
pub unhealthy_nodes: usize,
pub unresponsive_nodes: usize,
pub timestamp: u64,
pub nodes: HashMap<String, NodeHealthInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeHealthInfo {
pub node_id: String,
pub status: HealthStatus,
#[serde(default)]
pub priority: HealthPriority,
pub reported_at: u64,
pub age_micros: u64,
pub responsive: bool,
#[serde(default)]
pub node_type: Option<NodeType>,
#[serde(default)]
pub tags: Vec<NodeTag>,
/// Instance id, if this node is one of several keyed instances of the same node type (see
/// [`HealthReport::instance_id`]).
#[serde(default)]
pub instance_id: Option<String>,
}
pub struct HealthMonitor {
redis_client: redis::Client,
cache: Arc<RwLock<Option<(SystemHealth, std::time::Instant)>>>,
cache_ttl: Duration,
}
impl HealthMonitor {
/// Connect to Redis for health monitoring
pub async fn connect(redis_url: &str) -> Result<Self> {
let redis_client = redis::Client::open(redis_url)
.map_err(|e| Mecha10Error::Other(format!("Failed to connect to Redis: {}", e)))?;
Ok(Self {
redis_client,
cache: Arc::new(RwLock::new(None)),
cache_ttl: Duration::from_secs(5),
})
}
/// Get system-wide health summary
pub async fn system_health(&self) -> Result<SystemHealth> {
{
let cache = self.cache.read().await;
if let Some((health, cached_at)) = cache.as_ref() {
if cached_at.elapsed() < self.cache_ttl {
return Ok(health.clone());
}
}
}
let health = self.fetch_system_health().await?;
{
let mut cache = self.cache.write().await;
*cache = Some((health.clone(), std::time::Instant::now()));
}
Ok(health)
}
async fn fetch_system_health(&self) -> Result<SystemHealth> {
use redis::AsyncCommands;
let mut conn = self
.redis_client
.get_multiplexed_async_connection()
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to get Redis connection: {}", e)))?;
let pattern = "mecha10:health:*";
let keys: Vec<String> = conn
.keys(pattern)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to list health keys: {}", e)))?;
let keys: Vec<_> = keys.into_iter().filter(|k| !k.contains(":history:")).collect();
let mut nodes = HashMap::new();
let mut healthy_count = 0;
let mut degraded_count = 0;
let mut unhealthy_count = 0;
let mut unresponsive_count = 0;
let now = crate::prelude::now_micros();
for key in keys {
let value: Option<String> = conn
.get(&key)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to get health value: {}", e)))?;
if let Some(value) = value {
if let Ok(report) = serde_json::from_str::<HealthReport>(&value) {
let age_micros = now.saturating_sub(report.reported_at);
let timeout_micros = report.priority.timeout_micros();
let responsive = age_micros < timeout_micros;
if !responsive {
unresponsive_count += 1;
} else {
match report.status.level {
HealthLevel::Ok => healthy_count += 1,
HealthLevel::Degraded => degraded_count += 1,
HealthLevel::Error => unhealthy_count += 1,
HealthLevel::Unknown => unresponsive_count += 1,
}
}
nodes.insert(
report.node_id.clone(),
NodeHealthInfo {
node_id: report.node_id,
status: report.status,
priority: report.priority,
reported_at: report.reported_at,
age_micros,
responsive,
node_type: report.node_type,
tags: report.tags,
instance_id: report.instance_id,
},
);
}
}
}
let level = if unhealthy_count > 0 || unresponsive_count > nodes.len() / 2 {
HealthLevel::Error
} else if degraded_count > 0 || unresponsive_count > 0 {
HealthLevel::Degraded
} else if healthy_count > 0 {
HealthLevel::Ok
} else {
HealthLevel::Unknown
};
Ok(SystemHealth {
level,
total_nodes: nodes.len(),
healthy_nodes: healthy_count,
degraded_nodes: degraded_count,
unhealthy_nodes: unhealthy_count,
unresponsive_nodes: unresponsive_count,
timestamp: now,
nodes,
})
}
/// Get health status for a specific node
pub async fn node_health(&self, node_id: &str) -> Result<Option<NodeHealthInfo>> {
let system_health = self.system_health().await?;
Ok(system_health.nodes.get(node_id).cloned())
}
/// Get list of all healthy nodes
pub async fn healthy_nodes(&self) -> Result<Vec<NodeHealthInfo>> {
self.nodes_matching(|n| n.responsive && n.status.level == HealthLevel::Ok)
.await
}
/// Get list of degraded nodes
pub async fn degraded_nodes(&self) -> Result<Vec<NodeHealthInfo>> {
self.nodes_matching(|n| n.responsive && n.status.level == HealthLevel::Degraded)
.await
}
/// Get list of unhealthy nodes
pub async fn unhealthy_nodes(&self) -> Result<Vec<NodeHealthInfo>> {
self.nodes_matching(|n| n.responsive && n.status.level == HealthLevel::Error)
.await
}
/// Get list of unresponsive nodes (haven't reported recently)
pub async fn unresponsive_nodes(&self) -> Result<Vec<NodeHealthInfo>> {
self.nodes_matching(|n| !n.responsive).await
}
/// Shared implementation behind `healthy_nodes`/`degraded_nodes`/`unhealthy_nodes`/
/// `unresponsive_nodes`, which differ only in which predicate over a node's
/// responsiveness/health level they filter by.
async fn nodes_matching(&self, predicate: impl Fn(&NodeHealthInfo) -> bool) -> Result<Vec<NodeHealthInfo>> {
let system_health = self.system_health().await?;
Ok(system_health.nodes.values().filter(|n| predicate(n)).cloned().collect())
}
/// Clear the cache (force fresh data on next query)
pub async fn clear_cache(&self) {
let mut cache = self.cache.write().await;
*cache = None;
}
/// Get health history for a node
///
/// Returns health reports for the specified node over the given duration.
/// History is stored for up to 24 hours.
///
/// # Arguments
///
/// * `node_id` - The node to query history for
/// * `duration` - How far back to retrieve history (e.g., Duration::from_secs(3600) for 1 hour)
///
/// # Example
///
/// ```rust,no_run
/// # use mecha10_core::prelude::*;
/// # use mecha10_core::health::HealthMonitor;
/// # use std::time::Duration;
/// # async fn example() -> Result<()> {
/// let monitor = HealthMonitor::connect("redis://localhost:6379").await?;
///
/// // Get last hour of health history
/// let history = monitor.node_health_history("camera_node", Duration::from_secs(3600)).await?;
/// println!("Found {} health reports in the last hour", history.len());
/// # Ok(())
/// # }
/// ```
pub async fn node_health_history(&self, node_id: &str, duration: Duration) -> Result<Vec<HealthReport>> {
use redis::AsyncCommands;
let mut conn = self
.redis_client
.get_multiplexed_async_connection()
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to connect to Redis: {}", e)))?;
let history_key = format!("mecha10:health:history:{}", node_id);
let now = crate::prelude::now_micros();
let duration_micros = duration.as_micros() as u64;
let start_time = now.saturating_sub(duration_micros);
let results: Vec<String> = conn
.zrangebyscore(&history_key, start_time as f64, now as f64)
.await
.map_err(|e| Mecha10Error::Other(format!("Failed to retrieve health history: {}", e)))?;
let mut reports = Vec::new();
for json_str in results {
match serde_json::from_str::<HealthReport>(&json_str) {
Ok(report) => reports.push(report),
Err(e) => {
tracing::warn!("Failed to deserialize health report: {}", e);
continue;
}
}
}
Ok(reports)
}
}
#[async_trait::async_trait]
pub trait HealthReportingExt {
/// Start automatic health reporting with default settings (Normal priority, 5s interval)
///
/// Spawns a background task that periodically reports health status.
/// The health_check_fn is called to generate the current health status.
///
/// # Example
///
/// ```rust,no_run
/// use mecha10_core::prelude::*;
/// use mecha10_core::health::HealthReportingExt;
/// use mecha10_core::messages::HealthStatus;
///
/// # async fn example(ctx: &Context) -> Result<()> {
/// // Report healthy status every 5 seconds (default)
/// ctx.start_health_reporting(|| async { HealthStatus::healthy() }).await?;
/// # Ok(())
/// # }
/// ```
async fn start_health_reporting<F, Fut>(&self, health_check_fn: F) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send;
/// Start automatic health reporting with custom configuration
///
/// # Example
///
/// ```rust,no_run
/// use mecha10_core::prelude::*;
/// use mecha10_core::health::HealthReportingExt;
/// use mecha10_core::messages::HealthStatus;
/// use mecha10_core::node::HealthPriority;
/// use std::time::Duration;
///
/// # async fn example(ctx: &Context) -> Result<()> {
/// // Report every second with Critical priority
/// ctx.start_health_reporting_with_config(
/// HealthPriority::Critical,
/// Duration::from_secs(1),
/// || async { HealthStatus::healthy() }
/// ).await?;
/// # Ok(())
/// # }
/// ```
async fn start_health_reporting_with_config<F, Fut>(
&self,
priority: HealthPriority,
interval: Duration,
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send;
/// Start automatic health reporting with default settings (Normal
/// priority, 5s interval), tagged with an explicit `node_type`/`tags`
/// pair.
///
/// For nodes that build directly on `Context` rather than going through
/// `run_node::<N: NodeImpl>` (which threads `N::NODE_TYPE`/`N::NODE_TAGS`
/// through automatically) -- e.g. `image-classifier`, `object-detector`,
/// `llm-command`, `websocket-bridge` -- this is how they report their own
/// type/tags on every `/system/health` heartbeat.
async fn start_health_reporting_with_type_tags<F, Fut>(
&self,
node_type: Option<NodeType>,
tags: &'static [NodeTag],
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send;
/// Start automatic health reporting with custom configuration, tagged
/// with an explicit `node_type`/`tags` pair. See
/// [`HealthReportingExt::start_health_reporting_with_type_tags`].
async fn start_health_reporting_with_config_and_type_tags<F, Fut>(
&self,
priority: HealthPriority,
interval: Duration,
node_type: Option<NodeType>,
tags: &'static [NodeTag],
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send;
}
#[async_trait::async_trait]
impl HealthReportingExt for Context {
async fn start_health_reporting<F, Fut>(&self, health_check_fn: F) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send,
{
self.start_health_reporting_with_config(HealthPriority::Normal, Duration::from_secs(5), health_check_fn)
.await
}
async fn start_health_reporting_with_config<F, Fut>(
&self,
priority: HealthPriority,
interval: Duration,
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send,
{
self.start_health_reporting_with_config_and_type_tags(priority, interval, None, &[], health_check_fn)
.await
}
async fn start_health_reporting_with_type_tags<F, Fut>(
&self,
node_type: Option<NodeType>,
tags: &'static [NodeTag],
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send,
{
self.start_health_reporting_with_config_and_type_tags(
HealthPriority::Normal,
Duration::from_secs(5),
node_type,
tags,
health_check_fn,
)
.await
}
async fn start_health_reporting_with_config_and_type_tags<F, Fut>(
&self,
priority: HealthPriority,
interval: Duration,
node_type: Option<NodeType>,
tags: &'static [NodeTag],
health_check_fn: F,
) -> Result<()>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = HealthStatus> + Send,
{
let reporter = HealthReporter::with_config_and_type_tags(self, priority, 60, true, node_type, tags).await?;
tracing::info!(
"Health reporting enabled: priority={:?}, interval={:?}",
priority,
interval
);
tokio::spawn(async move {
let mut ticker = tokio::time::interval(interval);
loop {
ticker.tick().await;
let status = health_check_fn().await;
if let Err(e) = reporter.report(status).await {
tracing::debug!("Failed to report health: {}", e);
}
}
});
Ok(())
}
}
/// `LAB-4131`: multi-instance node support's `instance_id` field on `HealthReport`.
#[cfg(test)]
mod instance_id_tests {
use super::*;
#[test]
fn health_report_round_trips_a_set_instance_id() {
let report = HealthReport {
node_id: "@mecha10/camera/real".to_string(),
status: HealthStatus::healthy(),
priority: HealthPriority::Normal,
reported_at: 0,
node_type: None,
tags: Vec::new(),
instance_id: Some("real".to_string()),
};
let json = serde_json::to_string(&report).expect("HealthReport should serialize");
let round_tripped: HealthReport = serde_json::from_str(&json).expect("HealthReport should deserialize");
assert_eq!(round_tripped.instance_id, Some("real".to_string()));
}
/// A health report emitted by an older node binary (before this field existed) must still
/// deserialize, with `instance_id` defaulting to `None` rather than failing to parse.
#[test]
fn health_report_without_instance_id_field_deserializes_as_none() {
let json = serde_json::json!({
"node_id": "@mecha10/camera",
"status": { "timestamp": 0, "level": "Ok", "message": "" },
"reported_at": 0
})
.to_string();
let report: HealthReport = serde_json::from_str(&json).expect("legacy HealthReport should deserialize");
assert_eq!(report.instance_id, None);
}
}