@@ -193,6 +193,35 @@ pub struct AdminStateConfig {
193193 pub worker_count_override : Option < u32 > ,
194194 /// `ADMIN_PASSWORD` when the environment set a non-empty one (Value 10).
195195 pub password_override : Option < String > ,
196+ /// The scrape endpoint, when the composition root enabled it. `None` leaves
197+ /// `<prefix>/metrics` unregistered, so it answers the router's `404` (ADR 0021,
198+ /// decision 2).
199+ pub metrics : Option < Arc < PrometheusMetrics > > ,
200+ }
201+
202+ /// The scrape endpoint's composition: the bearer token it accepts and the counters it renders.
203+ ///
204+ /// Built by the daemon's edge from `METRICS_ENABLED` and `METRICS_TOKEN` (ADR 0021, decision
205+ /// 2); a case constructs it directly. `token: None` is the enabled endpoint with no token
206+ /// configured, which answers `401` to everything.
207+ pub struct PrometheusMetrics {
208+ pub token : Option < Vec < u8 > > ,
209+ pub counters : Arc < nostream_relay:: metrics:: RelayMetrics > ,
210+ pub connections : Arc < nostream_relay:: connection_cap:: ConnectionCap > ,
211+ }
212+
213+ /// The token is the bearer credential and prints redacted, as `AdminState`'s secret does; the
214+ /// two counters print as their type name, because their own `Debug` dumps every gauge and a
215+ /// log line needs the handle, not its contents.
216+ impl std:: fmt:: Debug for PrometheusMetrics {
217+ fn fmt ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
218+ formatter
219+ . debug_struct ( "PrometheusMetrics" )
220+ . field ( "token" , & self . token . as_ref ( ) . map ( |_| "***" ) )
221+ . field ( "counters" , & "RelayMetrics" )
222+ . field ( "connections" , & "ConnectionCap" )
223+ . finish ( )
224+ }
196225}
197226
198227/// The Grafana links the dashboard template substitutes (`src/utils/admin-grafana.ts`).
@@ -229,6 +258,7 @@ pub struct AdminState {
229258 worker_index : Option < String > ,
230259 worker_count_override : Option < u32 > ,
231260 password_override : Option < String > ,
261+ metrics : Option < Arc < PrometheusMetrics > > ,
232262}
233263
234264impl AdminState {
@@ -257,9 +287,15 @@ impl AdminState {
257287 worker_index : config. worker_index ,
258288 worker_count_override : config. worker_count_override ,
259289 password_override : config. password_override ,
290+ metrics : config. metrics ,
260291 }
261292 }
262293
294+ /// The scrape endpoint, when the composition root enabled it.
295+ pub fn prometheus_metrics ( & self ) -> Option < & Arc < PrometheusMetrics > > {
296+ self . metrics . as_ref ( )
297+ }
298+
263299 /// The resolved settings paths.
264300 pub fn paths ( & self ) -> & SettingsPaths {
265301 & self . paths
@@ -812,3 +848,30 @@ fn log_issues(issues: &[Issue]) {
812848 tracing:: warn!( issue = %issue, "the settings document does not validate" ) ;
813849 }
814850}
851+
852+ #[ cfg( test) ]
853+ mod tests {
854+ #![ allow( clippy:: unwrap_used, clippy:: expect_used) ]
855+
856+ use super :: * ;
857+
858+ /// The bearer credential must not reach a log through `Debug`: the derived impl printed it
859+ /// as decimal bytes.
860+ #[ test]
861+ fn debug_redacts_the_metrics_token ( ) {
862+ let token = b"probe-token" . to_vec ( ) ;
863+ let leaked = format ! ( "{token:?}" ) ;
864+ let metrics = PrometheusMetrics {
865+ token : Some ( token) ,
866+ counters : Arc :: new ( nostream_relay:: metrics:: RelayMetrics :: default ( ) ) ,
867+ connections : Arc :: new ( nostream_relay:: connection_cap:: ConnectionCap :: new (
868+ usize:: MAX ,
869+ ) ) ,
870+ } ;
871+
872+ let printed = format ! ( "{metrics:?}" ) ;
873+
874+ assert ! ( printed. contains( r#"token: Some("***")"# ) , "{printed}" ) ;
875+ assert ! ( !printed. contains( & leaked) , "{printed}" ) ;
876+ }
877+ }
0 commit comments