Skip to content

Commit 9e0ab62

Browse files
avi-starkwareclaude
andcommitted
starknet_transaction_prover: bump prover_panics_total on panic hook fire
Bumps the new `prover_panics_total` counter from the panic hook before constructing the backtrace, so dashboards can alert on panic rate without log search. The pre-registered zero observation in `install_exporter` keeps the series visible at scrape time even before the first panic. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9a9f080 commit 9e0ab62

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

crates/starknet_transaction_prover/src/server/metrics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub const METRICS_PATH: &str = "/metrics";
2727
pub mod names {
2828
/// Build identity. Value is always 1; labels carry version + git_sha.
2929
pub const BUILD_INFO: &str = "prover_build_info";
30+
/// Unhandled panics caught by the global panic hook.
31+
pub const PANICS_TOTAL: &str = "prover_panics_total";
3032
/// Wall-clock duration of `prove_transaction` end-to-end. Bucketed.
3133
pub const PROVE_TRANSACTION_DURATION_SECONDS: &str =
3234
"prover_prove_transaction_duration_seconds";
@@ -71,6 +73,8 @@ pub fn install_exporter(version: &str, git_sha: &str) -> anyhow::Result<Promethe
7173
"git_sha" => git_sha.to_string(),
7274
)
7375
.set(1.0);
76+
// Pre-register at zero so the series exists in scrapes before the first panic.
77+
metrics::counter!(names::PANICS_TOTAL).increment(0);
7478
super::http_metrics::preregister_http_metrics();
7579
Ok(handle)
7680
}

crates/starknet_transaction_prover/src/server/panic.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! Process-wide panic hook: one structured `tracing` event with location +
22
//! backtrace (indexable by log aggregators) instead of the default ad-hoc
3-
//! stderr output. Only logs — abort-on-panic behavior is preserved.
3+
//! stderr output, plus a `prover_panics_total` bump so alerts can fire on
4+
//! panic rate rather than log search. Only logs and counts — runtime
5+
//! abort-on-panic behavior is preserved.
46
57
use std::backtrace::Backtrace;
68
use std::panic::PanicHookInfo;
79

810
use tracing::error;
911

12+
use crate::server::metrics::names::PANICS_TOTAL;
13+
1014
#[cfg(test)]
1115
#[path = "panic_test.rs"]
1216
mod panic_test;
@@ -16,6 +20,9 @@ pub fn install_panic_hook() {
1620
}
1721

1822
fn panic_hook(info: &PanicHookInfo<'_>) {
23+
// Increment first — if `Backtrace::force_capture` or the `error!` macro
24+
// panic recursively, the counter still reflects the original panic.
25+
metrics::counter!(PANICS_TOTAL).increment(1);
1926
let message = extract_payload(info);
2027
let location = info
2128
.location()

crates/starknet_transaction_prover/src/server/panic_test.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::sync::{Arc, Mutex};
22

3-
use crate::server::panic::extract_payload;
3+
use crate::server::metrics::names::PANICS_TOTAL;
4+
use crate::server::panic::{extract_payload, install_panic_hook};
5+
use crate::server::test_recorder::{metric_value, shared_handle};
46

57
fn capture_payload<F: FnOnce() + std::panic::UnwindSafe>(f: F) -> String {
68
let captured: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
@@ -21,9 +23,23 @@ fn capture_payload<F: FnOnce() + std::panic::UnwindSafe>(f: F) -> String {
2123
payload
2224
}
2325

24-
// The panic hook is global state — a single `#[test]` keeps captures serial.
26+
// Panic-capturing tests mutate the process-global panic hook.
2527
#[test]
2628
fn extracts_static_str_and_formatted_payloads() {
2729
assert_eq!(capture_payload(|| panic!("static literal")), "static literal");
2830
assert_eq!(capture_payload(|| panic!("formatted {}", 42)), "formatted 42");
2931
}
32+
33+
#[test]
34+
fn panic_hook_bumps_panics_total_counter() {
35+
let handle = shared_handle();
36+
let before = metric_value(&handle.render(), PANICS_TOTAL);
37+
38+
let prev_hook = std::panic::take_hook();
39+
install_panic_hook();
40+
let _ = std::panic::catch_unwind(|| panic!("counter-test panic"));
41+
std::panic::set_hook(prev_hook);
42+
43+
let after = metric_value(&handle.render(), PANICS_TOTAL);
44+
assert_eq!(after - before, 1.0);
45+
}

0 commit comments

Comments
 (0)