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
//! The `<svc>.console_metrics` UDS method and the `by_*` breakdown convention
//! (#10906, owner ruling D95).
//!
//! Why: D95 has each service report its own counters over its own socket
//! (ADR-0032: UDS only), pulled by the reader, never pushed. Several services
//! answer, and one reader renders them all, so the method name and the shape
//! of a breakdown are fixed here once rather than per service.
//!
//! # The contract
//!
//! **Method.** A service whose socket serves metrics registers the unary
//! JSON-RPC method `<svc>.console_metrics`, where `<svc>` is the prefix its
//! other methods use: `events.console_metrics` on the trusty-events socket,
//! `console.console_metrics` on the console socket. It accepts any params,
//! including none, and answers a [`super::ConsoleMetricsReport`] as the plain
//! result object: not wrapped in the MCP `content[0].text` envelope that
//! [`super::serialise_report`] builds for the stdio `console_metrics` tool.
//!
//! **`metrics` payload.** The report's `metrics` is a JSON object.
//!
//! 1. A key `by_<dimension>` is a breakdown. `<dimension>` is a singular
//! snake_case noun (`by_source`, `by_project`, `by_route`).
//! 2. A breakdown's value is a JSON object, never an array. Its keys are that
//! dimension's values as strings; it is `{}` when nothing was counted, and
//! the key is never omitted.
//! 3. A breakdown's entries are either all counts (non-negative integers) or
//! all body objects. One breakdown never mixes the two.
//! 4. A body object may hold breakdowns of its own (`by_source.mpm.by_domain`),
//! under the same rules.
//! 5. Every other key (`uptime_secs`, a total, a named sub-object such as
//! `messages`) is a plain field. A plain object field may hold breakdowns.
//! 6. Breakdown keys come from a bounded set. A value outside it is counted
//! under a reserved key the service documents (for example
//! [`crate::control_bus::UNATTRIBUTED_PROJECT`]), so a caller cannot grow
//! the maps without limit.
//! 7. Adding a key is additive. Renaming or removing one, or changing a
//! breakdown's entry form, bumps
//! [`super::ConsoleMetricsReport::metrics_schema_version`].
//!
//! [`check_by_convention`] checks rules 1 to 5 mechanically, so each service
//! tests its payload against the same definition.
//!
//! Test: `super::by_convention_tests`.
use fmt;
use ;
/// The prefix that marks a `metrics` key as a breakdown.
pub const BY_PREFIX: &str = "by_";
/// The first place a `metrics` payload breaks the `by_*` convention.
///
/// What: `path` is the dotted key path to the offending value (`metrics` for
/// the root); `reason` says which rule failed.
/// Check a report's `metrics` against the `by_*` convention (module docs,
/// rules 1 to 5).
///
/// Why: a service and its readers agree on the shape by testing against one
/// checker, not by each restating the rules.
/// What: `metrics` must be an object. Every `by_<dimension>` key, at any depth
/// reached through objects, must name a dimension and hold an object whose
/// entries are all counts or all objects; object entries and plain object
/// fields are checked recursively.
///
/// # Errors
///
/// The first [`ByConventionViolation`] found, in key order.
///
/// Test: `super::by_convention_tests::a_conforming_payload_passes`,
/// `super::by_convention_tests::each_rule_violation_is_reported_with_its_path`.