summaryrefslogtreecommitdiffstats
path: root/src/api/stats.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/stats.rs')
-rw-r--r--src/api/stats.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/api/stats.rs b/src/api/stats.rs
index 07521ab..889b675 100644
--- a/src/api/stats.rs
+++ b/src/api/stats.rs
@@ -189,6 +189,52 @@ struct StabilityStats {
observed_secs: u64,
}
+/// What `/admin/stats` of a node that only relays (`--relay-only`) says.
+#[derive(Clone)]
+pub(crate) struct RelayOnly {
+ pub(crate) transport: std::sync::Arc<dyn crate::transport::Transport>,
+ pub(crate) started: tokio::time::Instant,
+}
+
+#[derive(Debug, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct RelayOnlyStats {
+ /// The relay's libp2p peer ID.
+ peer_id: String,
+ /// Where nodes reach it, as addresses to list (`_continuum/relays`).
+ addresses: Vec<String>,
+ uptime_secs: u64,
+ transports: TransportStats,
+}
+
+/// `GET /admin/stats` of a node that only relays.
+#[cfg_attr(not(feature = "libp2p"), allow(dead_code))]
+pub(crate) async fn get_relay_only_stats(
+ State(relay): State<RelayOnly>,
+ headers: HeaderMap,
+) -> Response {
+ let stats = RelayOnlyStats {
+ peer_id: relay.transport.local_id().to_owned(),
+ addresses: relay
+ .transport
+ .local_addresses()
+ .into_iter()
+ .map(|address| format!("libp2p:{address}"))
+ .collect(),
+ uptime_secs: relay.started.elapsed().as_secs(),
+ transports: relay.transport.stats(),
+ };
+ let accept = headers
+ .get(header::ACCEPT)
+ .and_then(|value| value.to_str().ok());
+ if prometheus::wanted(accept) {
+ let body = prometheus::render_relay_only(&stats);
+ ([(header::CONTENT_TYPE, prometheus::CONTENT_TYPE)], body).into_response()
+ } else {
+ Json(stats).into_response()
+ }
+}
+
/// `GET /admin/stats`.
pub(crate) async fn get_stats(State(state): State<AppState>, headers: HeaderMap) -> Response {
let stats = collect(&state).await;