Skip to content

Commit 3fc5130

Browse files
authored
feat: add offer metric to state bridge (#1533)
1 parent d18cc49 commit 3fc5130

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

portal-bridge/src/bridge/state.rs

+40-33
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ impl StateBridge {
317317
let global_offer_report = self.global_offer_report.clone();
318318
tokio::spawn(async move {
319319
let timer = metrics.start_process_timer("spawn_offer_state_proof");
320-
match timeout(
320+
321+
let result = timeout(
321322
SERVE_BLOCK_TIMEOUT,
322323
StateNetworkApiClient::trace_offer(
323324
&portal_client,
@@ -326,43 +327,49 @@ impl StateBridge {
326327
content_value.encode(),
327328
),
328329
)
329-
.await
330-
{
331-
Ok(result) => match result {
332-
Ok(result) => {
333-
global_offer_report
334-
.lock()
335-
.expect("to acquire lock")
336-
.update(&result);
337-
offer_report
338-
.lock()
339-
.expect("to acquire lock")
340-
.update(&enr, &result);
341-
}
342-
Err(e) => {
343-
global_offer_report
344-
.lock()
345-
.expect("to acquire lock")
346-
.update(&OfferTrace::Failed);
347-
offer_report
348-
.lock()
349-
.expect("to acquire lock")
350-
.update(&enr, &OfferTrace::Failed);
351-
warn!("Error offering to: {enr}, error: {e:?}");
330+
.await;
331+
332+
let offer_trace = match &result {
333+
Ok(Ok(result)) => {
334+
if matches!(result, &OfferTrace::Failed) {
335+
warn!("Internal error offering to: {enr}");
352336
}
353-
},
337+
result
338+
}
339+
Ok(Err(err)) => {
340+
warn!("Error offering to: {enr}, error: {err:?}");
341+
&OfferTrace::Failed
342+
}
354343
Err(_) => {
355-
global_offer_report
356-
.lock()
357-
.expect("to acquire lock")
358-
.update(&OfferTrace::Failed);
359-
offer_report
360-
.lock()
361-
.expect("to acquire lock")
362-
.update(&enr, &OfferTrace::Failed);
363344
error!("trace_offer timed out on state proof {content_key}: indicating a bug is present");
345+
&OfferTrace::Failed
364346
}
365347
};
348+
349+
// Update report and metrics
350+
global_offer_report
351+
.lock()
352+
.expect("to acquire lock")
353+
.update(offer_trace);
354+
offer_report
355+
.lock()
356+
.expect("to acquire lock")
357+
.update(&enr, offer_trace);
358+
359+
metrics.report_offer(
360+
match content_key {
361+
StateContentKey::AccountTrieNode(_) => "account_trie_node",
362+
StateContentKey::ContractStorageTrieNode(_) => "contract_storage_trie_node",
363+
StateContentKey::ContractBytecode(_) => "contract_bytecode",
364+
},
365+
match offer_trace {
366+
OfferTrace::Success(_) => "success",
367+
OfferTrace::Declined => "declined",
368+
OfferTrace::Failed => "failed",
369+
},
370+
);
371+
372+
// Release permit
366373
drop(permit);
367374
metrics.stop_process_timer(timer);
368375
});

trin-metrics/src/bridge.rs

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct BridgeMetrics {
1515
pub process_timer: HistogramVec,
1616
pub bridge_info: IntGaugeVec,
1717
pub gossip_total: IntCounterVec,
18+
pub offer_total: IntCounterVec,
1819
pub current_block: IntGaugeVec,
1920
}
2021

@@ -41,6 +42,11 @@ impl BridgeMetrics {
4142
&["bridge", "success", "type"],
4243
registry
4344
)?;
45+
let offer_total = register_int_counter_vec_with_registry!(
46+
opts!("bridge_offer_total", "counts all content offer requests"),
47+
&["bridge", "type", "result"],
48+
registry
49+
)?;
4450
let current_block = register_int_gauge_vec_with_registry!(
4551
opts!(
4652
"bridge_current_block",
@@ -53,6 +59,7 @@ impl BridgeMetrics {
5359
process_timer,
5460
bridge_info,
5561
gossip_total,
62+
offer_total,
5663
current_block,
5764
})
5865
}
@@ -94,6 +101,13 @@ impl BridgeMetricsReporter {
94101
.inc();
95102
}
96103

104+
pub fn report_offer(&self, content_type: &str, status: &str) {
105+
self.bridge_metrics
106+
.offer_total
107+
.with_label_values(&[&self.bridge, content_type, status])
108+
.inc();
109+
}
110+
97111
fn report_bridge_info(&self, bridge_mode: &str) {
98112
let labels: [&str; 2] = [&self.bridge, bridge_mode];
99113
self.bridge_metrics

0 commit comments

Comments
 (0)