Commit f87b6c9
fix(message): rate-limit "Could not parse" warnings per NAME (#1566)
* fix(message): rate-limit "Could not parse" warnings per NAME
When a firmware version pushes messages whose on-wire format the lib
does not yet support, every push triggers a `Could not parse <NAME>`
WARNING via `_handle_error_or_analyse`. In one observed case a mower's
spontaneous `getMapTrace` pushes produced 217 520 identical entries in
3 days (~70k/day, ~50/min during mowing), bloating the parent process'
log file and any downstream sink (HA recorder DB, Loki, etc.).
The first few warnings are useful — they let an operator notice that
something is wrong. Beyond that, identical repeats add no information
and only make the rest of the log harder to read.
This adds a per-NAME counter and a threshold (default 3): the first 3
parse failures for a given message NAME are still logged at WARNING,
followed by a single notice that further entries will be downgraded to
DEBUG. After that, all subsequent parse failures for that NAME log at
DEBUG. Counters are isolated per NAME, so a NEW message class hitting
a parse error still surfaces normally.
The 3 existing emission sites (`_handle_error_or_analyse` exception
path, `_handle_error_or_analyse` ERROR-state path, `MessageBodyData`
exception path) all route through the new helper. Behaviour for
non-throttled flows (success, ANALYSE) is unchanged.
Tests:
- `test_warn_once_throttles_repeated_parse_failures` — calls
`Message.handle()` `THRESHOLD + 2` times on a class whose `_handle`
always raises, asserts THRESHOLD + 1 WARNINGs (incl. the
"switching to DEBUG" notice) followed by 2 DEBUGs.
- `test_warn_once_isolated_per_message_name` — burns through the
threshold for one NAME, verifies a sibling NAME still WARNs on its
first parse failure.
- Full suite: 699/699 pass.
Refs: #1376
* review: drop verbose comments, full counter reset via fixture, exact log assertions
Address review feedback on #1566:
- Remove the file-level explanatory comment block above the threshold
- Drop the "Restart Home Assistant" line — the lib does not know its host
- Replace per-NAME counter cleanup with a pytest fixture that wipes the
whole dict before and after each test
- Compare ``caplog.records`` against the full expected level/message
list in order (the previous count-only assertion would have passed
with WARNING/DEBUG entries interleaved out of order)
* Refactor tests
---------
Co-authored-by: Robert Resch <robert@resch.dev>1 parent e91edcc commit f87b6c9
2 files changed
Lines changed: 145 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
28 | 45 | | |
29 | 46 | | |
30 | 47 | | |
| |||
65 | 82 | | |
66 | 83 | | |
67 | 84 | | |
68 | | - | |
| 85 | + | |
69 | 86 | | |
70 | 87 | | |
71 | 88 | | |
| |||
81 | 98 | | |
82 | 99 | | |
83 | 100 | | |
84 | | - | |
| 101 | + | |
85 | 102 | | |
86 | 103 | | |
87 | 104 | | |
| |||
256 | 273 | | |
257 | 274 | | |
258 | 275 | | |
259 | | - | |
| 276 | + | |
260 | 277 | | |
261 | 278 | | |
262 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
67 | 70 | | |
68 | 71 | | |
69 | 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 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
0 commit comments