Skip to content

Commit 9c3fb00

Browse files
committed
[Aptos Data Client] Only update metrics on successful responses.
1 parent ffc6c28 commit 9c3fb00

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

state-sync/aptos-data-client/src/client.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ impl AptosDataClient {
395395
T: TryFrom<StorageServiceResponse, Error = E>,
396396
E: Into<Error>,
397397
{
398+
// Select a peer to service the request
398399
let peer = self.choose_peer_for_request(&request).map_err(|error| {
399400
debug!(
400401
(LogSchema::new(LogEntry::StorageServiceRequest)
@@ -404,7 +405,8 @@ impl AptosDataClient {
404405
);
405406
error
406407
})?;
407-
let _timer = start_request_timer(&metrics::REQUEST_LATENCIES, &request.get_label(), peer);
408+
409+
// Send the request to the peer and transform the response
408410
self.send_request_to_peer_and_decode(peer, request, request_timeout_ms)
409411
.await
410412
}
@@ -420,13 +422,29 @@ impl AptosDataClient {
420422
T: TryFrom<StorageServiceResponse, Error = E>,
421423
E: Into<Error>,
422424
{
425+
// Start the timer for the request
426+
let timer = start_request_timer(&metrics::REQUEST_LATENCIES, &request.get_label(), peer);
427+
428+
// Get the response from the peer
423429
let response = self
424430
.send_request_to_peer(peer, request.clone(), request_timeout_ms)
425-
.await?;
431+
.await;
426432

427-
let (context, storage_response) = response.into_parts();
433+
// If an error occurred, stop the timer (without updating the metrics)
434+
// and return the error. Otherwise, stop the timer and update the metrics.
435+
let storage_response = match response {
436+
Ok(storage_response) => {
437+
timer.stop_and_record(); // Update the latency metrics
438+
storage_response
439+
},
440+
Err(error) => {
441+
timer.stop_and_discard(); // Discard the timer without updating the metrics
442+
return Err(error);
443+
},
444+
};
428445

429446
// Ensure the response obeys the compression requirements
447+
let (context, storage_response) = storage_response.into_parts();
430448
if request.use_compression && !storage_response.is_compressed() {
431449
return Err(Error::InvalidResponse(format!(
432450
"Requested compressed data, but the response was uncompressed! Response: {:?}",
@@ -439,10 +457,10 @@ impl AptosDataClient {
439457
)));
440458
}
441459

442-
// try to convert the storage service enum into the exact variant we're expecting.
460+
// Try to convert the storage service enum into the exact variant we're expecting
443461
match T::try_from(storage_response) {
444462
Ok(new_payload) => Ok(Response::new(context, new_payload)),
445-
// if the variant doesn't match what we're expecting, report the issue.
463+
// If the variant doesn't match what we're expecting, report the issue
446464
Err(err) => {
447465
context
448466
.response_callback

state-sync/aptos-data-client/src/poller.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
latency_monitor::LatencyMonitor,
1010
logging::{LogEntry, LogEvent, LogSchema},
1111
metrics,
12-
metrics::{set_gauge, start_request_timer, DataType, PRIORITIZED_PEER, REGULAR_PEER},
12+
metrics::{set_gauge, DataType, PRIORITIZED_PEER, REGULAR_PEER},
1313
utils,
1414
utils::choose_peers_by_latency,
1515
};
@@ -408,21 +408,13 @@ pub(crate) fn poll_peer(
408408
let use_compression = data_summary_poller.data_client_config.use_compression;
409409
let storage_request = StorageServiceRequest::new(data_request, use_compression);
410410

411-
// Start the peer polling timer
412-
let timer = start_request_timer(
413-
&metrics::REQUEST_LATENCIES,
414-
&storage_request.get_label(),
415-
peer,
416-
);
417-
418411
// Fetch the storage summary for the peer and stop the timer
419412
let request_timeout = data_summary_poller.data_client_config.response_timeout_ms;
420413
let result: crate::error::Result<StorageServerSummary> = data_summary_poller
421414
.data_client
422415
.send_request_to_peer_and_decode(peer, storage_request, request_timeout)
423416
.await
424417
.map(Response::into_payload);
425-
drop(timer);
426418

427419
// Mark the in-flight poll as now complete
428420
data_summary_poller.in_flight_request_complete(&peer);

0 commit comments

Comments
 (0)