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
//! Application-provided sink for synchronous metric and event fan-out.
//!
//! Implement this trait when your application needs a **publish path** (message bus, RPC, or
//! composite adapter) in addition to or instead of async storage persist. Wire the sink with
//! `Spectra::builder().sink(...)` (see `spectra` / `spectra-runtime`).
//!
//! # Publisher role (publish-consume)
//!
//! In a publisher process, the sink is the bridge out of Spectra:
//!
//! 1. Declare schemas with `spectra_schema!` / `spectra_metric!` (each expansion emits a
//! `*Payload` / `*_TOPIC` pair beside the typed helper).
//! 2. Implement [`SpectraSink`]: map each emit to a topic payload shaped like [`crate::MetricEmit`]
//! / [`crate::SpectraEvent`] and publish on your bus.
//! 3. Wire `.sink(Arc::new(your_sink)).persist_disabled().build()` so the publisher does **not**
//! open the analytics database.
//!
//! Consumer processes subscribe on the bus and write storage via `try_record_counter_now` /
//! `try_log_event_now` (or a storage backend). The `spectra` crate documents the full
//! publisher/consumer split under **Getting started → Publish-consume**. Spectra does not embed a bus;
//! [Photon](https://github.com/unified-field-dev/photon) (`uf-photon`) is a common host choice
//! for durable pub/sub.
//!
//! ```ignore
//! use std::sync::Arc;
//! use spectra_core::SpectraSink;
//! // Host binary also depends on `spectra` for Spectra::builder() and backends.
//!
//! struct BusPublishSink { /* Photon handle, channel, … */ }
//!
//! impl SpectraSink for BusPublishSink {
//! fn record_counter(&self, name: &str, labels: &[(&str, &str)], delta: i64) {
//! // Build a topics::* payload and publish asynchronously — do not block.
//! let _ = (name, labels, delta);
//! }
//! fn record_gauge(&self, name: &str, labels: &[(&str, &str)], value: f64) {
//! let _ = (name, labels, value);
//! }
//! fn log_event(&self, table: &str, fields: &serde_json::Value) {
//! let _ = (table, fields);
//! }
//! }
//! ```
//!
//! # Design notes
//!
//! - Invoked on the emit thread unless buffering ([`crate::emit_buffer`]) or async persist
//! defers replay. Prefer non-blocking publish (spawn, channel, Photon buffering).
//! - [`crate::try_record_counter`] and related emit functions no-op when re-entering sink
//! dispatch to prevent loops.
//! - Pair with [`crate::RecordingSink`] in tests or [`crate::ChainedSink`] to fan out to
//! multiple handlers.
//! - For dual-path (transport **and** local persist), omit `persist_disabled` so the runtime
//! wraps your sink with storage persist.
//!
//! # Examples
//!
//! ```
//! use spectra_core::SpectraSink;
//! use serde_json::json;
//!
//! struct PrintSink;
//!
//! impl SpectraSink for PrintSink {
//! fn record_counter(&self, name: &str, labels: &[(&str, &str)], delta: i64) {
//! println!("counter {name} +{delta} {labels:?}");
//! }
//! fn record_gauge(&self, name: &str, labels: &[(&str, &str)], value: f64) {
//! println!("gauge {name} = {value} {labels:?}");
//! }
//! fn log_event(&self, table: &str, fields: &serde_json::Value) {
//! println!("event {table}: {fields}");
//! }
//! }
//!
//! let sink = PrintSink;
//! sink.record_counter("requests_total", &[("method", "GET")], 1);
//! sink.log_event("request_log", &json!({"status": 200}));
//! ```
/// Synchronous fan-out target for metrics and structured events.
///
/// Implement this trait for a transport publish adapter, telemetry mirror, or other destination
/// that should receive emits. In distributed mode this is the **publisher** boundary — see the
/// [module docs](self).
///
/// Implementations run inline unless a surrounding buffer or adapter moves work off-thread, so
/// handlers should avoid blocking.
///
/// # Examples
///
/// ```
/// use serde_json::Value;
/// use spectra_core::SpectraSink;
///
/// struct PrintSink;
///
/// impl SpectraSink for PrintSink {
/// fn record_counter(&self, name: &str, labels: &[(&str, &str)], delta: i64) {
/// println!("{name} +{delta} {labels:?}");
/// }
///
/// fn record_gauge(&self, name: &str, labels: &[(&str, &str)], value: f64) {
/// println!("{name} = {value} {labels:?}");
/// }
///
/// fn log_event(&self, table: &str, fields: &Value) {
/// println!("{table}: {fields}");
/// }
/// }
///
/// let sink = PrintSink;
/// sink.record_counter("requests_total", &[("method", "GET")], 1);
/// ```