1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Operational Prometheus-style metrics for the order book core.
//!
//! Issue #60 — feature-gated, additive observability hooks. When the
//! `metrics` feature is enabled the helpers in this module forward to
//! the `metrics` crate's global recorder; when the feature is off every
//! helper compiles down to a no-op so that call-sites in the matching
//! hot path stay unconditional and allocation-free.
//!
//! # Metrics surface
//!
//! - `orderbook_rejects_total{reason="…"}` — counter, incremented on
//! every rejection that flows through `record_reject`. The label
//! value is the [`RejectReason`] [`Display`] string (stable across
//! `0.7.x`).
//! - `orderbook_depth_levels_bid` / `orderbook_depth_levels_ask` —
//! gauges, updated on every book change to reflect the current count
//! of distinct price levels on each side.
//! - `orderbook_trades_total` — counter, incremented exactly once per
//! emitted trade transaction (a `MatchResult` may contain several).
//! - `orderbook_reserve_discards_total` /
//! `orderbook_reserve_hidden_discarded_total` — counters, incremented
//! whenever a reserve without automatic replenishment loses its hidden
//! tranche because its visible one was exhausted (#230). The first
//! counts orders, the second sums the hidden quantity that was dropped;
//! neither can be derived from the other. Both sides of the trade feed
//! them: the aggressive residual guard in `modifications.rs`
//! (`add_order_inner`) and the maker removal in `matching.rs`, which
//! `pricelevel` performs when a depleted non-replenishing maker leaves
//! its level. The matching `INFO` trace carries a `path` field
//! (`"taker"` / `"maker"`) so the two are distinguishable.
//!
//! # Determinism
//!
//! Metrics emission is **out-of-band**: it does not influence matching,
//! does not allocate on the happy path, and does not cross the
//! determinism boundary. `restore_from_snapshot_package` deliberately
//! does **not** rehydrate metric counters — they are operational only
//! and live for the process lifetime.
//!
//! [`RejectReason`]: crate::orderbook::reject_reason::RejectReason
//! [`Display`]: std::fmt::Display
use crateRejectReason;
/// Counter name: total order rejections, labelled by reject reason.
pub const REJECTS_TOTAL: &str = "orderbook_rejects_total";
/// Gauge name: current count of distinct bid price levels.
pub const DEPTH_LEVELS_BID: &str = "orderbook_depth_levels_bid";
/// Gauge name: current count of distinct ask price levels.
pub const DEPTH_LEVELS_ASK: &str = "orderbook_depth_levels_ask";
/// Counter name: monotonic count of every emitted trade transaction.
pub const TRADES_TOTAL: &str = "orderbook_trades_total";
/// Counter name: reserve residuals discarded for lack of automatic
/// replenishment (#230), counted in orders.
pub const RESERVE_DISCARDS_TOTAL: &str = "orderbook_reserve_discards_total";
/// Counter name: hidden quantity dropped by those discards (#230),
/// counted in quantity units.
pub const RESERVE_HIDDEN_DISCARDED_TOTAL: &str = "orderbook_reserve_hidden_discarded_total";
/// Record an order rejection.
///
/// Increments `orderbook_rejects_total` by 1 with the
/// `reason="<RejectReason::Display>"` label. Compiles to a no-op when
/// the `metrics` feature is disabled.
/// No-op when the `metrics` feature is disabled.
/// Update the bid / ask depth gauges to the supplied counts.
///
/// Called from book-change emission paths. Compiles to a no-op when
/// the `metrics` feature is disabled.
/// No-op when the `metrics` feature is disabled.
/// Record `n` newly emitted trade transactions.
///
/// Called once per `TradeListener` callback with the number of
/// transactions in the underlying `MatchResult`. Compiles to a no-op
/// when the `metrics` feature is disabled.
/// No-op when the `metrics` feature is disabled.
/// Record one discarded reserve residual and the hidden quantity it
/// dropped (#230).
///
/// Increments `orderbook_reserve_discards_total` by 1 and
/// `orderbook_reserve_hidden_discarded_total` by `quantity`. Called once
/// per dropped order from each of the two paths that can drop one, both
/// requiring `auto_replenish == false` and an exhausted visible tranche:
///
/// - the aggressive residual guard in `add_order_inner`
/// (`modifications.rs`), where the taker's residual is discarded instead
/// of rested;
/// - the maker removal in `match_order_inner` (`matching.rs`), where
/// `pricelevel` takes a resting maker off its level and strands the
/// hidden depth behind it.
///
/// A zero `quantity` is not a discard and is ignored. Compiles to a no-op
/// when the `metrics` feature is disabled.
/// No-op when the `metrics` feature is disabled.