Skip to content

Commit

Permalink
[refac] Move rpc NodeStats types to snarkos_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sadroeck committed Jun 17, 2021
1 parent 76e8f8d commit dffd8bf
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 174 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ version = "0.16"
version = "0.12"
features = [ "process" ]

[dependencies.serde]
version = "1.0"
features = ["derive"]

[dependencies.tokio]
version = "1"
features = [ "macros", "rt-multi-thread" ]
Expand Down
1 change: 1 addition & 0 deletions metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod prometheus;

mod metric_types;

pub mod snapshots;
pub mod stats;
#[cfg(test)]
pub mod testing;
8 changes: 8 additions & 0 deletions metrics/src/metric_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl Counter {
pub(crate) fn increment(&self, val: u64) {
self.0.fetch_add(val, Ordering::SeqCst);
}

pub fn read(&self) -> u64 {
self.0.load(Ordering::Relaxed)
}
}

/// Mimics a [`metrics-core`] arbitrarily increasing & decreasing [`Gauge`]
Expand All @@ -35,4 +39,8 @@ impl Gauge {
// TODO: @sadroeck - Reinterpret as f64 & do atomic C&S
self.0.fetch_sub(val as u64, Ordering::SeqCst);
}

pub fn read(&self) -> f64 {
todo!("@sadroeck add me")
}
}
112 changes: 112 additions & 0 deletions metrics/src/snapshots.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use serde::{Deserialize, Serialize};

