Skip to content

Commit

Permalink
feat: add offer metric to state bridge (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Oct 17, 2024
1 parent d18cc49 commit 3fc5130
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
73 changes: 40 additions & 33 deletions portal-bridge/src/bridge/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ impl StateBridge {
let global_offer_report = self.global_offer_report.clone();
tokio::spawn(async move {
let timer = metrics.start_process_timer("spawn_offer_state_proof");
match timeout(

let result = timeout(
SERVE_BLOCK_TIMEOUT,
StateNetworkApiClient::trace_offer(
&portal_client,
Expand All @@ -326,43 +327,49 @@ impl StateBridge {
content_value.encode(),
),
)
.await
{
Ok(result) => match result {
Ok(result) => {
global_offer_report
.lock()
.expect("to acquire lock")
.update(&result);
offer_report
.lock()
.expect("to acquire lock")
.update(&enr, &result);
}
Err(e) => {
global_offer_report
.lock()
.expect("to acquire lock")
.update(&OfferTrace::Failed);
offer_report
.lock()
.expect("to acquire lock")
.update(&enr, &OfferTrace::Failed);
warn!("Error offering to: {enr}, error: {e:?}");
.await;

let offer_trace = match &result {
Ok(Ok(result)) => {
if matches!(result, &OfferTrace::Failed) {
warn!("Internal error offering to: {enr}");
}
},
result
}
Ok(Err(err)) => {
warn!("Error offering to: {enr}, error: {err:?}");
&OfferTrace::Failed
}
Err(_) => {
global_offer_report
.lock()
.expect("to acquire lock")
.update(&OfferTrace::Failed);
offer_report
.lock()
.expect("to acquire lock")
.update(&enr, &OfferTrace::Failed);
error!("trace_offer timed out on state proof {content_key}: indicating a bug is present");
&OfferTrace::Failed
}
};

// Update report and metrics
global_offer_report
.lock()
.expect("to acquire lock")
.update(offer_trace);
offer_report
.lock()
.expect("to acquire lock")
.update(&enr, offer_trace);

metrics.report_offer(
match content_key {
StateContentKey::AccountTrieNode(_) => "account_trie_node",
StateContentKey::ContractStorageTrieNode(_) => "contract_storage_trie_node",
StateContentKey::ContractBytecode(_) => "contract_bytecode",
},
match offer_trace {
OfferTrace::Success(_) => "success",
OfferTrace::Declined => "declined",
OfferTrace::Failed => "failed",
},
);

// Release permit
drop(permit);
metrics.stop_process_timer(timer);
});
Expand Down
14 changes: 14 additions & 0 deletions trin-metrics/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct BridgeMetrics {
pub process_timer: HistogramVec,
pub bridge_info: IntGaugeVec,
pub gossip_total: IntCounterVec,
pub offer_total: IntCounterVec,
pub current_block: IntGaugeVec,
}

Expand All @@ -41,6 +42,11 @@ impl BridgeMetrics {
&["bridge", "success", "type"],
registry
)?;
let offer_total = register_int_counter_vec_with_registry!(
opts!("bridge_offer_total", "counts all content offer requests"),
&["bridge", "type", "result"],
registry
)?;
let current_block = register_int_gauge_vec_with_registry!(
opts!(
"bridge_current_block",
Expand All @@ -53,6 +59,7 @@ impl BridgeMetrics {
process_timer,
bridge_info,
gossip_total,
offer_total,
current_block,
})
}
Expand Down Expand Up @@ -94,6 +101,13 @@ impl BridgeMetricsReporter {
.inc();
}

pub fn report_offer(&self, content_type: &str, status: &str) {
self.bridge_metrics
.offer_total
.with_label_values(&[&self.bridge, content_type, status])
.inc();
}

fn report_bridge_info(&self, bridge_mode: &str) {
let labels: [&str; 2] = [&self.bridge, bridge_mode];
self.bridge_metrics
Expand Down

0 comments on commit 3fc5130

Please sign in to comment.