Metrics and graceful shutdown
Quick answer
Metrics summarize service behavior, while graceful shutdown stops accepting new work and gives existing work a bounded chance to finish. Both make service operation more predictable.
Measure and drain
A counter accumulates events, a gauge measures current state, and a histogram groups observations into buckets. Avoid high-cardinality labels such as request IDs. On shutdown, mark readiness false, stop admission, signal workers, wait within a deadline and apply an explicit fallback. The second fixture is a single-threaded admission model, not an actual signal handler or cancellation implementation.
fn main() { let statuses = [200, 503, 200, 500]; let failures = statuses.iter().filter(|&&s| s >= 500).count(); println!("total={} failures={failures}", statuses.len());}Expected stdout (not executed):
total=4 failures=2Counts permit aggregate comparison without identifying users.
struct Gate { accepting: bool, active: usize }impl Gate { fn admit(&mut self) -> bool { if !self.accepting { return false; } self.active += 1; true }}fn main() { let mut g = Gate { accepting: true, active: 0 }; assert!(g.admit()); g.accepting = false; println!("{} {}", g.admit(), g.active);}Expected stdout (not executed):
false 1Existing work remains tracked after new requests are rejected.
Common mistake
Do not wait indefinitely for every task after receiving a shutdown request.
Try it yourself
Add a completion operation to Gate and test that shutdown drains to zero.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Metrics and graceful shutdown — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A service wants the current number of active jobs rather than total jobs ever started. Which metric suits it?
Metrics and graceful shutdown — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let statuses = [200, 503, 200, 500]; let failures = statuses.iter().filter(|&&s| s >= 500).count(); println!("total={} failures={failures}", statuses.len()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Metrics and graceful shutdown — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust struct Gate { accepting: bool, active: usize } impl Gate { fn admit(&mut self) -> bool { if !self.accepting { return false; } self.active += 1; true } } fn main() { let mut g = Gate { accepting: true, active: 0 }; assert!(g.admit()); g.accepting = false; println!("{} {}", g.admit(), g.active); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.