summaryrefslogtreecommitdiffstats
path: root/src/api/stats.rs
blob: 889b675e33fd8dcaf3006de442d8a29862d76b16 (plain)
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
//! /admin/stats: how this node is doing, in numbers. JSON, or the Prometheus text
//! format when the client asks for it (`prometheus`).

mod prometheus;

use axum::{
    Json,
    extract::State,
    http::{HeaderMap, header},
    response::{IntoResponse, Response},
};
use std::collections::BTreeMap;

use serde::Serialize;

use super::peers::EpochInfo;
use crate::state::{AppState, Incidents};
use crate::swarm::{members, stability, topology};
use crate::transport::stats::TransportStats;
use crate::transport::traffic::PeerTraffic;

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct StatsResponse {
    node_id: String,
    /// Where this node can be reached (`Transport::local_addresses`).
    addresses: Vec<String>,
    alias: Option<String>,
    uptime_secs: u64,
    ready: bool,
    epoch: EpochInfo,
    document: DocumentStats,
    catch_up: CatchUpStats,
    remakes: RemakeStats,
    sync: SyncStats,
    relay: RelayStats,
    topology: TopologyStats,
    stability: StabilityStats,
    /// HELLO_REPLYs to an earlier run of ours, ignored.
    stale_hello_replies: u64,
    /// All peers together, including ones no longer listed in /peers.
    traffic: PeerTraffic,
    /// The peers listed in /peers together (a forgotten peer's are gone).
    incidents: Incidents,
    propagation: PropagationStats,
    /// This node's own files (`files`), by kind.
    disk: BTreeMap<&'static str, DiskStats>,
    /// How the transports find and reach peers (`Transport::stats`).
    transports: TransportStats,
}

/// Reads and writes of one kind of file, since the node started.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct DiskStats {
    reads: u64,
    read_bytes: u64,
    writes: u64,
    written_bytes: u64,
    appends: u64,
    appended_bytes: u64,
    errors: u64,
}

/// How long changes written elsewhere took from their commit there until applied here,
/// once caught up. Includes the clock skew between the writer and us.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct PropagationStats {
    count: u64,
    sum_ms: u64,
    /// Changes stamped later than our clock on arrival (writer's clock ahead).
    ahead: u64,
    /// Not cumulative; the last bucket has no bound.
    buckets: Vec<PropagationBucket>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct PropagationBucket {
    le_ms: Option<u64>,
    count: u64,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct DocumentStats {
    heads: usize,
    /// Most heads after applying a sync message (concurrent changes not yet merged).
    max_heads: usize,
    /// Size as saved (compressed): about what a joining node downloads. Measured in the
    /// background, at least a minute apart (longer for large documents); absent until
    /// first measured.
    saved_bytes: Option<u64>,
    /// How long ago `saved_bytes` was measured.
    saved_bytes_secs_ago: Option<u64>,
    /// How long the last measurement took (copy and save), in milliseconds.
    saved_bytes_took_ms: Option<u64>,
    /// How long takers of the document's lock waited for it, since start: the node's
    /// work on the one document queues there.
    lock_wait: crate::document::lock::LockWaits,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct CatchUpStats {
    /// Catch-up sources chosen (more than one: it switched).
    sources: u32,
    /// Seconds from start until the initial sync was done; absent until then (and on
    /// the genesis node).
    done_after_secs: Option<f64>,
    /// Sync payload received until then.
    sync_bytes: Option<u64>,
}

/// How sync with the neighbours goes.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct SyncStats {
    /// Since a sync message from any neighbour was applied; absent if none ever was. A
    /// node whose writes go nowhere shows it growing.
    last_from_neighbour_secs: Option<u64>,
    /// Peers whose heads differ from ours while sync is idle (`peers.md`, healing).
    diverged_peers: usize,
    /// The largest sync message received since start (its Automerge part). Larger
    /// transfers come as deltas (blobs).
    largest_received_bytes: u64,
}

/// Passing changes on (`swarm::relay`) and serving blobs, since start.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct RelayStats {
    announce_rounds: u64,
    pulls: u64,
    /// Announcements of heads we lacked that waited for the pull in progress.
    pulls_queued: u64,
    /// Blob requests not answered: too many were being answered.
    blob_requests_dropped: u64,
}

