streamcast-watch 0.33.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,4 @@
1
+ streamcast
2
+ Copyright 2026 nhobin219
3
+
4
+ This product includes software developed by nhobin219.
@@ -0,0 +1,422 @@
1
+ Metadata-Version: 2.4
2
+ Name: streamcast-watch
3
+ Version: 0.33.0
4
+ Summary: Alert on rules over streamcast streams: a subscriber that keeps a bounded cache in memory, never a second copy.
5
+ Author: Nico Bautista Hobin
6
+ Author-email: Nico Bautista Hobin <nico@penca.io>
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ License-File: NOTICE
10
+ Classifier: Framework :: AsyncIO
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Topic :: System :: Monitoring
13
+ Classifier: Typing :: Typed
14
+ Requires-Dist: streamcast-client==0.33.0
15
+ Requires-Dist: pyyaml>=6.0.1
16
+ Requires-Dist: boto3>=1.34.41 ; extra == 'aws'
17
+ Requires-Python: >=3.11, <3.15
18
+ Project-URL: Source, https://github.com/nhobin219/streamcast
19
+ Project-URL: Documentation, https://github.com/nhobin219/streamcast/blob/main/watch/README.md
20
+ Provides-Extra: aws
21
+ Description-Content-Type: text/markdown
22
+
23
+ # streamcast-watch
24
+
25
+ Alerts on rules over [streamcast](https://github.com/nhobin219/streamcast) streams.
26
+ It is a real-time subscriber: it replays a bounded stretch of each stream, follows
27
+ it live into a bounded in-memory cache, evaluates its rules every 15 seconds, and
28
+ sends one notice when a rule starts to hold and one when it clears. A rule it cannot
29
+ evaluate is said to be UNKNOWN, never taken as clear.
30
+
31
+ ```bash
32
+ pip install streamcast-watch==0.33.0
33
+ streamcast-watch alert --rules rules.yaml --broker ws://127.0.0.1:8765 --notifier log
34
+ ```
35
+
36
+ The PyPI project is **`streamcast-watch`**, first published with 0.33.0;
37
+ install `streamcast-watch[aws]==0.33.0` for SNS and CloudWatch. The release image is
38
+ `ghcr.io/nhobin219/streamcast-watch:0.33.0` (linux/amd64 and linux/arm64), with the
39
+ AWS extra installed. It runs `tini -- streamcast-watch` as `10001:10001`:
40
+
41
+ ```sh
42
+ docker run --rm ghcr.io/nhobin219/streamcast-watch:0.33.0 --help
43
+ ```
44
+
45
+ For deployment, pin the multi-arch manifest digest printed by the release's
46
+ **Publish versioned multi-architecture watch image → Merge release digests** step:
47
+ read `pushing sha256:… to ghcr.io/nhobin219/streamcast-watch:X.Y.Z` for the
48
+ index digest, not the `copying` lines. Cross-check the top-level `Digest:` from
49
+ `docker buildx imagetools inspect ghcr.io/nhobin219/streamcast-watch:0.33.0`,
50
+ and pin `ghcr.io/nhobin219/streamcast-watch:0.33.0@sha256:…`.
51
+ See [release preparation](../docs/RELEASE.md) before the first tag.
52
+
53
+ Its other half is a publisher: `streamcast-watch publish` reads, from inside a
54
+ Kubernetes pod, whether each workload in a targets file is serving and how often its
55
+ containers restarted, and writes that to a metrics stream as OTel metric rows, the
56
+ alerter's input like any other service's. They are two processes from one image
57
+ (`docker/watch.Dockerfile`): the publisher needs the Kubernetes API and no cloud, the
58
+ alerter needs somewhere to deliver and no Kubernetes API, and a dead publisher is then
59
+ just another producer gone quiet, which a `stale` rule sees.
60
+
61
+ **The stream is the only store.** The watch keeps no database and writes nothing to
62
+ disk. Its cache is memory only and holds no rows: each rule keeps its own reduction of
63
+ the rows it matches (one number for `stale`; per series, a ring of `period` buckets of
64
+ `min`, `max` and `last` for `threshold`), bounded in time by the ring and in size by
65
+ `max_series` and `max_buckets`, and rebuilt from the stream on every start. A cache,
66
+ never a second copy.
67
+
68
+ **Absence alerts.** A `stale` rule ages its series from the later of its newest row
69
+ and the moment the alerter started. A broker that is down, a follower that cannot
70
+ connect, a publisher that stopped, and an alerter restarted with nothing to replay
71
+ all read as stale once the rule's period has passed. None of them reads as empty and
72
+ fine. The expectation comes from the rules file, not from anything the stream said.
73
+
74
+ It depends only on `streamcast-client` (the subscriber, with no storage dependencies)
75
+ and PyYAML. The `aws` extra (`streamcast-watch[aws]`, boto3 1.34.41 or later) is for
76
+ the AWS notifier and liveness seams, which live in a module of their own.
77
+
78
+ ## Rules
79
+
80
+ One YAML file. Adding a watched series is a few lines here, and no code, whoever
81
+ publishes it: a rule may match any metric name, the watch's own or a service's:
82
+
83
+ ```yaml
84
+ rules:
85
+ - name: SortieWatchSilent
86
+ kind: stale
87
+ stream: sortie-metrics
88
+ after: 3m
89
+ match: {name: watch.heartbeat, service: streamcast-watch-publisher}
90
+ says: The watch's publisher has not published for 3 minutes, or the broker is out.
91
+
92
+ - name: SortieBrokerNotServing
93
+ kind: threshold
94
+ stream: sortie-metrics
95
+ match: {name: watch.target.serving, attributes: {watch.target: sortie-broker}}
96
+ reduce: min
97
+ below: 1
98
+ period: 1m
99
+ points: 3
100
+ of: 5
101
+
102
+ - name: SortieBrokerRestarting
103
+ kind: threshold
104
+ stream: sortie-metrics
105
+ match: {name: k8s.container.restart.count, attributes: {watch.target: sortie-broker}}
106
+ reduce: increase
107
+ above: 0
108
+ period: 5m
109
+ points: 2
110
+ of: 6
111
+ ```
112
+
113
+ Every rule has `name` (unique), `kind`, `stream`, and `match`. A match takes the metric
114
+ `name`, and optionally the row's `service` and `attributes` equalities. A rule may also
115
+ have `says`, the notice's text, and `keep_firing_for`, 5m by default. A `stale` rule's
116
+ `keep_firing_for` must be at least its `after`, so one longer than 5m sets it.
117
+
118
+ | Kind | Holds when |
119
+ |---|---|
120
+ | `stale` (`after`) | no matching row reached the broker in the last `after`, counting from the alerter's start until a row does. Until a row taken after the start arrives, its OK does not run a FIRING rule's clear timer (below) |
121
+ | `threshold` (`reduce`, `below` or `above`, `period`, `points`, `of`) | at least `points` of `of` `period`-long buckets breach. Buckets are aligned to `period` by when the value was observed (`time_unix_nano`), and each reduces to one number by `min`, `max` or `increase`. `increase` is per series and summed: the bucket's last value minus the series' last value in its newest earlier bucket (going back at most one bucket before the window), a fall counting as 0. A recreated pod is a new series by its `k8s.pod.uid`. A bucket with no row does not breach; absence is `stale`'s job. It reads OK only on complete evidence, and is otherwise UNKNOWN (below) |
122
+
123
+ Durations are `30s`, `3m`, `1h`. The file is checked as a whole at start. A rules file
124
+ with an unknown `kind`, a duplicate name, `points` above `of` or below 1, a duration
125
+ not above zero, a `stale` rule whose `keep_firing_for` is shorter than its `after`, a
126
+ key no kind reads, or a metric name that is not one makes `alert` exit with status 2.
127
+ The message names the rule and the field, or both fields, and never their values. A
128
+ `stale` rule reads OK for `after` past its newest row whether or not another arrives,
129
+ so a shorter clear delay would let one row, after an outage that resumes at once,
130
+ send an OK the next `after` contradicts.
131
+
132
+ **A metric name is checked by its grammar, never against a list**: OpenTelemetry's
133
+ instrument name syntax (`streamcast_watch.otel.METRIC_NAME`), an ASCII letter followed
134
+ by at most 254 letters, digits, `_`, `.`, `-` or `/`. `streamcast_watch.names` holds
135
+ the names this package publishes, as constants; it is not an allow-list, so a service
136
+ that publishes its own OTel metric rows needs rule entries only. A rule naming a metric
137
+ nobody publishes loads, and then its `stale` fires or its `threshold` never does:
138
+ which names a deployment publishes is the deployment's to check.
139
+
140
+ ### Three verdicts, and what the cache can know
141
+
142
+ A `threshold` is BREACH, OK or UNKNOWN at each evaluation. A `stale` rule is BREACH or
143
+ OK only: being unable to read is already its alarm.
144
+
145
+ **Completeness is tracked on the observation clock**, between two edges the window
146
+ derives from the broker's `streamcast_ts`, with `max_skew` (S, 15 s) counted twice,
147
+ once for a publisher's clock against the broker's and once for a broker clock step:
148
+
149
+ - `high` is the greatest `streamcast_ts` delivered since the seed, minus 2S. The
150
+ follower delivers every row in offset order without a gap, so a row not yet
151
+ delivered comes after the newest one delivered, so it was taken at most S before
152
+ that greatest stamp (a larger step back is a clock fault) and observed at most S
153
+ before it was taken: every row observed before `high` is here.
154
+ - `low` covers the rows below a seeded replay's first offset, which are never asked
155
+ for: until the replay's first row, nothing is complete; then everything observed
156
+ after that row's stamp plus 2S is. A replay from `EARLIEST` has no `low`.
157
+
158
+ **S is checked on every row, not assumed.** A row whose observation time is more than
159
+ S from its broker stamp, whose stamp is more than S ahead of this process's clock, or
160
+ whose stamp steps back more than S below the greatest one delivered, is a clock fault:
161
+ it is not counted, no bucket of the rules it matches (every rule on the stream, for a
162
+ step back) is complete for one ring span, and it is logged once per episode. Those
163
+ rules read OK on nothing for that span, and read UNKNOWN unless a bucket's held rows
164
+ already breach a subset-safe reduction (`min below`, `max above`): that breach still
165
+ fires during the fault, because fewer rows can only raise a minimum or lower a
166
+ maximum, and the faulty row, which is not counted, did not cause it. The one relation S
167
+ bounds that cannot be checked is a broker clock step among the rows below a seed,
168
+ which are never delivered.
169
+
170
+ **A bucket is complete** when it starts after `low`, ends by `high`, no series was
171
+ refused into it, it is still in the ring, and its rule has no live clock fault. Each
172
+ bucket is then:
173
+
174
+ - *breach*: it holds rows whose number breaches, and either the reduction is
175
+ subset-safe — `min` with `below`, `max` with `above`, where fewer rows can only
176
+ raise a minimum or lower a maximum — or the bucket is complete. `increase` also
177
+ needs every bucket from the baseline it read to this one complete;
178
+ - *ok*: it is complete and does not breach;
179
+ - *unknown*: otherwise.
180
+
181
+ The rule reads two windows of `of` buckets: the one ending with the bucket that holds
182
+ `now`, so a subset-safe rule fires as soon as its rows arrive, and the one ending
183
+ with the last full bucket at or before `min(now, high)`, which is fully settled. It
184
+ is **BREACH** when either has `points` breach buckets; **OK** when the settled one has
185
+ fewer than `points` breach and unknown buckets together and `now − high` is at most
186
+ `max_lag` (300 s); and **UNKNOWN** otherwise. UNKNOWN has four causes, each named in
187
+ its notice: the replay's seed, the series cap, a clock fault, and no row for longer
188
+ than `max_lag`. No other rule's traffic and no volume can cause one: each rule keeps
189
+ its own buckets, and nothing in them is evicted to make room.
190
+
191
+ What a reader sees: buckets are aligned to `period`, so a 3-of-5 one-minute rule
192
+ fires within one period of where a sliding window would; a subset-safe rule is as
193
+ fresh as its rows; and every other verdict settles 2S plus the publishing cadence
194
+ after its bucket ends.
195
+
196
+ A new kind is a `Rule` subclass registered under its `kind:` name:
197
+
198
+ ```python
199
+ from streamcast_watch import Finding, Rule, register
200
+
201
+ @register("absent_value")
202
+ class AbsentValue(Rule):
203
+ OPTIONS = frozenset({...}) # its own keys in a rules file
204
+ ... # evidence(settings), options(raw), holds(window, now)
205
+ ```
206
+
207
+ ## Notices
208
+
209
+ OK to FIRING sends one `[ALARM] <rule>`. FIRING to OK sends one `[OK] <rule>`, once
210
+ the rule has read OK for `keep_firing_for` without a break. UNKNOWN sends nothing,
211
+ keeps a FIRING rule firing, and restarts that clear timer: a rule that cannot be
212
+ evaluated is never taken to have cleared. Nothing is sent again while the state holds.
213
+ A `stale` rule's OK with no row since the alerter started restarts the clear timer
214
+ too: it says only that `after` has not passed since the start, so a rule restored
215
+ FIRING (below) stays firing through an outage that has not changed, until a row
216
+ arrives or `after` passes and it reads BREACH again, with no second ALARM.
217
+
218
+ After `unknown_after` (20, five minutes) UNKNOWN evaluations in a row, one
219
+ `[UNKNOWN] <rule>` names the causes: the replay's seed and until when, the series
220
+ cap, a clock fault and until when, or no row for how long. The next BREACH or OK
221
+ sends one `[EVALUABLE] <rule>`. They carry the rule's name, the causes, times and
222
+ counts, and series by their identifying attributes only, through the same notifier. A notice carries the rule's `says`, the values that decided it
223
+ (which intervals breached, which series rose, the newest row's time), and the
224
+ alerter's own view of the stream: connected or not and since when, and the last row's
225
+ time and offset. It carries metric names and numbers only, never a row's body. A series is named by its
226
+ metric and the attributes that say which target, Service, pod or container it belongs
227
+ to (`names.IDENTITY`); any other attribute belongs to the service that published the
228
+ metric and is never repeated. A rules file that cannot be built is refused naming the
229
+ rule and the field and the grammar it was checked against, never the value it held.
230
+
231
+ A notice that cannot be delivered stays pending, in order, and is retried with backoff.
232
+ It is never dropped. A rule's notices waiting together collapse, each pair on its own,
233
+ to at most four, so an ALARM, OK, ALARM sequence while delivery is down arrives as one
234
+ ALARM, and UNKNOWN, EVALUABLE, UNKNOWN as one UNKNOWN.
235
+
236
+ Delivery is the `Notifier` seam: `async send(notice)`, raising `NotifyFailed`.
237
+ `LogNotifier` writes notices to the log. An outside liveness check is the `Liveness`
238
+ seam: `async beat(now)`, called at most once a minute, after a tick in which
239
+ every rule was evaluated and while no notice has waited more than two minutes.
240
+ The beat reads no verdict or broker state. A rule's blindness is reported by its
241
+ `[UNKNOWN]` notice; if that notice cannot be delivered, the pending-notice bound
242
+ stops the beat too. `NoLiveness` does nothing. Implementations for a cloud live
243
+ in a module of their own (`aws`, below), so the rest of the code does not change
244
+ between clouds.
245
+
246
+ ## How it follows a stream
247
+
248
+ For each stream its rules name, a `Follower`:
249
+
250
+ 1. Reads the stream's frontier from a live-only subscribe (the greeting's
251
+ `end_offset`) and closes it.
252
+ 2. Subscribes at `max(EARLIEST, end_offset - replay_rows)` (5,000 offsets by default),
253
+ filtered on the broker to the metric names its rules read, with
254
+ `control={"replay_end"}`. That offset is its resume point from the moment it is
255
+ chosen, before the subscribe is answered. The broker's `ReplayEnd` mark says where
256
+ the replay stopped. It arrives after every replayed row, and after it no row below
257
+ its `end_offset` is unseen, so a resume never rescans what a filtered replay already
258
+ passed. A broker older than the mark is followed the same way: its replay is over
259
+ when a row at or past the greeting's `end_offset` arrives.
260
+ 3. Follows live. The resume point moves only when a row has been delivered, or when
261
+ the mark arrives. After a drop at any moment — before the greeting, before the
262
+ first row, mid-replay or live — it reconnects with backoff, 1 s doubling to 30 s,
263
+ at that point, so the cache sees each row once and skips none. It never probes the
264
+ frontier again after a drop: a new seed would start above rows the first would
265
+ have delivered. A refused resume (`ahead`: the stream was recreated; `too_old`,
266
+ `evicted`) starts the bounded replay again.
267
+
268
+ Each subscription accepted at a new seed starts a new epoch of the window's coverage,
269
+ so nothing is complete until the new seed's first row and its `low` have passed. A
270
+ resume after a drop continues the same gapless delivery and keeps the epoch. A stream
271
+ with no log has no offset to resume at, so every live-only subscription to it is a
272
+ new, seeded epoch.
273
+
274
+ A row that is not a metric data point (a broker or proxy not to trust) is dropped,
275
+ counted as delivered, and logged once by its offset. **No log line and no notice holds
276
+ a row or a frame**: a failure is named by its kind — the exception's type, a close
277
+ code, an `errno`, an HTTP status, a refusal's `why` — and never by an exception's text,
278
+ a close reason or a frame, any of which can quote what the peer sent.
279
+
280
+ It never uses `catch_up` and needs no object-storage credentials.
281
+
282
+ ## The Kubernetes publisher
283
+
284
+ `streamcast-watch publish --targets targets.yaml --broker ws://HOST:PORT --stream NAME`,
285
+ inside a pod. Every minute it lists each target's pods by label and each of its
286
+ Services' EndpointSlices, and publishes one batch to `<broker>/<stream>`:
287
+
288
+ ```yaml
289
+ targets:
290
+ - name: sortie-broker
291
+ namespace: sortie-system
292
+ pods: {app.kubernetes.io/name: streamcast, streamcast.io/broker: sortie-broker}
293
+ services: [sortie-broker, sortie-broker-pods]
294
+ ```
295
+
296
+ | Row (`name`) | Type | Attributes | Value |
297
+ |---|---|---|---|
298
+ | `watch.target.serving` | gauge | `watch.target`, `k8s.namespace.name` | 1 when a pod matched and every listed Service has a ready endpoint, else 0 |
299
+ | `k8s.service.endpoint.count` | gauge | `watch.target`, `k8s.service.name`, `k8s.service.endpoint.condition=ready` | the Service's ready endpoints, each pod once across its slices |
300
+ | `k8s.container.restart.count` | sum, cumulative, not monotonic | `watch.target`, `k8s.pod.name`, `k8s.pod.uid`, `k8s.container.name` | `restartCount`, from `initContainerStatuses` (native sidecars) and `containerStatuses` |
301
+ | `watch.heartbeat` | gauge | `service` = `streamcast-watch-publisher` | the targets read, **only when every target was read** |
302
+
303
+ Every row's `service` is `streamcast-watch-publisher`. A target is read whole or not
304
+ at all: one request that fails (an RBAC 403, a namespace that is gone) withholds that
305
+ target's rows and the heartbeat, so the `stale` rule on the heartbeat says the watch
306
+ is blind. A batch the broker does not take is dropped, never buffered, and the drop
307
+ is logged once while it fails the same way; a stream nobody declared (4404) is tried
308
+ again every iteration. Each iteration is cut at 45 s, so iterations never overlap.
309
+
310
+ A targets file is checked whole at start like a rules file: names, namespaces and
311
+ Services by Kubernetes' own grammar, label keys and values likewise, at least one
312
+ label (an empty selector lists every pod), unique names. An error names the target
313
+ and the field, never the value.
314
+
315
+ **What it may read.** It reads the API with the service account's mounted token,
316
+ read again for every request because Kubernetes rotates it, and the mounted CA, which
317
+ it requires: it never sends the token to a server it cannot verify. It only ever
318
+ sends `GET`, and only to collections (pods by label selector, EndpointSlices by
319
+ Service), never to one named object. Its Role in each target's namespace needs `list`
320
+ on `pods` and `list` on `discovery.k8s.io` `endpointslices`, and nothing else: no Secret, no
321
+ ClusterRole, no write verb. A failure is logged as an HTTP status or a failure's kind,
322
+ never a response body. It holds no cloud credential.
323
+
324
+ ## What the alerter announced
325
+
326
+ A restarted alerter has nothing on disk, so it keeps what it has told a person on the
327
+ stream. After an ALARM or OK is **delivered**, it publishes `watch.alert.announced`
328
+ (gauge, 1 for ALARM, 0 for OK, attribute `watch.rule`, `service`
329
+ `streamcast-watch-alerter`) to the rule's stream, and publishes every rule's state again
330
+ every minute. On start the bounded replay brings it back. A stream is restored when
331
+ its follower's replay ends, and only then: every rule last announced FIRING, and not
332
+ announced since by this process, becomes FIRING. So a rule that still holds sends no
333
+ second ALARM, one that cleared sends its OK, and one that cannot be evaluated stays
334
+ FIRING. UNKNOWN and EVALUABLE are not recorded.
335
+
336
+ If the replay has not ended `restore_within` (60 s) after the start, the stream's rules
337
+ are evaluated anyway, in fallback, so an unreachable broker is still alerted on. That
338
+ is not a restore: nothing is published until the replay ends, so a new process never
339
+ overwrites a 1 it has not read, and when the replay does end, however late, its state
340
+ is reconciled. A rule announced FIRING that now reads OK sends its OK after
341
+ `keep_firing_for`. An ALARM this process queued and has not delivered is withdrawn,
342
+ since the person already has one; an ALARM it already delivered in fallback is the one
343
+ duplicate, and nothing more is sent for it. A replay that never ends is never restored,
344
+ and nothing is ever published. The record follows the delivery, so a crash between the
345
+ two sends a notice twice, never loses one; with no replay at all, a rule that holds
346
+ sends its ALARM again. Delivery is at least once.
347
+
348
+ **One alerter per stream at a time is a prerequisite**: one replica, replaced with
349
+ `strategy: Recreate`. Nothing coordinates two: each would restore before the other
350
+ announced, both would notify, and each would publish its own state over the other's.
351
+
352
+ ## The AWS seam
353
+
354
+ `streamcast_watch.aws`, behind the `aws` extra, is the whole of the AWS surface and the
355
+ only module that imports boto3. Credentials are boto3's default chain (Pod Identity on
356
+ EKS); nothing reads or holds a key.
357
+
358
+ - **`SnsNotifier`** (`--notifier sns --topic-arn ARN`) publishes each notice to an
359
+ existing topic: subject `[ALARM] <rule>` (printable ASCII, under 100 characters), the
360
+ body as the message. It needs `sns:Publish` on that topic. The region is the ARN's.
361
+ - **`CloudWatchLiveness`** (`--liveness cloudwatch`) puts `Alive = 1` in namespace
362
+ `Watch/Alerter`, dimension `Alerter = sortie`, once a minute. It needs
363
+ `cloudwatch:PutMetricData`. The alerter beats only after an evaluation in which every
364
+ rule was evaluated and while no notice is overdue. An outside alarm watching
365
+ the metric's `SampleCount` says "not evaluating every rule, or not delivering". It never
366
+ depends on the broker's connection. The region is the topic's when one is given.
367
+
368
+ A refusal leaves the seam as the error's class and HTTP status, never AWS's message,
369
+ which can quote the topic's ARN; no log line holds the ARN. Every call is bounded:
370
+ 5 s to connect, 10 s to read, three attempts.
371
+
372
+ ## Commands, environment and the image
373
+
374
+ ```
375
+ streamcast-watch alert --rules PATH --broker URI [--notifier log|sns] [--topic-arn ARN]
376
+ [--liveness none|cloudwatch] [--health-port 8080]
377
+ streamcast-watch publish --targets PATH --broker URI --stream NAME [--health-port 8080]
378
+ streamcast-watch notify-test [--notifier log|sns] [--topic-arn ARN]
379
+ streamcast-watch spec --stream NAME
380
+ ```
381
+
382
+ `--notifier`, `--topic-arn` and `--liveness` default to `STREAMCAST_WATCH_NOTIFIER`,
383
+ `STREAMCAST_WATCH_TOPIC_ARN` and `STREAMCAST_WATCH_LIVENESS`, so a pod that sets them
384
+ can run `kubectl exec … -- streamcast-watch notify-test` with no arguments and test the
385
+ very path its alerter delivers through. `notify-test` sends one `[TEST]` notice and
386
+ exits 0 once it is delivered, 1 if not. `alert` and `publish` serve `GET /healthz` on
387
+ `--health-port` (0 for none, at most 65535): 200 while the evaluation loop ticked
388
+ within 45 s, or an iteration finished within 150 s; neither asks the broker. Every
389
+ command checks its arguments and files before it connects to anything, and no error
390
+ repeats a refused value, `--health-port`'s included.
391
+
392
+ `docker/watch.Dockerfile` builds one image with both commands and the `aws` extra, and
393
+ without the broker: Python 3.12, user `10001:10001`, entry point `streamcast-watch`,
394
+ nothing written at run time, so it runs with a read-only root filesystem. Its Python
395
+ and uv images are pinned by the digest of their multi-platform index, with the tag
396
+ each was read from in a comment beside it.
397
+
398
+ ## The metrics schema
399
+
400
+ `streamcast_watch.otel.METRICS_SCHEMA` is OpenTelemetry's metric data point as a row,
401
+ one row per data point. It is the schema's one home. `examples/otel/` publishes and
402
+ re-exports through it, and `streamcast-watch spec --stream NAME` prints the
403
+ declaration a broker's control API takes:
404
+
405
+ ```bash
406
+ streamcast-watch spec --stream sortie-metrics
407
+ ```
408
+
409
+ ## Bounds
410
+
411
+ | What | Time bound | Size bound |
412
+ |---|---|---|
413
+ | A `stale` rule's evidence (memory) | — | one number |
414
+ | A `threshold` rule's evidence (memory) | one ring per series: `of + 3 + ⌈max_lag / period⌉ + ⌈2 max_skew / period⌉` buckets by `time_unix_nano` (14 and 11 for the approved rules), pruned by bucket index whatever order rows arrive in; a row whose bucket is already below the ring is dropped as it arrives | `max_series` (64) series, each at most `max_buckets` (256) buckets of `min`, `max` and `last`: 4 MiB at most per rule. A new series past the cap is refused, logged once, and its bucket cannot be decided; a series retires when its buckets leave the ring. A rule whose ring needs more than `max_buckets` is refused at load |
415
+ | A stream's coverage (memory) | per epoch | four numbers |
416
+ | A clock fault | one ring span | — |
417
+ | Rows no rule matches, rows without `streamcast_ts`, clock-fault rows (from `threshold` evidence), rows that are not metric data points | dropped as they arrive | — |
418
+ | A replay | — | `replay_rows` (5,000) offsets, and never more than the broker's `max_replay` |
419
+ | Pending notices (memory) | until delivered | four per rule |
420
+ | Announced state (memory) | — | one number per rule, replayed and kept; rows for rules not in the file are dropped |
421
+ | The publisher (memory) | one iteration | one batch; nothing kept between iterations |
422
+ | A Kubernetes response | 10 s | 8 MiB, refused unread past it |