/// Returned value for the `getnodestats` rpc call
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeStats {
/// Stats related to messages received by the node.
pub inbound: NodeInboundStats,
/// Stats related to messages sent by the node.
pub outbound: NodeOutboundStats,
/// Stats related to the node's connections.
pub connections: NodeConnectionStats,
/// Stats related to the node's handshakes.
pub handshakes: NodeHandshakeStats,
/// Stats related to the node's queues.
pub queues: NodeQueueStats,
/// Miscellaneous stats related to the node.
pub misc: NodeMiscStats,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeInboundStats {
/// The number of successfully processed inbound messages.
pub all_successes: u64,
/// The number of inbound messages that couldn't be processed.
pub all_failures: u64,
/// The number of all received `Block` messages.
pub blocks: u64,
/// The number of all received `GetBlocks` messages.
pub getblocks: u64,
/// The number of all received `GetMemoryPool` messages.
pub getmemorypool: u64,
/// The number of all received `GetPeers` messages.
pub getpeers: u64,
/// The number of all received `GetSync` messages.
pub getsync: u64,
/// The number of all received `MemoryPool` messages.
pub memorypool: u64,
/// The number of all received `Peers` messages.
pub peers: u64,
/// The number of all received `Ping` messages.
pub pings: u64,
/// The number of all received `Pong` messages.
pub pongs: u64,
/// The number of all received `Sync` messages.
pub syncs: u64,
/// The number of all received `SyncBlock` messages.
pub syncblocks: u64,
/// The number of all received `Transaction` messages.
pub transactions: u64,
/// The number of all received `Unknown` messages.
pub unknown: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeOutboundStats {
/// The number of messages successfully sent by the node.
pub all_successes: u64,
/// The number of messages that failed to be sent to peers.
pub all_failures: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeConnectionStats {
/// The number of all connections the node has accepted.
pub all_accepted: u64,
/// The number of all connections the node has initiated.
pub all_initiated: u64,
/// The number of rejected inbound connection requests.
pub all_rejected: u64,
/// Number of currently connected peers.
pub connected_peers: u32,
/// Number of known disconnected peers.
pub disconnected_peers: u32,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeHandshakeStats {
/// The number of failed handshakes as the initiator.
pub failures_init: u64,
/// The number of failed handshakes as the responder.
pub failures_resp: u64,
/// The number of successful handshakes as the initiator.
pub successes_init: u64,
/// The number of successful handshakes as the responder.
pub successes_resp: u64,
/// The number of handshake timeouts as the initiator.
pub timeouts_init: u64,
/// The number of handshake timeouts as the responder.
pub timeouts_resp: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeQueueStats {
/// The number of messages queued in the common inbound channel.
pub inbound: u64,
/// The number of messages queued in the individual outbound channels.
pub outbound: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NodeMiscStats {
/// The current block height of the node.
pub block_height: u64,
/// The number of blocks the node has mined.
pub blocks_mined: u64,
/// The number of duplicate blocks received.
pub duplicate_blocks: u64,
/// The number of duplicate sync blocks received.
pub duplicate_sync_blocks: u64,
/// The number of RPC requests received.
pub rpc_requests: u64,
}
89 changes: 86 additions & 3 deletions metrics/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use metrics::{GaugeValue, Key, Recorder, Unit};

use crate::metric_types::{Counter, Gauge};
use crate::snapshots::{
NodeConnectionStats, NodeHandshakeStats, NodeInboundStats, NodeMiscStats, NodeOutboundStats, NodeQueueStats,
NodeStats,
};

pub const INBOUND_ALL_SUCCESSES: &str = "snarkos_inbound_all_successes_total";
pub const INBOUND_ALL_FAILURES: &str = "snarkos_inbound_all_failures_total";
Expand Down Expand Up @@ -62,8 +66,6 @@ pub const MISC_RPC_REQUESTS: &str = "snarkos_misc_rpc_requests_total";

pub static NODE_STATS: Stats = Stats::new();

// TODO: make members private and make gathering of stats feature-gated and possibly
// interchangeable with prometheus metrics.
pub struct Stats {
/// Stats related to messages received by the node.
inbound: InboundStats,
Expand All @@ -90,14 +92,24 @@ impl Stats {
misc: MiscStats::new(),
}
}

pub fn snapshot(&self) -> NodeStats {
NodeStats {
inbound: self.inbound.snapshot(),
outbound: self.outbound.snapshot(),
connections: self.connections.snapshot(),
handshakes: self.handshakes.snapshot(),
queues: self.queues.snapshot(),
misc: self.misc.snapshot(),
}
}
}

pub struct InboundStats {
/// The number of successfully processed inbound messages.
all_successes: Counter,
/// The number of inbound messages that couldn't be processed.
all_failures: Counter,

/// The number of all received `Block` messages.
blocks: Counter,
/// The number of all received `GetBlocks` messages.
Expand Down Expand Up @@ -146,6 +158,26 @@ impl InboundStats {
unknown: Counter::new(),
}
}

pub fn snapshot(&self) -> NodeInboundStats {
NodeInboundStats {
all_successes: self.all_successes.read(),
all_failures: self.all_failures.read(),
blocks: self.blocks.read(),
getblocks: self.getblocks.read(),
getmemorypool: self.getmemorypool.read(),
getpeers: self.getpeers.read(),
getsync: self.getsync.read(),
memorypool: self.memorypool.read(),
peers: self.peers.read(),
pings: self.pings.read(),
pongs: self.pongs.read(),
syncs: self.syncs.read(),
syncblocks: self.syncblocks.read(),
transactions: self.transactions.read(),
unknown: self.unknown.read(),
}
}
}

pub struct OutboundStats {
Expand All @@ -162,6 +194,13 @@ impl OutboundStats {
all_failures: Counter::new(),
}
}

pub fn snapshot(&self) -> NodeOutboundStats {
NodeOutboundStats {
all_successes: self.all_successes.read(),
all_failures: self.all_failures.read(),
}
}
}

pub struct ConnectionStats {
Expand All @@ -171,6 +210,10 @@ pub struct ConnectionStats {
all_initiated: Counter,
/// The number of rejected inbound connection requests.
all_rejected: Counter,
/// Number of currently connected peers.
connected_peers: Gauge,
/// Number of known disconnected peers.
disconnected_peers: Gauge,
}

impl ConnectionStats {
Expand All @@ -179,6 +222,18 @@ impl ConnectionStats {
all_accepted: Counter::new(),
all_initiated: Counter::new(),
all_rejected: Counter::new(),
connected_peers: Gauge::new(),
disconnected_peers: Gauge::new(),
}
}

pub fn snapshot(&self) -> NodeConnectionStats {
NodeConnectionStats {
all_accepted: self.all_accepted.read(),
all_initiated: self.all_initiated.read(),
all_rejected: self.all_rejected.read(),
connected_peers: self.connected_peers.read() as u32,
disconnected_peers: self.disconnected_peers.read() as u32,
}
}
}
Expand Down Expand Up @@ -209,6 +264,17 @@ impl HandshakeStats {
timeouts_resp: Counter::new(),
}
}

pub fn snapshot(&self) -> NodeHandshakeStats {
NodeHandshakeStats {
successes_init: self.successes_init.read(),
successes_resp: self.successes_resp.read(),
failures_init: self.failures_init.read(),
failures_resp: self.failures_resp.read(),
timeouts_init: self.timeouts_init.read(),
timeouts_resp: self.timeouts_resp.read(),
}
}
}

pub struct QueueStats {
Expand All @@ -225,6 +291,13 @@ impl QueueStats {
outbound: Gauge::new(),
}
}

pub fn snapshot(&self) -> NodeQueueStats {
NodeQueueStats {
inbound: self.inbound.read(),
outbound: self.outbound.read(),
}
}
}

pub struct MiscStats {
Expand All @@ -249,6 +322,16 @@ impl MiscStats {
rpc_requests: Counter::new(),
}
}

pub fn snapshot(&self) -> NodeMiscStats {
NodeMiscStats {
block_height: self.block_height.read(),
blocks_mined: self.blocks_mined.read(),
duplicate_blocks: self.duplicate_blocks.read(),
duplicate_sync_blocks: self.duplicate_sync_blocks.read(),
rpc_requests: self.rpc_requests.read(),
}
}
}

impl Recorder for Stats {
Expand Down
4 changes: 4 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ version = "17"
[dependencies.metrics]
version = "0.16"

[dependencies.snarkos-metrics]
version = "1.3.8"
path = "../metrics"

[dependencies.parking_lot]
version = "0.11.1"

Expand Down
Loading

0 comments on commit dffd8bf

Please sign in to comment.