Skip to content

Commit 820a46a

Browse files
whankinsivlowhung
andauthored
feat: gRPC request stats (#784)
Signed-off-by: William Hankins <william@sundae.fi> Co-authored-by: Matthew Hounslow <lowhung@proton.me>
1 parent d7858de commit 820a46a

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

modules/midnight_state/src/grpc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod conversions;
22
pub mod server;
33
mod service;
4+
mod stats;
45
mod utxo_events;
56

67
pub mod midnight_state_proto {

modules/midnight_state/src/grpc/server.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use anyhow::Result;
66
use caryatid_sdk::Context;
77
use tokio::net::TcpListener;
88
use tokio::sync::Mutex;
9+
use tokio::time::{sleep, Duration};
910
use tonic::transport::Server;
1011

1112
use crate::grpc::midnight_state_proto::midnight_state_server::MidnightStateServer;
@@ -23,6 +24,17 @@ pub async fn run(
2324
tracing::info!("gRPC server listening on {}", addr);
2425

2526
let service = MidnightStateService::new(history, context);
27+
28+
// background stats logger
29+
let stats_service = service.clone();
30+
tokio::spawn(async move {
31+
loop {
32+
sleep(Duration::from_secs(60)).await;
33+
let stats = stats_service.stats();
34+
tracing::info!("gRPC request stats: {}", stats);
35+
}
36+
});
37+
2638
let reflection = tonic_reflection::server::Builder::configure()
2739
.register_encoded_file_descriptor_set(FILE_DESCRIPTOR_SET)
2840
.build_v1()?;

modules/midnight_state/src/grpc/service.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::sync::{atomic::Ordering, Arc};
22

33
use crate::{
44
grpc::{
@@ -12,6 +12,7 @@ use crate::{
1212
StakePoolEntry, TechnicalCommitteeDatumRequest, TechnicalCommitteeDatumResponse,
1313
UtxoEvent, UtxoEventsRequest, UtxoEventsResponse,
1414
},
15+
stats::{RequestStats, RequestStatsSnapshot},
1516
utxo_events::truncate_by_tx_capacity,
1617
},
1718
state::State,
@@ -33,14 +34,24 @@ use tonic::{Request, Response, Status};
3334

3435
const MAX_EVENTS_PER_TX: usize = 64;
3536

37+
#[derive(Clone)]
3638
pub struct MidnightStateService {
3739
history: Arc<Mutex<StateHistory<State>>>,
3840
context: Arc<Context<Message>>,
41+
stats: Arc<RequestStats>,
3942
}
4043

4144
impl MidnightStateService {
4245
pub fn new(history: Arc<Mutex<StateHistory<State>>>, context: Arc<Context<Message>>) -> Self {
43-
Self { history, context }
46+
Self {
47+
history,
48+
context,
49+
stats: Arc::new(RequestStats::default()),
50+
}
51+
}
52+
53+
pub fn stats(&self) -> RequestStatsSnapshot {
54+
self.stats.snapshot()
4455
}
4556
}
4657

@@ -176,6 +187,7 @@ impl MidnightState for MidnightStateService {
176187
&self,
177188
request: Request<UtxoEventsRequest>,
178189
) -> Result<Response<UtxoEventsResponse>, Status> {
190+
self.stats.utxo_events.fetch_add(1, Ordering::Relaxed);
179191
let req = request.into_inner();
180192

181193
let start_block = req.start_block;
@@ -251,6 +263,7 @@ impl MidnightState for MidnightStateService {
251263
&self,
252264
request: Request<TechnicalCommitteeDatumRequest>,
253265
) -> Result<Response<TechnicalCommitteeDatumResponse>, Status> {
266+
self.stats.technical_committee_datum.fetch_add(1, Ordering::Relaxed);
254267
let req = request.into_inner();
255268

256269
let technical_committee = {
@@ -281,6 +294,7 @@ impl MidnightState for MidnightStateService {
281294
&self,
282295
request: Request<CouncilDatumRequest>,
283296
) -> Result<Response<CouncilDatumResponse>, Status> {
297+
self.stats.council_datum.fetch_add(1, Ordering::Relaxed);
284298
let req = request.into_inner();
285299

286300
let council = {
@@ -311,6 +325,7 @@ impl MidnightState for MidnightStateService {
311325
&self,
312326
request: Request<AriadneParametersRequest>,
313327
) -> Result<Response<AriadneParametersResponse>, Status> {
328+
self.stats.ariadne_parameters.fetch_add(1, Ordering::Relaxed);
314329
let req = request.into_inner();
315330

316331
let params = {
@@ -341,6 +356,7 @@ impl MidnightState for MidnightStateService {
341356
&self,
342357
request: Request<BlockByHashRequest>,
343358
) -> Result<Response<BlockByHashResponse>, Status> {
359+
self.stats.block_by_hash.fetch_add(1, Ordering::Relaxed);
344360
let req = request.into_inner();
345361
let block_hash = BlockHash::try_from(req.block_hash)
346362
.map_err(|_| Status::invalid_argument("invalid block hash"))?;
@@ -383,6 +399,7 @@ impl MidnightState for MidnightStateService {
383399
&self,
384400
request: Request<EpochNonceRequest>,
385401
) -> Result<Response<EpochNonceResponse>, Status> {
402+
self.stats.epoch_nonce.fetch_add(1, Ordering::Relaxed);
386403
let req = request.into_inner();
387404

388405
let nonce_opt = {
@@ -400,6 +417,7 @@ impl MidnightState for MidnightStateService {
400417
&self,
401418
request: Request<EpochCandidatesRequest>,
402419
) -> Result<Response<EpochCandidatesResponse>, Status> {
420+
self.stats.epoch_candidates.fetch_add(1, Ordering::Relaxed);
403421
let req = request.into_inner();
404422

405423
let candidates = {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::{
2+
fmt,
3+
sync::atomic::{AtomicU64, Ordering},
4+
};
5+
6+
#[derive(Default)]
7+
pub struct RequestStats {
8+
pub utxo_events: AtomicU64,
9+
pub council_datum: AtomicU64,
10+
pub technical_committee_datum: AtomicU64,
11+
pub ariadne_parameters: AtomicU64,
12+
pub block_by_hash: AtomicU64,
13+
pub epoch_nonce: AtomicU64,
14+
pub epoch_candidates: AtomicU64,
15+
}
16+
17+
#[derive(Debug)]
18+
pub struct RequestStatsSnapshot {
19+
pub utxo_events: u64,
20+
pub council_datum: u64,
21+
pub technical_committee_datum: u64,
22+
pub ariadne_parameters: u64,
23+
pub block_by_hash: u64,
24+
pub epoch_nonce: u64,
25+
pub epoch_candidates: u64,
26+
}
27+
28+
impl fmt::Display for RequestStatsSnapshot {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
write!(
31+
f,
32+
"utxo_events={} council_datum={} technical_committee_datum={} \
33+
ariadne_parameters={} block_by_hash={} epoch_nonce={} epoch_candidates={}",
34+
self.utxo_events,
35+
self.council_datum,
36+
self.technical_committee_datum,
37+
self.ariadne_parameters,
38+
self.block_by_hash,
39+
self.epoch_nonce,
40+
self.epoch_candidates
41+
)
42+
}
43+
}
44+
45+
impl RequestStats {
46+
pub fn snapshot(&self) -> RequestStatsSnapshot {
47+
RequestStatsSnapshot {
48+
utxo_events: self.utxo_events.load(Ordering::Relaxed),
49+
council_datum: self.council_datum.load(Ordering::Relaxed),
50+
technical_committee_datum: self.technical_committee_datum.load(Ordering::Relaxed),
51+
ariadne_parameters: self.ariadne_parameters.load(Ordering::Relaxed),
52+
block_by_hash: self.block_by_hash.load(Ordering::Relaxed),
53+
epoch_nonce: self.epoch_nonce.load(Ordering::Relaxed),
54+
epoch_candidates: self.epoch_candidates.load(Ordering::Relaxed),
55+
}
56+
}
57+
}

processes/midnight_indexer/config.preview.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ node-addresses = [
4040
[module.midnight-state]
4141
address-deltas-topic = "cardano.address.deltas"
4242

43-
# The below configuration is for `qanet`: https://github.com/midnightntwrk/midnight-node/tree/6b96e5cfb8c200cba66b624837e1c3e6da72cf82/res/qanet
43+
# The below configuration is for `preview`: https://github.com/midnightntwrk/midnight-node/tree/main/res/preview
4444
# Config for CNight asset
4545
cnight-policy-id = "d2dbff622e509dda256fedbd31ef6e9fd98ed49ad91d5c0e07f68af1"
4646
cnight-asset-name = ""
@@ -50,13 +50,13 @@ mapping-validator-address = "addr_test1wplxjzranravtp574s2wz00md7vz9rzpucu252je6
5050
auth-token-asset-name = ""
5151

5252
# Config for Midnight governance
53-
technical-committee-address = "addr_test1wqx3yfmsp82nmtyjj4k86s3l04l6lvwaqh2vk2ygcge7kdsk4xc7j"
54-
technical-committee-policy-id = "0d12277009d53dac92956c7d423f7d7fafb1dd05d4cb2888c233eb36"
55-
council-address = "addr_test1wqqwkauz0ypglg5e4u780kcp8hzt75u72yg6z7td62gnk0qed0p06"
56-
council-policy-id = "00eb778279028fa299af3c77db013dc4bf539e5111a1796dd2913b3c"
53+
technical-committee-address = "addr_test1wptcy7h9rmkhdnhn3jvm6tuhehcq2hhhzntvn00nq79ph8c44v43j"
54+
technical-committee-policy-id = "57827ae51eed76cef38c99bd2f97cdf0055ef714d6c9bdf3078a1b9f"
55+
council-address = "addr_test1wzy47zdsq22pg9l48c5v0f835ljdjzkz47sa5za9cehcejcw28k2d"
56+
council-policy-id = "895f09b002941417f53e28c7a4f1a7e4d90ac2afa1da0ba5c66f8ccb"
5757

5858
# Config for Midnight Ariadne parameters
59-
permissioned-candidate-policy = "f8625f11a58fa5ab5b85502a8fe5c843ece460c9c5f9273be17d3424"
59+
permissioned-candidate-policy = "24dccfce2576ae6fa7149bc485850656ae6faf9f4158891316773a78"
6060

6161
# Config for Midnight gRPC server
6262
grpc-bind-address = "0.0.0.0:50051"

0 commit comments

Comments
 (0)