/// Remakes and their aftermath here (`remake::Summary`).
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct RemakeStats {
    /// Remakes of the local epoch not decided yet (expired ones included).
    undecided: usize,
    /// Committed remakes of the local epoch: 1 while switching; more is a double commit.
    committed: usize,
    /// Syncing a newer epoch from scratch.
    rejoining: bool,
    /// Attempts at catching up with a newer epoch (0 when not catching up).
    catch_up_attempts: u32,
    /// Quarantined change sets (`GET /admin/quarantine`), and the oldest one's age.
    quarantined: usize,
    oldest_quarantine_secs: Option<u64>,
    /// Old epochs' documents kept in memory.
    archived_epochs: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct TopologyStats {
    k: usize,
    /// Absent until ready with a member list.
    view: Option<TopologyView>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct TopologyView {
    /// Registered members, us included.
    members: usize,
    /// Members other than us not judged unreachable.
    reachable: usize,
    stable: usize,
    neighbours: usize,
    stable_neighbours: usize,
    full_mesh: bool,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct StabilityStats {
    /// As published in the member registry.
    tier: &'static str,
    /// Fraction of the observed time (up to a day) with a live peer.
    connected_fraction: f64,
    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;
    let accept = headers
        .get(header::ACCEPT)
        .and_then(|value| value.to_str().ok());
    if prometheus::wanted(accept) {
        let body = prometheus::render(&state, &stats).await;
        ([(header::CONTENT_TYPE, prometheus::CONTENT_TYPE)], body).into_response()
    } else {
        Json(stats).into_response()
    }
}

async fn collect(state: &AppState) -> StatsResponse {
    let node = state.stats.snapshot();

    let (heads, tier, remakes) = {
        let replica = state.doc.read().await;
        let heads = replica.heads().len();
        let remakes = crate::remake::summary(&state.remake, &replica.doc, replica.epoch);
        (
            heads,
            members::tier(&replica.doc, state.local_id()),
            remakes,
        )
    };
    let now_ms = state.clock.now_millis();
    let saved = state.stats.saved_size();

    let (connected_fraction, observed) = stability::measured(state);

    let mut incidents = Incidents::default();
    let mut last_from_neighbour: Option<tokio::time::Instant> = None;
    let mut diverged_peers = 0;
    for (peer, peer_state) in state.peers_snapshot().await {
        let peer_state = peer_state.lock().await;
        incidents.add(&peer_state.incidents);
        if peer_state.diverged_since.is_some() {
            diverged_peers += 1;
        }
        if topology::is_neighbour(state, &peer) {
            last_from_neighbour = last_from_neighbour.max(peer_state.last_sync_rx);
        }
    }

    StatsResponse {
        node_id: state.local_id().to_owned(),
        addresses: state.transport.local_addresses(),
        alias: state.alias.clone(),
        uptime_secs: node.uptime.as_secs(),
        ready: state.is_ready().await,
        epoch: state.local_epoch().into(),
        document: DocumentStats {
            heads,
            max_heads: node.max_heads.max(heads),
            saved_bytes: saved.map(|s| s.bytes),
            saved_bytes_secs_ago: saved.map(|s| s.at.elapsed().as_secs()),
            saved_bytes_took_ms: saved.map(|s| s.took.as_millis() as u64),
            lock_wait: state.doc.waits(),
        },
        catch_up: CatchUpStats {
            sources: node.catch_up_sources,
            done_after_secs: node.caught_up.map(|c| c.after.as_secs_f64()),
            sync_bytes: node.caught_up.map(|c| c.sync_bytes),
        },
        remakes: RemakeStats {
            undecided: remakes.undecided,
            committed: remakes.committed,
            rejoining: remakes.rejoining,
            catch_up_attempts: remakes.catch_up_attempts,
            quarantined: remakes.quarantined,
            oldest_quarantine_secs: remakes
                .oldest_quarantine
                .map(|created| now_ms.ms_since(created) / 1000),
            archived_epochs: remakes.archived_epochs,
        },
        sync: SyncStats {
            last_from_neighbour_secs: last_from_neighbour.map(|at| at.elapsed().as_secs()),
            diverged_peers,
            largest_received_bytes: node.largest_sync_received,
        },
        relay: RelayStats {
            announce_rounds: node.relay.announce_rounds,
            pulls: node.relay.pulls,
            pulls_queued: node.relay.pulls_queued,
            blob_requests_dropped: node.blob_requests_dropped,
        },
        topology: TopologyStats {
            k: state.neighbours_k,
            view: topology::summary(state).map(|s| TopologyView {
                members: s.members,
                reachable: s.reachable,
                stable: s.stable,
                neighbours: s.neighbours,
                stable_neighbours: s.stable_neighbours,
                full_mesh: s.full_mesh,
            }),
        },
        stability: StabilityStats {
            tier: tier.as_str(),
            connected_fraction,
            observed_secs: observed.as_secs(),
        },
        stale_hello_replies: node.stale_hello_replies,
        traffic: state.traffic.total(),
        incidents,
        propagation: PropagationStats {
            count: node.propagation.count,
            sum_ms: node.propagation.sum_ms,
            ahead: node.propagation.ahead,
            buckets: node
                .propagation
                .buckets()
                .map(|(le_ms, count)| PropagationBucket { le_ms, count })
                .collect(),
        },
        disk: state
            .files
            .counts()
            .into_iter()
            .map(|(kind, counts)| {
                let stats = DiskStats {
                    reads: counts.reads,
                    read_bytes: counts.read_bytes,
                    writes: counts.writes,
                    written_bytes: counts.written_bytes,
                    appends: counts.appends,
                    appended_bytes: counts.appended_bytes,
                    errors: counts.errors,
                };
                (kind.as_str(), stats)
            })
            .collect(),
        transports: state.transport.stats(),
    }
}