photon_telemetry/lib.rs
1//! Operations telemetry port for Photon.
2//!
3//! Hosts install a process-wide [`OpsLog`] via [`install_ops_log`] or
4//! [`PhotonBuilder::ops_log`](https://docs.rs/uf-photon/latest/photon/struct.PhotonBuilder.html#method.ops_log).
5//! Backend instrumentation (publish counters, DLQ rows, checkpoint failures) calls [`ops_log`].
6//!
7//! ## Entry points
8//!
9//! - [`OpsLog`] — counter / gauge / event trait
10//! - [`install_ops_log`] / [`ops_log`] / [`ops_log_from_env`] — process-wide adapter
11//! - [`ConsoleOpsLog`] / [`NoOpsLog`] — shipped adapters
12//!
13//! Runnable: `cargo run -p uf-photon --example telemetry_ops_log --features runtime,mem`.
14
15mod console;
16mod global;
17mod noop;
18
19#[cfg(feature = "recording")]
20mod recording;
21
22pub use console::ConsoleOpsLog;
23pub use global::{install_ops_log, ops_log, ops_log_from_env};
24pub use noop::NoOpsLog;
25
26#[cfg(feature = "recording")]
27pub use recording::{RecordedCounter, RecordedEvent, RecordedGauge, RecordingOpsLog};
28
29/// Structured ops metrics/events for publish, drain, DLQ, checkpoints.
30///
31/// Install before or during [`PhotonBuilder::build`](https://docs.rs/uf-photon/latest/photon/struct.PhotonBuilder.html#method.build)
32/// via [`PhotonBuilder::ops_log`](https://docs.rs/uf-photon/latest/photon/struct.PhotonBuilder.html#method.ops_log)
33/// (or [`install_ops_log`]). Photon calls this trait from backend instrumentation — it is not the
34/// application event bus.
35///
36/// # Example
37///
38/// ```rust,ignore
39/// use photon_runtime::Photon;
40/// use photon_telemetry::ConsoleOpsLog;
41///
42/// let _photon = Photon::builder()
43/// .ops_log(ConsoleOpsLog)
44/// .auto_registry()
45/// .build()?;
46/// ```
47pub trait OpsLog: Send + Sync {
48 /// Increment a counter with optional labels.
49 fn record_counter(&self, name: &str, labels: &[(&str, &str)], value: f64);
50
51 /// Set a gauge with optional labels.
52 fn record_gauge(&self, name: &str, labels: &[(&str, &str)], value: f64);
53
54 /// Emit a structured diagnostic event.
55 fn log_event(&self, name: &str, payload: &serde_json::Value);
56}
57
58#[cfg(test)]
59mod tests {
60 use super::{ConsoleOpsLog, NoOpsLog, OpsLog};
61
62 #[test]
63 fn noop_ops_log_is_silent() {
64 let log = NoOpsLog;
65 log.record_counter("c", &[], 1.0);
66 log.record_gauge("g", &[], 2.0);
67 log.log_event("e", &serde_json::json!({}));
68 }
69
70 #[test]
71 fn console_ops_log_does_not_panic() {
72 let log = ConsoleOpsLog;
73 log.record_counter("photon_publishes", &[("topic", "t")], 1.0);
74 }
75}