Skip to content

Commit

Permalink
More idiomatic way to measure time for Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
pbeza committed Jul 5, 2024
1 parent e9a9498 commit 0b5a48e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
6 changes: 2 additions & 4 deletions core/bin/zksync_tee_prover/src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Instant;

use reqwest::Client;
use secp256k1::{ecdsa::Signature, PublicKey};
use serde::{de::DeserializeOwned, Serialize};
Expand Down Expand Up @@ -99,13 +97,13 @@ impl TeeApiClient {
proof: root_hash.as_bytes().into(),
tee_type,
}));
let started_at = Instant::now();
let observer = METRICS.proof_submitting_time.start();
self.post::<_, SubmitTeeProofResponse, _>(
format!("/tee/submit_proofs/{batch_number}").as_str(),
request,
)
.await?;
METRICS.proof_submitting_time.observe(started_at.elapsed());
observer.observe();
tracing::info!(
"Proof submitted successfully for batch number {}",
batch_number
Expand Down
14 changes: 6 additions & 8 deletions core/bin/zksync_tee_prover/src/tee_prover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{Duration, Instant};
use std::time::Duration;

use secp256k1::{ecdsa::Signature, Message, PublicKey, Secp256k1, SecretKey};
use url::Url;
Expand Down Expand Up @@ -83,14 +83,14 @@ impl TeeProver {
) -> Result<(Signature, L1BatchNumber, H256), TeeProverError> {
match tvi {
TeeVerifierInput::V1(tvi) => {
let started_at = Instant::now();
let observer = METRICS.proof_generation_time.start();
let verification_result = tvi.verify().map_err(TeeProverError::Verification)?;
let root_hash_bytes = verification_result.value_hash.as_bytes();
let batch_number = verification_result.batch_number;
let msg_to_sign = Message::from_slice(root_hash_bytes)
.map_err(|e| TeeProverError::Verification(e.into()))?;
let signature = self.signing_key.sign_ecdsa(msg_to_sign);
METRICS.proof_generation_time.observe(started_at.elapsed());
observer.observe();
Ok((signature, batch_number, verification_result.value_hash))
}
_ => Err(TeeProverError::Verification(anyhow::anyhow!(
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Task for TeeProver {

let mut retries = 1;
let mut backoff = self.config.initial_retry_backoff;
let mut job_wait_started_at = Instant::now();
let mut observer = METRICS.job_waiting_time.start();

loop {
if *stop_receiver.0.borrow() {
Expand All @@ -174,13 +174,11 @@ impl Task for TeeProver {
retries = 1;
backoff = self.config.initial_retry_backoff;
if let Some(batch_number) = batch_number {
METRICS
.job_waiting_time
.observe(job_wait_started_at.elapsed());
observer.observe();
observer = METRICS.job_waiting_time.start();
METRICS
.last_batch_number_processed
.set(batch_number.0 as u64);
job_wait_started_at = Instant::now();
}
}
Err(err) => {
Expand Down

0 comments on commit 0b5a48e

Please sign in to comment.