Skip to content

Commit

Permalink
use macro
Browse files Browse the repository at this point in the history
  • Loading branch information
longbowlu committed Sep 9, 2022
1 parent 3dc922d commit 16e769c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 68 deletions.
33 changes: 12 additions & 21 deletions crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
checkpoints::CheckpointStore,
event_handler::EventHandler,
execution_engine,
metrics::start_timer,
query_helpers::QueryHelpers,
transaction_input_checker,
transaction_streamer::TransactionStreamer,
Expand Down Expand Up @@ -459,11 +460,8 @@ impl AuthorityState {
let transaction_digest = *transaction.digest();
debug!(tx_digest=?transaction_digest, "handle_transaction. Tx data: {:?}", transaction.signed_data.data);
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(self.metrics.clone(), |metrics| {
metrics
.handle_transaction_latency
.observe(start_ts.elapsed().as_secs_f64());
});
let _metrics_guard =
start_timer!(self.metrics.handle_transaction_latency.clone(), &start_ts);

self.metrics.tx_orders.inc();
// Check the sender's signature.
Expand Down Expand Up @@ -502,12 +500,10 @@ impl AuthorityState {
signed_effects: SignedTransactionEffects,
) -> SuiResult {
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(self.metrics.clone(), |metrics| {
metrics
.handle_node_sync_certificate_latency
.observe(start_ts.elapsed().as_secs_f64());
});

let _metrics_guard = start_timer!(
self.metrics.handle_node_sync_certificate_latency.clone(),
&start_ts
);
let digest = *certificate.digest();
debug!(?digest, "handle_node_sync_transaction");
fp_ensure!(
Expand Down Expand Up @@ -552,11 +548,8 @@ impl AuthorityState {
certificate: CertifiedTransaction,
) -> SuiResult<TransactionInfoResponse> {
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(self.metrics.clone(), |metrics| {
metrics
.handle_certificate_latency
.observe(start_ts.elapsed().as_secs_f64());
});
let _metrics_guard =
start_timer!(self.metrics.handle_certificate_latency.clone(), &start_ts);

self.metrics.total_cert_attempts.inc();
if self.is_fullnode() {
Expand Down Expand Up @@ -1727,11 +1720,9 @@ impl AuthorityState {
signed_effects: &SignedTransactionEffects,
) -> SuiResult {
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(self.metrics.clone(), |metrics| {
metrics
.commit_certificate_latency
.observe(start_ts.elapsed().as_secs_f64());
});
let _metrics_guard =
start_timer!(self.metrics.commit_certificate_latency.clone(), &start_ts);

if self.is_halted() && !certificate.signed_data.data.kind.is_system_tx() {
// TODO: Here we should allow consensus transaction to continue.
// TODO: Do we want to include the new validator set?
Expand Down
53 changes: 20 additions & 33 deletions crates/sui-core/src/authority_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
CheckpointConsensusAdapter, CheckpointSender, ConsensusAdapter, ConsensusAdapterMetrics,
ConsensusListener, ConsensusListenerMessage,
},
metrics::start_timer,
};
use anyhow::anyhow;
use anyhow::Result;
Expand Down Expand Up @@ -330,22 +331,16 @@ impl ValidatorService {
let is_consensus_tx = transaction.contains_shared_object();

let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(metrics.clone(), |metrics| {
let _metrics_guard = start_timer!(
if is_consensus_tx {
metrics
.handle_transaction_consensus_latency
.observe(start_ts.elapsed().as_secs_f64());
metrics.handle_transaction_consensus_latency.clone()
} else {
metrics
.handle_transaction_non_consensus_latency
.observe(start_ts.elapsed().as_secs_f64());
}
});
let tx_verif_metrics_guard = scopeguard::guard(metrics.clone(), |metrics| {
metrics
.tx_verification_latency
.observe(start_ts.elapsed().as_secs_f64());
});
metrics.handle_transaction_non_consensus_latency.clone()
},
&start_ts
);
let tx_verif_metrics_guard =
start_timer!(metrics.tx_verification_latency.clone(), &start_ts);

transaction
.verify()
Expand Down Expand Up @@ -381,23 +376,19 @@ impl ValidatorService {
let mut certificate = request.into_inner();
let is_consensus_tx = certificate.contains_shared_object();
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(metrics.clone(), |metrics| {
let _metrics_guard = start_timer!(
if is_consensus_tx {
metrics
.handle_certificate_consensus_latency
.observe(start_ts.elapsed().as_secs_f64());
metrics.handle_certificate_consensus_latency.clone()
} else {
metrics
.handle_certificate_non_consensus_latency
.observe(start_ts.elapsed().as_secs_f64());
}
});
metrics.handle_certificate_non_consensus_latency.clone()
},
&start_ts
);

// 1) Verify certificate
let cert_verif_metrics_guard = scopeguard::guard(metrics.clone(), |metrics| {
metrics
.cert_verification_latency
.observe(start_ts.elapsed().as_secs_f64());
});
let cert_verif_metrics_guard =
start_timer!(metrics.cert_verification_latency.clone(), &start_ts);

certificate
.verify(&state.committee.load())
.map_err(|e| tonic::Status::invalid_argument(e.to_string()))?;
Expand All @@ -424,11 +415,7 @@ impl ValidatorService {
.map_err(|e| tonic::Status::internal(e.to_string()))?
{
let start_ts = Instant::now();
let _metrics_guard = scopeguard::guard(metrics.clone(), |metrics| {
metrics
.consensus_latency
.observe(start_ts.elapsed().as_secs_f64());
});
let _metrics_guard = start_timer!(metrics.consensus_latency.clone(), &start_ts);
consensus_adapter
.submit(&state.name, &certificate)
.await
Expand Down
1 change: 1 addition & 0 deletions crates/sui-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod epoch;
pub mod event_handler;
pub mod execution_engine;
pub mod gateway_state;
pub mod metrics;
pub mod safe_client;
pub mod streamer;
pub mod transaction_input_checker;
Expand Down
14 changes: 14 additions & 0 deletions crates/sui-core/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[macro_export]
macro_rules! start_timer {
($metrics: expr, $start_ts: expr) => {{
let metrics = $metrics;
let start_ts = $start_ts;
scopeguard::guard(metrics, |metrics| {
metrics.observe(start_ts.elapsed().as_secs_f64());
})
}};
}
pub use start_timer;
19 changes: 5 additions & 14 deletions crates/sui-core/src/safe_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::authority_client::{AuthorityAPI, BatchInfoResponseItemStream};
use crate::epoch::epoch_store::EpochStore;
use crate::metrics::start_timer;
use futures::StreamExt;
use prometheus::core::{GenericCounter, GenericGauge};
use prometheus::{
Expand Down Expand Up @@ -462,9 +463,7 @@ where
let digest = *transaction.digest();
let start_ts = Instant::now();
let _metrics_guard =
scopeguard::guard(self.metrics_handle_transaction_latency.clone(), |metrics| {
metrics.observe(start_ts.elapsed().as_secs_f64());
});
start_timer!(self.metrics_handle_transaction_latency.clone(), &start_ts);
let transaction_info = self
.authority_client
.handle_transaction(transaction)
Expand Down Expand Up @@ -500,9 +499,7 @@ where
let digest = *certificate.digest();
let start_ts = Instant::now();
let _metrics_guard =
scopeguard::guard(self.metrics_handle_certificate_latency.clone(), |metrics| {
metrics.observe(start_ts.elapsed().as_secs_f64());
});
start_timer!(self.metrics_handle_certificate_latency.clone(), &start_ts);
let transaction_info = self
.authority_client
.handle_certificate(certificate)
Expand Down Expand Up @@ -531,11 +528,8 @@ where
self.metrics_total_requests_handle_object_info_request.inc();

let start_ts = Instant::now();
let _metrics_guard =
scopeguard::guard(self.metrics_handle_obj_info_latency.clone(), |metrics| {
metrics.observe(start_ts.elapsed().as_secs_f64());
});

let _metrics_guard = start_timer!(self.metrics_handle_obj_info_latency.clone(), &start_ts);
let response = self
.authority_client
.handle_object_info_request(request.clone())
Expand All @@ -559,10 +553,7 @@ where
let digest = request.transaction_digest;

let start_ts = Instant::now();
let _metrics_guard =
scopeguard::guard(self.metrics_handle_tx_info_latency.clone(), |metrics| {
metrics.observe(start_ts.elapsed().as_secs_f64());
});
let _metrics_guard = start_timer!(self.metrics_handle_tx_info_latency.clone(), &start_ts);

let transaction_info = self
.authority_client
Expand Down

0 comments on commit 16e769c

Please sign in to comment.