archivist_lib/logs/
stats.rs

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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::logs::stored_message::StoredMessage;
use diagnostics_data::Severity;
use fuchsia_inspect::{IntProperty, Node, NumericProperty, Property, StringProperty, UintProperty};
use fuchsia_inspect_derive::Inspect;

#[derive(Debug, Default, Inspect)]
pub struct LogStreamStats {
    sockets_opened: UintProperty,
    sockets_closed: UintProperty,
    last_timestamp: IntProperty,
    total: LogCounter,
    rolled_out: LogCounter,
    fatal: LogCounter,
    error: LogCounter,
    warn: LogCounter,
    info: LogCounter,
    debug: LogCounter,
    trace: LogCounter,
    url: StringProperty,
    invalid: LogCounter,
    inspect_node: Node,
}

impl LogStreamStats {
    pub fn set_url(&self, url: &str) {
        self.url.set(url);
    }

    pub fn open_socket(&self) {
        self.sockets_opened.add(1);
    }

    pub fn close_socket(&self) {
        self.sockets_closed.add(1);
    }

    pub fn increment_rolled_out(&self, msg_len: usize) {
        self.rolled_out.increment_bytes(msg_len);
    }

    pub fn increment_invalid(&self, bytes: usize) {
        self.invalid.increment_bytes(bytes);
    }

    pub fn ingest_message(&self, msg: &StoredMessage) {
        self.last_timestamp.set(msg.timestamp().into_nanos());
        self.total.count(msg);
        match msg.severity() {
            Severity::Trace => self.trace.count(msg),
            Severity::Debug => self.debug.count(msg),
            Severity::Info => self.info.count(msg),
            Severity::Warn => self.warn.count(msg),
            Severity::Error => self.error.count(msg),
            Severity::Fatal => self.fatal.count(msg),
        }
    }
}

#[derive(Debug, Default, Inspect)]
struct LogCounter {
    number: UintProperty,
    bytes: UintProperty,

    inspect_node: Node,
}

impl LogCounter {
    fn count(&self, msg: &StoredMessage) {
        self.number.add(1);
        self.bytes.add(msg.size() as u64);
    }

    fn increment_bytes(&self, bytes: usize) {
        self.number.add(1);
        self.bytes.add(bytes as u64);
    }
}