@smthrs/notifications 0.0.0-stage → 1.0.0-rc.4
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.
- package/CHANGELOG.md +127 -0
- package/LICENSE +21 -0
- package/README.md +91 -2
- package/dist/cjs/AlertPolicy.d.ts +284 -0
- package/dist/cjs/AlertPolicy.d.ts.map +1 -0
- package/dist/cjs/AlertPolicy.js +124 -0
- package/dist/cjs/AlertPolicy.js.map +7 -0
- package/dist/cjs/AlertRuntime.d.ts +90 -0
- package/dist/cjs/AlertRuntime.d.ts.map +1 -0
- package/dist/cjs/AlertRuntime.js +166 -0
- package/dist/cjs/AlertRuntime.js.map +7 -0
- package/dist/cjs/AlertSink.d.ts +101 -0
- package/dist/cjs/AlertSink.d.ts.map +1 -0
- package/dist/cjs/AlertSink.js +44 -0
- package/dist/cjs/AlertSink.js.map +7 -0
- package/dist/cjs/Alerts.d.ts +57 -0
- package/dist/cjs/Alerts.d.ts.map +1 -0
- package/dist/cjs/Alerts.js +46 -0
- package/dist/cjs/Alerts.js.map +7 -0
- package/dist/cjs/Notification.d.ts +214 -0
- package/dist/cjs/Notification.d.ts.map +1 -0
- package/dist/cjs/Notification.js +62 -0
- package/dist/cjs/Notification.js.map +7 -0
- package/dist/cjs/NotificationEvent.d.ts +214 -0
- package/dist/cjs/NotificationEvent.d.ts.map +1 -0
- package/dist/cjs/NotificationEvent.js +84 -0
- package/dist/cjs/NotificationEvent.js.map +7 -0
- package/dist/cjs/NotificationQueue.d.ts +193 -0
- package/dist/cjs/NotificationQueue.d.ts.map +1 -0
- package/dist/cjs/NotificationQueue.js +478 -0
- package/dist/cjs/NotificationQueue.js.map +7 -0
- package/dist/cjs/NotificationState.d.ts +169 -0
- package/dist/cjs/NotificationState.d.ts.map +1 -0
- package/dist/cjs/NotificationState.js +110 -0
- package/dist/cjs/NotificationState.js.map +7 -0
- package/dist/cjs/Projection.d.ts +27 -0
- package/dist/cjs/Projection.d.ts.map +1 -0
- package/dist/cjs/Projection.js +54 -0
- package/dist/cjs/Projection.js.map +7 -0
- package/dist/cjs/SteerPayload.d.ts +177 -0
- package/dist/cjs/SteerPayload.d.ts.map +1 -0
- package/dist/cjs/SteerPayload.js +61 -0
- package/dist/cjs/SteerPayload.js.map +7 -0
- package/dist/cjs/index.d.ts +41 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +47 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/foldCache.d.ts +36 -0
- package/dist/cjs/internal/foldCache.d.ts.map +1 -0
- package/dist/cjs/internal/foldCache.js +54 -0
- package/dist/cjs/internal/foldCache.js.map +7 -0
- package/dist/cjs/layerWebhook.d.ts +53 -0
- package/dist/cjs/layerWebhook.d.ts.map +1 -0
- package/dist/cjs/layerWebhook.js +107 -0
- package/dist/cjs/layerWebhook.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/AlertPolicy.d.ts +284 -0
- package/dist/esm/AlertPolicy.d.ts.map +1 -0
- package/dist/esm/AlertPolicy.js +284 -0
- package/dist/esm/AlertPolicy.js.map +1 -0
- package/dist/esm/AlertRuntime.d.ts +90 -0
- package/dist/esm/AlertRuntime.d.ts.map +1 -0
- package/dist/esm/AlertRuntime.js +191 -0
- package/dist/esm/AlertRuntime.js.map +1 -0
- package/dist/esm/AlertSink.d.ts +101 -0
- package/dist/esm/AlertSink.d.ts.map +1 -0
- package/dist/esm/AlertSink.js +63 -0
- package/dist/esm/AlertSink.js.map +1 -0
- package/dist/esm/Alerts.d.ts +57 -0
- package/dist/esm/Alerts.d.ts.map +1 -0
- package/dist/esm/Alerts.js +57 -0
- package/dist/esm/Alerts.js.map +1 -0
- package/dist/esm/Notification.d.ts +214 -0
- package/dist/esm/Notification.d.ts.map +1 -0
- package/dist/esm/Notification.js +121 -0
- package/dist/esm/Notification.js.map +1 -0
- package/dist/esm/NotificationEvent.d.ts +214 -0
- package/dist/esm/NotificationEvent.d.ts.map +1 -0
- package/dist/esm/NotificationEvent.js +158 -0
- package/dist/esm/NotificationEvent.js.map +1 -0
- package/dist/esm/NotificationQueue.d.ts +193 -0
- package/dist/esm/NotificationQueue.d.ts.map +1 -0
- package/dist/esm/NotificationQueue.js +626 -0
- package/dist/esm/NotificationQueue.js.map +1 -0
- package/dist/esm/NotificationState.d.ts +169 -0
- package/dist/esm/NotificationState.d.ts.map +1 -0
- package/dist/esm/NotificationState.js +191 -0
- package/dist/esm/NotificationState.js.map +1 -0
- package/dist/esm/Projection.d.ts +27 -0
- package/dist/esm/Projection.d.ts.map +1 -0
- package/dist/esm/Projection.js +43 -0
- package/dist/esm/Projection.js.map +1 -0
- package/dist/esm/SteerPayload.d.ts +177 -0
- package/dist/esm/SteerPayload.d.ts.map +1 -0
- package/dist/esm/SteerPayload.js +139 -0
- package/dist/esm/SteerPayload.js.map +1 -0
- package/dist/esm/index.d.ts +41 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +41 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/foldCache.d.ts +36 -0
- package/dist/esm/internal/foldCache.d.ts.map +1 -0
- package/dist/esm/internal/foldCache.js +58 -0
- package/dist/esm/internal/foldCache.js.map +1 -0
- package/dist/esm/layerWebhook.d.ts +53 -0
- package/dist/esm/layerWebhook.d.ts.map +1 -0
- package/dist/esm/layerWebhook.js +118 -0
- package/dist/esm/layerWebhook.js.map +1 -0
- package/docs/README.md +144 -0
- package/docs/api.md +439 -0
- package/docs/concepts/admission-and-promotion.md +144 -0
- package/docs/concepts/alerting.md +138 -0
- package/docs/concepts/journal-records.md +132 -0
- package/docs/guides/alert-on-a-stuck-run.md +140 -0
- package/docs/guides/drain-at-a-turn-boundary.md +95 -0
- package/docs/guides/handle-a-full-queue.md +102 -0
- package/docs/guides/report-pending-notifications.md +92 -0
- package/docs/guides/send-alerts-to-a-webhook.md +130 -0
- package/docs/guides/steer-a-run.md +127 -0
- package/docs/guides/testing.md +161 -0
- package/docs/installation.md +119 -0
- package/docs/quickstart.md +159 -0
- package/docs/troubleshooting.md +254 -0
- package/package.json +182 -3
- package/src/AlertPolicy.ts +377 -0
- package/src/AlertRuntime.ts +280 -0
- package/src/AlertSink.ts +105 -0
- package/src/Alerts.ts +71 -0
- package/src/Notification.ts +171 -0
- package/src/NotificationEvent.ts +213 -0
- package/src/NotificationQueue.ts +857 -0
- package/src/NotificationState.ts +295 -0
- package/src/Projection.ts +46 -0
- package/src/SteerPayload.ts +198 -0
- package/src/index.ts +47 -0
- package/src/internal/foldCache.ts +72 -0
- package/src/layerWebhook.ts +161 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/layerWebhook.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * The webhook alert sink: one POST per alert, keyed so the receiver can\n * dedupe on it.\n *\n * The transport rules live here rather than beside the policy, because which\n * endpoint is paged, over which scheme, carrying which credential is a\n * deployment's decision and the delays are not.\n *\n * @since 1.0.0\n */\n\nimport { Duration, Effect, Layer, Option } from \"effect\"\nimport * as FetchHttpClient from \"effect/unstable/http/FetchHttpClient\"\nimport * as HttpClient from \"effect/unstable/http/HttpClient\"\nimport * as HttpClientRequest from \"effect/unstable/http/HttpClientRequest\"\nimport { alertId } from \"./AlertPolicy.ts\"\nimport { AlertError, Sink } from \"./AlertSink.ts\"\n\n/**\n * How long the webhook sink waits for an answer before it calls the page\n * refused.\n *\n * @category constants\n * @since 1.0.0\n */\nexport const defaultWebhookTimeout: Duration.Duration = Duration.seconds(10)\n\n/**\n * The one check the webhook endpoint gets before an alert is ever built on\n * it.\n *\n * The url is operator config rather than runtime input, so there is no SSRF\n * vector to close here; the scheme bound is what keeps a credential the\n * deployment set in `headers` from being handed to a scheme that is not HTTP\n * at all. A `file:` or `javascript:` url is a config bug, and it fails the\n * composition the way an impossible policy rule does. The error names no\n * url: the value can carry basic-auth credentials, and an error is logged.\n */\nconst webhookUrl = (raw: string): Effect.Effect<string, AlertError> =>\n Effect.suspend(() => {\n const misconfigured = new AlertError({\n code: \"sink_misconfigured\",\n message: \"Alert webhook url must be an absolute http: or https: URL\"\n })\n let parsed: URL\n try {\n parsed = new URL(raw)\n } catch {\n return Effect.fail(misconfigured)\n }\n return parsed.protocol === \"http:\" || parsed.protocol === \"https:\"\n ? Effect.succeed(raw)\n : Effect.fail(misconfigured)\n })\n\n/**\n * Runs a webhook request with redirect following switched off at the fetch\n * transport.\n *\n * The request carries whatever credential the deployment put in `headers`,\n * and a transport that follows a 3xx re-sends those headers to an origin the\n * deployment never named. Every other fetch default the composition set is\n * kept; only the redirect mode is forced. A 3xx therefore surfaces to the\n * status rule, which calls it `sink_rejected`: a redirecting pager endpoint\n * is a misconfiguration to fix, not an address to forward credentials to.\n */\nconst followingNoRedirects = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Effect.flatMap(\n Effect.serviceOption(FetchHttpClient.RequestInit),\n (init) =>\n Effect.provideService(effect, FetchHttpClient.RequestInit, {\n ...(Option.isSome(init) ? init.value : {}),\n redirect: \"manual\"\n })\n )\n\n/**\n * A sink that POSTs each alert to one webhook.\n *\n * The body is the alert plus its {@link alertId}, and the same id is sent as\n * an `Idempotency-Key` header, because the package requires the receiver to\n * dedupe on it and a body without it cannot. The header is set after the\n * caller's headers, so it is the one this sink sends.\n *\n * The endpoint must be an absolute `http:` or `https:` URL, checked when the\n * layer is built, and the request is made with redirect following disabled:\n * the caller's headers can carry a credential, and a followed redirect would\n * re-send them to an origin the deployment never named. A 3xx answer is a\n * refusal like any other non-2xx, not a forwarding instruction. The sink owns\n * its Fetch transport; an externally supplied `HttpClient` is not used.\n * Each delivery has its own scope, which aborts the request and releases any\n * unread response body on completion, failure, timeout, or interruption.\n *\n * A non-2xx response is a failure, not a delivery, and an endpoint that never\n * answers is a failure after `timeout`. Paging is exactly the case where a\n * silently dropped request is worse than a retry, and a hung request is worse\n * than either: it is indistinguishable from silence, so it must not be\n * possible to wait on one forever.\n *\n * @param options the endpoint, any headers it needs, and how long to wait\n * @category layers\n * @since 0.1.0\n */\nexport const layerWebhook = (\n options: {\n readonly url: string\n readonly headers?: Readonly<Record<string, string>> | undefined\n readonly timeout?: Duration.Duration | undefined\n }\n): Layer.Layer<Sink> =>\n Layer.effect(\n Sink,\n Effect.gen(function*() {\n const url = yield* Effect.orDie(webhookUrl(options.url))\n const client = HttpClient.withScope(yield* HttpClient.HttpClient)\n const timeout = options.timeout ?? defaultWebhookTimeout\n return {\n deliver: (alert) =>\n client.execute(\n HttpClientRequest.post(url).pipe(\n (request) =>\n options.headers === undefined ? request : HttpClientRequest.setHeaders(request, options.headers),\n HttpClientRequest.setHeader(\"Idempotency-Key\", alertId(alert)),\n HttpClientRequest.bodyJsonUnsafe({ ...alert, alertId: alertId(alert) })\n )\n ).pipe(\n followingNoRedirects,\n Effect.flatMap((response) =>\n response.status >= 200 && response.status < 300\n ? Effect.void\n : Effect.fail(\n new AlertError({\n code: \"sink_rejected\",\n status: response.status,\n message: `Alert webhook answered ${response.status}`\n })\n )\n ),\n Effect.timeout(timeout),\n Effect.scoped,\n Effect.catchTag(\"TimeoutError\", () =>\n Effect.fail(\n new AlertError({\n code: \"sink_timeout\",\n message: `Alert webhook did not answer within ${Duration.toMillis(timeout)} ms`\n })\n )),\n // The transport error holds the request, and the request holds the\n // caller's headers. Only its reason tag crosses into the failure.\n Effect.catchTag(\"HttpClientError\", (cause) =>\n Effect.fail(\n new AlertError({\n code: \"sink_unreachable\",\n reason: cause.reason._tag,\n message: \"Alert webhook could not be reached\"\n })\n ))\n )\n }\n })\n ).pipe(Layer.provide(FetchHttpClient.layer))\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,oBAAgD;AAChD,sBAAiC;AACjC,iBAA4B;AAC5B,wBAAmC;AACnC,yBAAwB;AACxB,uBAAiC;AAS1B,MAAM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AAS,UAAU,OAAO,SAAS,SAAS,MACxC,qBAAO,OACP,qBAAO;AAAA,YACP,IAAI,4BAAW;AAAA,cACb,MAAM;AAAA,cACN,QAAQ,SAAS;AAAA,cACjB,SAAS,0BAA0B,SAAS,MAAM;AAAA,YACpD,CAAC;AAAA,UACH;AAAA,QACJ;AAAA,QACA,qBAAO,QAAQ,OAAO;AAAA,QACtB,qBAAO;AAAA,QACP,qBAAO,SAAS,gBAAgB,MAC9B,qBAAO;AAAA,UACL,IAAI,4BAAW;AAAA,YACb,MAAM;AAAA,YACN,SAAS,uCAAuC,uBAAS,SAAS,OAAO,CAAC;AAAA,UAC5E,CAAC;AAAA,QACH,CAAC;AAAA;AAAA;AAAA,QAGH,qBAAO,SAAS,mBAAmB,CAAC,UAClC,qBAAO;AAAA,UACL,IAAI,4BAAW;AAAA,YACb,MAAM;AAAA,YACN,QAAQ,MAAM,OAAO;AAAA,YACrB,SAAS;AAAA,UACX,CAAC;AAAA,QACH,CAAC;AAAA,MACL;AAAA,IACJ;AAAA,EACF,CAAC;AACH,EAAE,KAAK,oBAAM,QAAQ,gBAAgB,KAAK,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alert policy: what a condition is, when a journal leaves one open, and
|
|
3
|
+
* which open conditions have outlived their delay.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is pure and a function of journal time, so the same entries
|
|
6
|
+
* decide the same alerts in any process at any instant past the delay. The
|
|
7
|
+
* transport that pages about them is `./layerWebhook.ts` and the journal-backed
|
|
8
|
+
* tick that drives them is `./AlertRuntime.ts`; `./Alerts.ts` is the module a
|
|
9
|
+
* consumer imports.
|
|
10
|
+
*
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
import type { JournalEvent } from "@smthrs/journal";
|
|
14
|
+
import { Schema } from "effect";
|
|
15
|
+
/**
|
|
16
|
+
* The journal event type one delivered alert is recorded under.
|
|
17
|
+
*
|
|
18
|
+
* @category constants
|
|
19
|
+
* @since 0.1.0
|
|
20
|
+
*/
|
|
21
|
+
export declare const deliveredEventType = "flows.alerts.delivered";
|
|
22
|
+
/**
|
|
23
|
+
* The journal event type one failed delivery attempt is recorded under.
|
|
24
|
+
*
|
|
25
|
+
* One entry is written per alert per failure code, never one per tick: a
|
|
26
|
+
* webhook that stays down for an hour leaves one record, not a hundred and
|
|
27
|
+
* twenty that every later tick has to read past.
|
|
28
|
+
*
|
|
29
|
+
* @category constants
|
|
30
|
+
* @since 0.1.0
|
|
31
|
+
*/
|
|
32
|
+
export declare const failedEventType = "flows.alerts.failed";
|
|
33
|
+
/**
|
|
34
|
+
* How loud an alert is.
|
|
35
|
+
*
|
|
36
|
+
* @category models
|
|
37
|
+
* @since 0.1.0
|
|
38
|
+
*/
|
|
39
|
+
export declare const Severity: Schema.Literals<readonly ["info", "warning", "critical"]>;
|
|
40
|
+
/**
|
|
41
|
+
* How loud an alert is.
|
|
42
|
+
*
|
|
43
|
+
* @category models
|
|
44
|
+
* @since 0.1.0
|
|
45
|
+
*/
|
|
46
|
+
export type Severity = typeof Severity.Type;
|
|
47
|
+
/**
|
|
48
|
+
* How a condition is recognized in the journal.
|
|
49
|
+
*
|
|
50
|
+
* `field` names a payload key, `value` the value that means the condition
|
|
51
|
+
* holds, and `eventTypes` narrows which entries are consulted at all. An
|
|
52
|
+
* `eventTypes` item ending in `.*` names a family: `control.*` consults
|
|
53
|
+
* `control.run.parked` and `control.monitor.beat` alike. An entry that carries
|
|
54
|
+
* the field with a different value CLOSES the condition, which is what makes a
|
|
55
|
+
* resume clear an approval alert without a second vocabulary for "cleared".
|
|
56
|
+
*
|
|
57
|
+
* @category models
|
|
58
|
+
* @since 0.1.0
|
|
59
|
+
*/
|
|
60
|
+
export declare const Detector: Schema.Struct<{
|
|
61
|
+
readonly field: Schema.NonEmptyString;
|
|
62
|
+
readonly value: Schema.NonEmptyString;
|
|
63
|
+
readonly eventTypes: Schema.optional<Schema.$Array<Schema.NonEmptyString>>;
|
|
64
|
+
}>;
|
|
65
|
+
/**
|
|
66
|
+
* How a condition is recognized in the journal.
|
|
67
|
+
*
|
|
68
|
+
* @category models
|
|
69
|
+
* @since 0.1.0
|
|
70
|
+
*/
|
|
71
|
+
export type Detector = typeof Detector.Type;
|
|
72
|
+
/**
|
|
73
|
+
* What to do about one condition, and after how long.
|
|
74
|
+
*
|
|
75
|
+
* `afterMs` is a whole, non-negative number of milliseconds. The bound is the
|
|
76
|
+
* schema's job because the delay is also arithmetic on journal time: a `NaN`
|
|
77
|
+
* delay fires on the first tick and stamps a `firedAt` that JSON writes as
|
|
78
|
+
* `null`, and a negative one fires with a `firedAt` earlier than the condition
|
|
79
|
+
* it describes.
|
|
80
|
+
*
|
|
81
|
+
* @category models
|
|
82
|
+
* @since 0.1.0
|
|
83
|
+
*/
|
|
84
|
+
export declare const Rule: Schema.Struct<{
|
|
85
|
+
readonly afterMs: Schema.Int;
|
|
86
|
+
readonly severity: Schema.optional<Schema.Literals<readonly ["info", "warning", "critical"]>>;
|
|
87
|
+
readonly owner: Schema.optional<Schema.String>;
|
|
88
|
+
readonly runbook: Schema.optional<Schema.String>;
|
|
89
|
+
}>;
|
|
90
|
+
/**
|
|
91
|
+
* What to do about one condition, and after how long.
|
|
92
|
+
*
|
|
93
|
+
* @category models
|
|
94
|
+
* @since 0.1.0
|
|
95
|
+
*/
|
|
96
|
+
export type Rule = typeof Rule.Type;
|
|
97
|
+
/**
|
|
98
|
+
* The conditions a policy alerts on and the delays it alerts after.
|
|
99
|
+
*
|
|
100
|
+
* `defaults` fills in what a rule leaves out, so a policy states the delay per
|
|
101
|
+
* condition and the ownership once.
|
|
102
|
+
*
|
|
103
|
+
* @category models
|
|
104
|
+
* @since 0.1.0
|
|
105
|
+
*/
|
|
106
|
+
export declare const Policy: Schema.Struct<{
|
|
107
|
+
readonly defaults: Schema.optional<Schema.Struct<{
|
|
108
|
+
readonly severity: Schema.optional<Schema.Literals<readonly ["info", "warning", "critical"]>>;
|
|
109
|
+
readonly owner: Schema.optional<Schema.String>;
|
|
110
|
+
readonly runbook: Schema.optional<Schema.String>;
|
|
111
|
+
}>>;
|
|
112
|
+
readonly rules: Schema.$Record<Schema.String, Schema.Struct<{
|
|
113
|
+
readonly afterMs: Schema.Int;
|
|
114
|
+
readonly severity: Schema.optional<Schema.Literals<readonly ["info", "warning", "critical"]>>;
|
|
115
|
+
readonly owner: Schema.optional<Schema.String>;
|
|
116
|
+
readonly runbook: Schema.optional<Schema.String>;
|
|
117
|
+
}>>;
|
|
118
|
+
/** Detectors for conditions this deployment names itself. */
|
|
119
|
+
readonly detectors: Schema.optional<Schema.$Record<Schema.String, Schema.Struct<{
|
|
120
|
+
readonly field: Schema.NonEmptyString;
|
|
121
|
+
readonly value: Schema.NonEmptyString;
|
|
122
|
+
readonly eventTypes: Schema.optional<Schema.$Array<Schema.NonEmptyString>>;
|
|
123
|
+
}>>>;
|
|
124
|
+
}>;
|
|
125
|
+
/**
|
|
126
|
+
* The conditions a policy alerts on and the delays it alerts after.
|
|
127
|
+
*
|
|
128
|
+
* @category models
|
|
129
|
+
* @since 0.1.0
|
|
130
|
+
*/
|
|
131
|
+
export type Policy = typeof Policy.Type;
|
|
132
|
+
/**
|
|
133
|
+
* The four conditions a control plane journals out of the box.
|
|
134
|
+
*
|
|
135
|
+
* `status` rides on every `control.run.*` entry, and `health` on every
|
|
136
|
+
* `control.monitor.beat`. A deployment whose supervisor journals a park reason
|
|
137
|
+
* gets `quota-parked` from the same field the run summary reports it under.
|
|
138
|
+
*
|
|
139
|
+
* Each consults the `control.*` family only. A run's journal is shared with
|
|
140
|
+
* flow steps, agent traces, and anything else that writes to the run, and a
|
|
141
|
+
* `status` or `health` field on one of those entries would otherwise open or
|
|
142
|
+
* close a condition the control plane never reported.
|
|
143
|
+
*
|
|
144
|
+
* @category constants
|
|
145
|
+
* @since 0.1.0
|
|
146
|
+
*/
|
|
147
|
+
export declare const defaultDetectors: Readonly<Record<string, Detector>>;
|
|
148
|
+
/**
|
|
149
|
+
* A condition that is open on a run, and when it opened.
|
|
150
|
+
*
|
|
151
|
+
* @category models
|
|
152
|
+
* @since 0.1.0
|
|
153
|
+
*/
|
|
154
|
+
export interface Open {
|
|
155
|
+
readonly runId: string;
|
|
156
|
+
readonly condition: string;
|
|
157
|
+
/** The journal time of the entry that opened the condition. */
|
|
158
|
+
readonly since: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* One alert a policy decided to raise.
|
|
162
|
+
*
|
|
163
|
+
* @category models
|
|
164
|
+
* @since 0.1.0
|
|
165
|
+
*/
|
|
166
|
+
export interface Alert {
|
|
167
|
+
readonly runId: string;
|
|
168
|
+
readonly condition: string;
|
|
169
|
+
readonly since: number;
|
|
170
|
+
/**
|
|
171
|
+
* The journal instant the condition outlived its delay: `since` plus the
|
|
172
|
+
* rule's `afterMs`, never the wall clock of whichever tick noticed.
|
|
173
|
+
*/
|
|
174
|
+
readonly firedAt: number;
|
|
175
|
+
readonly severity: Severity;
|
|
176
|
+
readonly coalescingKey: string;
|
|
177
|
+
readonly owner?: string | undefined;
|
|
178
|
+
readonly runbook?: string | undefined;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The journal source the alerter writes one outcome of one alert under.
|
|
182
|
+
*
|
|
183
|
+
* A delivery record counts as evidence that an alert went out only under this
|
|
184
|
+
* source, so another producer's `flows.alerts.delivered` record with a
|
|
185
|
+
* matching `alertId` does not suppress a page. This scopes records; it does
|
|
186
|
+
* not authenticate the writer, because the journal does not bind a source to
|
|
187
|
+
* a writer.
|
|
188
|
+
*
|
|
189
|
+
* @param id the alert's {@link alertId}
|
|
190
|
+
* @param outcome `delivered`, `refused`, or `failed/<code>`
|
|
191
|
+
* @private
|
|
192
|
+
* @since 1.0.0
|
|
193
|
+
*/
|
|
194
|
+
export declare const recordSourceId: (id: string, outcome: string) => string;
|
|
195
|
+
/**
|
|
196
|
+
* The key an alert coalesces on: one open condition on one run.
|
|
197
|
+
*
|
|
198
|
+
* Each component is percent-encoded before it is joined, so a run id or a
|
|
199
|
+
* condition name containing the separator cannot forge another pair's key and
|
|
200
|
+
* suppress a page that belongs to somebody else. Both components are values a
|
|
201
|
+
* deployment chooses, which is why the encoding is not optional.
|
|
202
|
+
*
|
|
203
|
+
* @param runId the run the condition is open on
|
|
204
|
+
* @param condition the condition name the policy uses
|
|
205
|
+
* @category getters
|
|
206
|
+
* @since 1.0.0
|
|
207
|
+
*/
|
|
208
|
+
export declare const coalescingKey: (runId: string, condition: string) => string;
|
|
209
|
+
/**
|
|
210
|
+
* The identity a delivery is recorded under.
|
|
211
|
+
*
|
|
212
|
+
* The opening time is part of it on purpose. A condition that clears and
|
|
213
|
+
* re-opens is a NEW alert, because the second approval wait is not the first
|
|
214
|
+
* one, and a key without the time would suppress it forever.
|
|
215
|
+
*
|
|
216
|
+
* @param alert the alert
|
|
217
|
+
* @category getters
|
|
218
|
+
* @since 0.1.0
|
|
219
|
+
*/
|
|
220
|
+
export declare const alertId: (alert: Pick<Alert, "coalescingKey" | "since">) => string;
|
|
221
|
+
/**
|
|
222
|
+
* The detectors one policy decides with: the four this package ships, with
|
|
223
|
+
* the deployment's own overriding them by name.
|
|
224
|
+
*
|
|
225
|
+
* @private
|
|
226
|
+
* @since 1.0.0
|
|
227
|
+
*/
|
|
228
|
+
export declare const detectorsOf: (policy: Policy) => Readonly<Record<string, Detector>>;
|
|
229
|
+
/**
|
|
230
|
+
* One entry's payload when it is a record a detector can read a field off,
|
|
231
|
+
* and `undefined` for the arrays and primitives that carry no fields at all.
|
|
232
|
+
*
|
|
233
|
+
* @private
|
|
234
|
+
* @since 1.0.0
|
|
235
|
+
*/
|
|
236
|
+
export declare const payloadRecord: (payload: unknown) => Readonly<Record<string, unknown>> | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* Folds one entry into the open-condition map. Shared by {@link conditions}
|
|
239
|
+
* and the runtime's incremental read, so both decide a condition the same
|
|
240
|
+
* way.
|
|
241
|
+
*
|
|
242
|
+
* The alerter's own records are never evidence about a condition. They are
|
|
243
|
+
* written into the same journal they are read from, and they carry the alert's
|
|
244
|
+
* own vocabulary: a refusal reports the answering HTTP `status`, which a
|
|
245
|
+
* detector watching the run's `status` would read as the condition clearing.
|
|
246
|
+
* A page would then close the condition it paged about and the next tick would
|
|
247
|
+
* re-open it, so a webhook that answered 503 once would alert forever.
|
|
248
|
+
*
|
|
249
|
+
* @private
|
|
250
|
+
* @since 1.0.0
|
|
251
|
+
*/
|
|
252
|
+
export declare const observe: (policy: Policy, detectors: Readonly<Record<string, Detector>>, entry: JournalEvent.Entry, payload: Readonly<Record<string, unknown>>, open: Map<string, number>) => void;
|
|
253
|
+
/**
|
|
254
|
+
* Every condition a run's journal leaves open, with the time each opened.
|
|
255
|
+
*
|
|
256
|
+
* An entry that carries a detector's field opens the condition when the value
|
|
257
|
+
* matches and closes it when it does not. Entries the detector does not name
|
|
258
|
+
* are ignored entirely, so a monitor beat cannot close an approval wait and an
|
|
259
|
+
* approval cannot close a stall.
|
|
260
|
+
*
|
|
261
|
+
* @param policy the policy whose detectors decide what counts
|
|
262
|
+
* @param runId the run the entries belong to
|
|
263
|
+
* @param entries the run's journal, oldest first
|
|
264
|
+
* @category projections
|
|
265
|
+
* @since 0.1.0
|
|
266
|
+
*/
|
|
267
|
+
export declare const conditions: (policy: Policy, runId: string, entries: ReadonlyArray<JournalEvent.Entry>) => ReadonlyArray<Open>;
|
|
268
|
+
/**
|
|
269
|
+
* The alerts a policy raises for the conditions open at `now`.
|
|
270
|
+
*
|
|
271
|
+
* Pure, and a function of journal time: the same journal raises the same
|
|
272
|
+
* alerts in any process, at any instant past the delay. `now` decides WHETHER
|
|
273
|
+
* an alert is raised; it never appears in one. A condition that has been open
|
|
274
|
+
* for less than its rule's delay raises nothing, which is the whole point of
|
|
275
|
+
* the delay, because most stalls clear themselves.
|
|
276
|
+
*
|
|
277
|
+
* @param policy the policy
|
|
278
|
+
* @param open the conditions the journal left open
|
|
279
|
+
* @param now the instant to judge them at
|
|
280
|
+
* @category projections
|
|
281
|
+
* @since 0.1.0
|
|
282
|
+
*/
|
|
283
|
+
export declare const decide: (policy: Policy, open: ReadonlyArray<Open>, now: number) => ReadonlyArray<Alert>;
|
|
284
|
+
//# sourceMappingURL=AlertPolicy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AlertPolicy.d.ts","sourceRoot":"","sources":["../../src/AlertPolicy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,2BAA2B,CAAA;AAE1D;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,wBAAwB,CAAA;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,2DAAmD,CAAA;AAExE;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,OAAO,QAAQ,CAAC,IAAI,CAAA;AAE3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,QAAQ;;;;EAInB,CAAA;AAEF;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,OAAO,QAAQ,CAAC,IAAI,CAAA;AAE3C;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,IAAI;;;;;EAKf,CAAA;AAEF;;;;;GAKG;AACH,MAAM,MAAM,IAAI,GAAG,OAAO,IAAI,CAAC,IAAI,CAAA;AAEnC;;;;;;;;GAQG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;IAOjB,6DAA6D;;;;;;EAE7D,CAAA;AAEF;;;;;GAKG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,MAAM,CAAC,IAAI,CAAA;AAEvC;;;;;;;;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AAO,MAAM,aAAa,GAAI,SAAS,OAAO,KAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAGtE,CAAA;AAEf;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,OAAO,GAClB,QAAQ,MAAM,EACd,WAAW,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,EAC7C,OAAO,YAAY,CAAC,KAAK,EACzB,SAAS,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC1C,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,KACxB,IAgBF,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,UAAU,GACrB,QAAQ,MAAM,EACd,OAAO,MAAM,EACb,SAAS,aAAa,CAAC,YAAY,CAAC,KAAK,CAAC,KACzC,aAAa,CAAC,IAAI,CASpB,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,MAAM,GACjB,QAAQ,MAAM,EACd,MAAM,aAAa,CAAC,IAAI,CAAC,EACzB,KAAK,MAAM,KACV,aAAa,CAAC,KAAK,CAsBlB,CAAA"}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alert policy: what a condition is, when a journal leaves one open, and
|
|
3
|
+
* which open conditions have outlived their delay.
|
|
4
|
+
*
|
|
5
|
+
* Everything here is pure and a function of journal time, so the same entries
|
|
6
|
+
* decide the same alerts in any process at any instant past the delay. The
|
|
7
|
+
* transport that pages about them is `./layerWebhook.ts` and the journal-backed
|
|
8
|
+
* tick that drives them is `./AlertRuntime.ts`; `./Alerts.ts` is the module a
|
|
9
|
+
* consumer imports.
|
|
10
|
+
*
|
|
11
|
+
* @since 1.0.0
|
|
12
|
+
*/
|
|
13
|
+
import { Schema } from "effect";
|
|
14
|
+
/**
|
|
15
|
+
* The journal event type one delivered alert is recorded under.
|
|
16
|
+
*
|
|
17
|
+
* @category constants
|
|
18
|
+
* @since 0.1.0
|
|
19
|
+
*/
|
|
20
|
+
export const deliveredEventType = "flows.alerts.delivered";
|
|
21
|
+
/**
|
|
22
|
+
* The journal event type one failed delivery attempt is recorded under.
|
|
23
|
+
*
|
|
24
|
+
* One entry is written per alert per failure code, never one per tick: a
|
|
25
|
+
* webhook that stays down for an hour leaves one record, not a hundred and
|
|
26
|
+
* twenty that every later tick has to read past.
|
|
27
|
+
*
|
|
28
|
+
* @category constants
|
|
29
|
+
* @since 0.1.0
|
|
30
|
+
*/
|
|
31
|
+
export const failedEventType = "flows.alerts.failed";
|
|
32
|
+
/**
|
|
33
|
+
* How loud an alert is.
|
|
34
|
+
*
|
|
35
|
+
* @category models
|
|
36
|
+
* @since 0.1.0
|
|
37
|
+
*/
|
|
38
|
+
export const Severity = Schema.Literals(["info", "warning", "critical"]);
|
|
39
|
+
/**
|
|
40
|
+
* How a condition is recognized in the journal.
|
|
41
|
+
*
|
|
42
|
+
* `field` names a payload key, `value` the value that means the condition
|
|
43
|
+
* holds, and `eventTypes` narrows which entries are consulted at all. An
|
|
44
|
+
* `eventTypes` item ending in `.*` names a family: `control.*` consults
|
|
45
|
+
* `control.run.parked` and `control.monitor.beat` alike. An entry that carries
|
|
46
|
+
* the field with a different value CLOSES the condition, which is what makes a
|
|
47
|
+
* resume clear an approval alert without a second vocabulary for "cleared".
|
|
48
|
+
*
|
|
49
|
+
* @category models
|
|
50
|
+
* @since 0.1.0
|
|
51
|
+
*/
|
|
52
|
+
export const Detector = Schema.Struct({
|
|
53
|
+
field: Schema.NonEmptyString,
|
|
54
|
+
value: Schema.NonEmptyString,
|
|
55
|
+
eventTypes: Schema.optional(Schema.Array(Schema.NonEmptyString))
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* What to do about one condition, and after how long.
|
|
59
|
+
*
|
|
60
|
+
* `afterMs` is a whole, non-negative number of milliseconds. The bound is the
|
|
61
|
+
* schema's job because the delay is also arithmetic on journal time: a `NaN`
|
|
62
|
+
* delay fires on the first tick and stamps a `firedAt` that JSON writes as
|
|
63
|
+
* `null`, and a negative one fires with a `firedAt` earlier than the condition
|
|
64
|
+
* it describes.
|
|
65
|
+
*
|
|
66
|
+
* @category models
|
|
67
|
+
* @since 0.1.0
|
|
68
|
+
*/
|
|
69
|
+
export const Rule = Schema.Struct({
|
|
70
|
+
afterMs: Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0))),
|
|
71
|
+
severity: Schema.optional(Severity),
|
|
72
|
+
owner: Schema.optional(Schema.String),
|
|
73
|
+
runbook: Schema.optional(Schema.String)
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* The conditions a policy alerts on and the delays it alerts after.
|
|
77
|
+
*
|
|
78
|
+
* `defaults` fills in what a rule leaves out, so a policy states the delay per
|
|
79
|
+
* condition and the ownership once.
|
|
80
|
+
*
|
|
81
|
+
* @category models
|
|
82
|
+
* @since 0.1.0
|
|
83
|
+
*/
|
|
84
|
+
export const Policy = Schema.Struct({
|
|
85
|
+
defaults: Schema.optional(Schema.Struct({
|
|
86
|
+
severity: Schema.optional(Severity),
|
|
87
|
+
owner: Schema.optional(Schema.String),
|
|
88
|
+
runbook: Schema.optional(Schema.String)
|
|
89
|
+
})),
|
|
90
|
+
rules: Schema.Record(Schema.String, Rule),
|
|
91
|
+
/** Detectors for conditions this deployment names itself. */
|
|
92
|
+
detectors: Schema.optional(Schema.Record(Schema.String, Detector))
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* The four conditions a control plane journals out of the box.
|
|
96
|
+
*
|
|
97
|
+
* `status` rides on every `control.run.*` entry, and `health` on every
|
|
98
|
+
* `control.monitor.beat`. A deployment whose supervisor journals a park reason
|
|
99
|
+
* gets `quota-parked` from the same field the run summary reports it under.
|
|
100
|
+
*
|
|
101
|
+
* Each consults the `control.*` family only. A run's journal is shared with
|
|
102
|
+
* flow steps, agent traces, and anything else that writes to the run, and a
|
|
103
|
+
* `status` or `health` field on one of those entries would otherwise open or
|
|
104
|
+
* close a condition the control plane never reported.
|
|
105
|
+
*
|
|
106
|
+
* @category constants
|
|
107
|
+
* @since 0.1.0
|
|
108
|
+
*/
|
|
109
|
+
export const defaultDetectors = {
|
|
110
|
+
"waiting-approval": { field: "status", value: "waiting-approval", eventTypes: ["control.*"] },
|
|
111
|
+
failed: { field: "status", value: "failed", eventTypes: ["control.*"] },
|
|
112
|
+
stalled: { field: "health", value: "stalled", eventTypes: ["control.*"] },
|
|
113
|
+
"quota-parked": { field: "waitingReason", value: "quota", eventTypes: ["control.*"] }
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Whether a detector's `eventTypes` names an entry's event type, exactly or
|
|
117
|
+
* through a `.*` family.
|
|
118
|
+
*/
|
|
119
|
+
const names = (eventTypes, eventType) => eventTypes.some((named) => named.endsWith(".*") ? eventType.startsWith(named.slice(0, -1)) : named === eventType);
|
|
120
|
+
/**
|
|
121
|
+
* The journal source the alerter writes one outcome of one alert under.
|
|
122
|
+
*
|
|
123
|
+
* A delivery record counts as evidence that an alert went out only under this
|
|
124
|
+
* source, so another producer's `flows.alerts.delivered` record with a
|
|
125
|
+
* matching `alertId` does not suppress a page. This scopes records; it does
|
|
126
|
+
* not authenticate the writer, because the journal does not bind a source to
|
|
127
|
+
* a writer.
|
|
128
|
+
*
|
|
129
|
+
* @param id the alert's {@link alertId}
|
|
130
|
+
* @param outcome `delivered`, `refused`, or `failed/<code>`
|
|
131
|
+
* @private
|
|
132
|
+
* @since 1.0.0
|
|
133
|
+
*/
|
|
134
|
+
export const recordSourceId = (id, outcome) => `/notifications/alerts/${id}/${outcome}`;
|
|
135
|
+
/**
|
|
136
|
+
* The key an alert coalesces on: one open condition on one run.
|
|
137
|
+
*
|
|
138
|
+
* Each component is percent-encoded before it is joined, so a run id or a
|
|
139
|
+
* condition name containing the separator cannot forge another pair's key and
|
|
140
|
+
* suppress a page that belongs to somebody else. Both components are values a
|
|
141
|
+
* deployment chooses, which is why the encoding is not optional.
|
|
142
|
+
*
|
|
143
|
+
* @param runId the run the condition is open on
|
|
144
|
+
* @param condition the condition name the policy uses
|
|
145
|
+
* @category getters
|
|
146
|
+
* @since 1.0.0
|
|
147
|
+
*/
|
|
148
|
+
export const coalescingKey = (runId, condition) => `${encodeURIComponent(runId)}:${encodeURIComponent(condition)}`;
|
|
149
|
+
/**
|
|
150
|
+
* The identity a delivery is recorded under.
|
|
151
|
+
*
|
|
152
|
+
* The opening time is part of it on purpose. A condition that clears and
|
|
153
|
+
* re-opens is a NEW alert, because the second approval wait is not the first
|
|
154
|
+
* one, and a key without the time would suppress it forever.
|
|
155
|
+
*
|
|
156
|
+
* @param alert the alert
|
|
157
|
+
* @category getters
|
|
158
|
+
* @since 0.1.0
|
|
159
|
+
*/
|
|
160
|
+
export const alertId = (alert) => `alert:${alert.coalescingKey}:${alert.since}`;
|
|
161
|
+
/**
|
|
162
|
+
* The detectors one policy decides with: the four this package ships, with
|
|
163
|
+
* the deployment's own overriding them by name.
|
|
164
|
+
*
|
|
165
|
+
* @private
|
|
166
|
+
* @since 1.0.0
|
|
167
|
+
*/
|
|
168
|
+
export const detectorsOf = (policy) => ({
|
|
169
|
+
...defaultDetectors,
|
|
170
|
+
...policy.detectors
|
|
171
|
+
});
|
|
172
|
+
/**
|
|
173
|
+
* One entry's payload when it is a record a detector can read a field off,
|
|
174
|
+
* and `undefined` for the arrays and primitives that carry no fields at all.
|
|
175
|
+
*
|
|
176
|
+
* @private
|
|
177
|
+
* @since 1.0.0
|
|
178
|
+
*/
|
|
179
|
+
export const payloadRecord = (payload) => typeof payload === "object" && payload !== null && !Array.isArray(payload)
|
|
180
|
+
? payload
|
|
181
|
+
: undefined;
|
|
182
|
+
/**
|
|
183
|
+
* Folds one entry into the open-condition map. Shared by {@link conditions}
|
|
184
|
+
* and the runtime's incremental read, so both decide a condition the same
|
|
185
|
+
* way.
|
|
186
|
+
*
|
|
187
|
+
* The alerter's own records are never evidence about a condition. They are
|
|
188
|
+
* written into the same journal they are read from, and they carry the alert's
|
|
189
|
+
* own vocabulary: a refusal reports the answering HTTP `status`, which a
|
|
190
|
+
* detector watching the run's `status` would read as the condition clearing.
|
|
191
|
+
* A page would then close the condition it paged about and the next tick would
|
|
192
|
+
* re-open it, so a webhook that answered 503 once would alert forever.
|
|
193
|
+
*
|
|
194
|
+
* @private
|
|
195
|
+
* @since 1.0.0
|
|
196
|
+
*/
|
|
197
|
+
export const observe = (policy, detectors, entry, payload, open) => {
|
|
198
|
+
if (entry.eventType === deliveredEventType || entry.eventType === failedEventType)
|
|
199
|
+
return;
|
|
200
|
+
for (const condition of Object.keys(policy.rules)) {
|
|
201
|
+
const detector = detectors[condition];
|
|
202
|
+
if (detector === undefined)
|
|
203
|
+
continue;
|
|
204
|
+
if (detector.eventTypes !== undefined && !names(detector.eventTypes, entry.eventType))
|
|
205
|
+
continue;
|
|
206
|
+
// Own properties only. `in` walks `Object.prototype`, so a detector named
|
|
207
|
+
// `toString` or `constructor` would read every record-shaped entry in the
|
|
208
|
+
// run as evidence and close the condition on all of them.
|
|
209
|
+
if (!Object.hasOwn(payload, detector.field))
|
|
210
|
+
continue;
|
|
211
|
+
if (payload[detector.field] === detector.value) {
|
|
212
|
+
if (!open.has(condition))
|
|
213
|
+
open.set(condition, entry.emittedAtMs);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
open.delete(condition);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Every condition a run's journal leaves open, with the time each opened.
|
|
222
|
+
*
|
|
223
|
+
* An entry that carries a detector's field opens the condition when the value
|
|
224
|
+
* matches and closes it when it does not. Entries the detector does not name
|
|
225
|
+
* are ignored entirely, so a monitor beat cannot close an approval wait and an
|
|
226
|
+
* approval cannot close a stall.
|
|
227
|
+
*
|
|
228
|
+
* @param policy the policy whose detectors decide what counts
|
|
229
|
+
* @param runId the run the entries belong to
|
|
230
|
+
* @param entries the run's journal, oldest first
|
|
231
|
+
* @category projections
|
|
232
|
+
* @since 0.1.0
|
|
233
|
+
*/
|
|
234
|
+
export const conditions = (policy, runId, entries) => {
|
|
235
|
+
const detectors = detectorsOf(policy);
|
|
236
|
+
const open = new Map();
|
|
237
|
+
for (const entry of entries) {
|
|
238
|
+
const payload = payloadRecord(entry.payload);
|
|
239
|
+
if (payload === undefined)
|
|
240
|
+
continue;
|
|
241
|
+
observe(policy, detectors, entry, payload, open);
|
|
242
|
+
}
|
|
243
|
+
return Array.from(open, ([condition, since]) => ({ runId, condition, since }));
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* The alerts a policy raises for the conditions open at `now`.
|
|
247
|
+
*
|
|
248
|
+
* Pure, and a function of journal time: the same journal raises the same
|
|
249
|
+
* alerts in any process, at any instant past the delay. `now` decides WHETHER
|
|
250
|
+
* an alert is raised; it never appears in one. A condition that has been open
|
|
251
|
+
* for less than its rule's delay raises nothing, which is the whole point of
|
|
252
|
+
* the delay, because most stalls clear themselves.
|
|
253
|
+
*
|
|
254
|
+
* @param policy the policy
|
|
255
|
+
* @param open the conditions the journal left open
|
|
256
|
+
* @param now the instant to judge them at
|
|
257
|
+
* @category projections
|
|
258
|
+
* @since 0.1.0
|
|
259
|
+
*/
|
|
260
|
+
export const decide = (policy, open, now) => open.flatMap((condition) => {
|
|
261
|
+
const rule = policy.rules[condition.condition];
|
|
262
|
+
if (rule === undefined)
|
|
263
|
+
return [];
|
|
264
|
+
if (now - condition.since < rule.afterMs)
|
|
265
|
+
return [];
|
|
266
|
+
const severity = rule.severity ?? policy.defaults?.severity ?? "warning";
|
|
267
|
+
const owner = rule.owner ?? policy.defaults?.owner;
|
|
268
|
+
const runbook = rule.runbook ?? policy.defaults?.runbook;
|
|
269
|
+
return [{
|
|
270
|
+
runId: condition.runId,
|
|
271
|
+
condition: condition.condition,
|
|
272
|
+
since: condition.since,
|
|
273
|
+
// Derived from the journal, not read off the clock. The alert id is
|
|
274
|
+
// stable across ticks, and the queue refuses a reused id whose content
|
|
275
|
+
// changed, so an alert stamped with the reading time would become
|
|
276
|
+
// permanently undeliverable the moment a tick refused and time moved on.
|
|
277
|
+
firedAt: condition.since + rule.afterMs,
|
|
278
|
+
severity,
|
|
279
|
+
coalescingKey: coalescingKey(condition.runId, condition.condition),
|
|
280
|
+
...(owner === undefined ? {} : { owner }),
|
|
281
|
+
...(runbook === undefined ? {} : { runbook })
|
|
282
|
+
}];
|
|
283
|
+
});
|
|
284
|
+
//# sourceMappingURL=AlertPolicy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AlertPolicy.js","sourceRoot":"","sources":["../../src/AlertPolicy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,wBAAwB,CAAA;AAE1D;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,qBAAqB,CAAA;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC,CAAA;AAUxE;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC;IACpC,KAAK,EAAE,MAAM,CAAC,cAAc;IAC5B,KAAK,EAAE,MAAM,CAAC,cAAc;IAC5B,UAAU,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;CACjE,CAAC,CAAA;AAUF;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,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,CAAC,KAAK;YACtB,SAAS,EAAE,SAAS,CAAC,SAAS;YAC9B,KAAK,EAAE,SAAS,CAAC,KAAK;YACtB,oEAAoE;YACpE,uEAAuE;YACvE,kEAAkE;YAClE,yEAAyE;YACzE,OAAO,EAAE,SAAS,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO;YACvC,QAAQ;YACR,aAAa,EAAE,aAAa,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC,SAAS,CAAC;YAClE,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;YACzC,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC;SAC9C,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["/**\n * Alert policy: what a condition is, when a journal leaves one open, and\n * which open conditions have outlived their delay.\n *\n * Everything here is pure and a function of journal time, so the same entries\n * decide the same alerts in any process at any instant past the delay. The\n * transport that pages about them is `./layerWebhook.ts` and the journal-backed\n * tick that drives them is `./AlertRuntime.ts`; `./Alerts.ts` is the module a\n * consumer imports.\n *\n * @since 1.0.0\n */\n\nimport type { JournalEvent } from \"@smthrs/journal\"\nimport { Schema } from \"effect\"\n\n/**\n * The journal event type one delivered alert is recorded under.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const deliveredEventType = \"flows.alerts.delivered\"\n\n/**\n * The journal event type one failed delivery attempt is recorded under.\n *\n * One entry is written per alert per failure code, never one per tick: a\n * webhook that stays down for an hour leaves one record, not a hundred and\n * twenty that every later tick has to read past.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const failedEventType = \"flows.alerts.failed\"\n\n/**\n * How loud an alert is.\n *\n * @category models\n * @since 0.1.0\n */\nexport const Severity = Schema.Literals([\"info\", \"warning\", \"critical\"])\n\n/**\n * How loud an alert is.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Severity = typeof Severity.Type\n\n/**\n * How a condition is recognized in the journal.\n *\n * `field` names a payload key, `value` the value that means the condition\n * holds, and `eventTypes` narrows which entries are consulted at all. An\n * `eventTypes` item ending in `.*` names a family: `control.*` consults\n * `control.run.parked` and `control.monitor.beat` alike. An entry that carries\n * the field with a different value CLOSES the condition, which is what makes a\n * resume clear an approval alert without a second vocabulary for \"cleared\".\n *\n * @category models\n * @since 0.1.0\n */\nexport const Detector = Schema.Struct({\n field: Schema.NonEmptyString,\n value: Schema.NonEmptyString,\n eventTypes: Schema.optional(Schema.Array(Schema.NonEmptyString))\n})\n\n/**\n * How a condition is recognized in the journal.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Detector = typeof Detector.Type\n\n/**\n * What to do about one condition, and after how long.\n *\n * `afterMs` is a whole, non-negative number of milliseconds. The bound is the\n * schema's job because the delay is also arithmetic on journal time: a `NaN`\n * delay fires on the first tick and stamps a `firedAt` that JSON writes as\n * `null`, and a negative one fires with a `firedAt` earlier than the condition\n * it describes.\n *\n * @category models\n * @since 0.1.0\n */\nexport const Rule = Schema.Struct({\n afterMs: Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0))),\n severity: Schema.optional(Severity),\n owner: Schema.optional(Schema.String),\n runbook: Schema.optional(Schema.String)\n})\n\n/**\n * What to do about one condition, and after how long.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Rule = typeof Rule.Type\n\n/**\n * The conditions a policy alerts on and the delays it alerts after.\n *\n * `defaults` fills in what a rule leaves out, so a policy states the delay per\n * condition and the ownership once.\n *\n * @category models\n * @since 0.1.0\n */\nexport const Policy = Schema.Struct({\n defaults: Schema.optional(Schema.Struct({\n severity: Schema.optional(Severity),\n owner: Schema.optional(Schema.String),\n runbook: Schema.optional(Schema.String)\n })),\n rules: Schema.Record(Schema.String, Rule),\n /** Detectors for conditions this deployment names itself. */\n detectors: Schema.optional(Schema.Record(Schema.String, Detector))\n})\n\n/**\n * The conditions a policy alerts on and the delays it alerts after.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Policy = typeof Policy.Type\n\n/**\n * The four conditions a control plane journals out of the box.\n *\n * `status` rides on every `control.run.*` entry, and `health` on every\n * `control.monitor.beat`. A deployment whose supervisor journals a park reason\n * gets `quota-parked` from the same field the run summary reports it under.\n *\n * Each consults the `control.*` family only. A run's journal is shared with\n * flow steps, agent traces, and anything else that writes to the run, and a\n * `status` or `health` field on one of those entries would otherwise open or\n * close a condition the control plane never reported.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const defaultDetectors: Readonly<Record<string, Detector>> = {\n \"waiting-approval\": { field: \"status\", value: \"waiting-approval\", eventTypes: [\"control.*\"] },\n failed: { field: \"status\", value: \"failed\", eventTypes: [\"control.*\"] },\n stalled: { field: \"health\", value: \"stalled\", eventTypes: [\"control.*\"] },\n \"quota-parked\": { field: \"waitingReason\", value: \"quota\", eventTypes: [\"control.*\"] }\n}\n\n/**\n * Whether a detector's `eventTypes` names an entry's event type, exactly or\n * through a `.*` family.\n */\nconst names = (eventTypes: ReadonlyArray<string>, eventType: string): boolean =>\n eventTypes.some((named) => named.endsWith(\".*\") ? eventType.startsWith(named.slice(0, -1)) : named === eventType)\n\n/**\n * A condition that is open on a run, and when it opened.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Open {\n readonly runId: string\n readonly condition: string\n /** The journal time of the entry that opened the condition. */\n readonly since: number\n}\n\n/**\n * One alert a policy decided to raise.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Alert {\n readonly runId: string\n readonly condition: string\n readonly since: number\n /**\n * The journal instant the condition outlived its delay: `since` plus the\n * rule's `afterMs`, never the wall clock of whichever tick noticed.\n */\n readonly firedAt: number\n readonly severity: Severity\n readonly coalescingKey: string\n readonly owner?: string | undefined\n readonly runbook?: string | undefined\n}\n\n/**\n * The journal source the alerter writes one outcome of one alert under.\n *\n * A delivery record counts as evidence that an alert went out only under this\n * source, so another producer's `flows.alerts.delivered` record with a\n * matching `alertId` does not suppress a page. This scopes records; it does\n * not authenticate the writer, because the journal does not bind a source to\n * a writer.\n *\n * @param id the alert's {@link alertId}\n * @param outcome `delivered`, `refused`, or `failed/<code>`\n * @private\n * @since 1.0.0\n */\nexport const recordSourceId = (id: string, outcome: string): string => `/notifications/alerts/${id}/${outcome}`\n\n/**\n * The key an alert coalesces on: one open condition on one run.\n *\n * Each component is percent-encoded before it is joined, so a run id or a\n * condition name containing the separator cannot forge another pair's key and\n * suppress a page that belongs to somebody else. Both components are values a\n * deployment chooses, which is why the encoding is not optional.\n *\n * @param runId the run the condition is open on\n * @param condition the condition name the policy uses\n * @category getters\n * @since 1.0.0\n */\nexport const coalescingKey = (runId: string, condition: string): string =>\n `${encodeURIComponent(runId)}:${encodeURIComponent(condition)}`\n\n/**\n * The identity a delivery is recorded under.\n *\n * The opening time is part of it on purpose. A condition that clears and\n * re-opens is a NEW alert, because the second approval wait is not the first\n * one, and a key without the time would suppress it forever.\n *\n * @param alert the alert\n * @category getters\n * @since 0.1.0\n */\nexport const alertId = (alert: Pick<Alert, \"coalescingKey\" | \"since\">): string =>\n `alert:${alert.coalescingKey}:${alert.since}`\n\n/**\n * The detectors one policy decides with: the four this package ships, with\n * the deployment's own overriding them by name.\n *\n * @private\n * @since 1.0.0\n */\nexport const detectorsOf = (policy: Policy): Readonly<Record<string, Detector>> => ({\n ...defaultDetectors,\n ...policy.detectors\n})\n\n/**\n * One entry's payload when it is a record a detector can read a field off,\n * and `undefined` for the arrays and primitives that carry no fields at all.\n *\n * @private\n * @since 1.0.0\n */\nexport const payloadRecord = (payload: unknown): Readonly<Record<string, unknown>> | undefined =>\n typeof payload === \"object\" && payload !== null && !Array.isArray(payload)\n ? payload as Readonly<Record<string, unknown>>\n : undefined\n\n/**\n * Folds one entry into the open-condition map. Shared by {@link conditions}\n * and the runtime's incremental read, so both decide a condition the same\n * way.\n *\n * The alerter's own records are never evidence about a condition. They are\n * written into the same journal they are read from, and they carry the alert's\n * own vocabulary: a refusal reports the answering HTTP `status`, which a\n * detector watching the run's `status` would read as the condition clearing.\n * A page would then close the condition it paged about and the next tick would\n * re-open it, so a webhook that answered 503 once would alert forever.\n *\n * @private\n * @since 1.0.0\n */\nexport const observe = (\n policy: Policy,\n detectors: Readonly<Record<string, Detector>>,\n entry: JournalEvent.Entry,\n payload: Readonly<Record<string, unknown>>,\n open: Map<string, number>\n): void => {\n if (entry.eventType === deliveredEventType || entry.eventType === failedEventType) return\n for (const condition of Object.keys(policy.rules)) {\n const detector = detectors[condition]\n if (detector === undefined) continue\n if (detector.eventTypes !== undefined && !names(detector.eventTypes, entry.eventType)) continue\n // Own properties only. `in` walks `Object.prototype`, so a detector named\n // `toString` or `constructor` would read every record-shaped entry in the\n // run as evidence and close the condition on all of them.\n if (!Object.hasOwn(payload, detector.field)) continue\n if (payload[detector.field] === detector.value) {\n if (!open.has(condition)) open.set(condition, entry.emittedAtMs)\n } else {\n open.delete(condition)\n }\n }\n}\n\n/**\n * Every condition a run's journal leaves open, with the time each opened.\n *\n * An entry that carries a detector's field opens the condition when the value\n * matches and closes it when it does not. Entries the detector does not name\n * are ignored entirely, so a monitor beat cannot close an approval wait and an\n * approval cannot close a stall.\n *\n * @param policy the policy whose detectors decide what counts\n * @param runId the run the entries belong to\n * @param entries the run's journal, oldest first\n * @category projections\n * @since 0.1.0\n */\nexport const conditions = (\n policy: Policy,\n runId: string,\n entries: ReadonlyArray<JournalEvent.Entry>\n): ReadonlyArray<Open> => {\n const detectors = detectorsOf(policy)\n const open = new Map<string, number>()\n for (const entry of entries) {\n const payload = payloadRecord(entry.payload)\n if (payload === undefined) continue\n observe(policy, detectors, entry, payload, open)\n }\n return Array.from(open, ([condition, since]) => ({ runId, condition, since }))\n}\n\n/**\n * The alerts a policy raises for the conditions open at `now`.\n *\n * Pure, and a function of journal time: the same journal raises the same\n * alerts in any process, at any instant past the delay. `now` decides WHETHER\n * an alert is raised; it never appears in one. A condition that has been open\n * for less than its rule's delay raises nothing, which is the whole point of\n * the delay, because most stalls clear themselves.\n *\n * @param policy the policy\n * @param open the conditions the journal left open\n * @param now the instant to judge them at\n * @category projections\n * @since 0.1.0\n */\nexport const decide = (\n policy: Policy,\n open: ReadonlyArray<Open>,\n now: number\n): ReadonlyArray<Alert> =>\n open.flatMap((condition) => {\n const rule = policy.rules[condition.condition]\n if (rule === undefined) return []\n if (now - condition.since < rule.afterMs) return []\n const severity = rule.severity ?? policy.defaults?.severity ?? \"warning\"\n const owner = rule.owner ?? policy.defaults?.owner\n const runbook = rule.runbook ?? policy.defaults?.runbook\n return [{\n runId: condition.runId,\n condition: condition.condition,\n since: condition.since,\n // Derived from the journal, not read off the clock. The alert id is\n // stable across ticks, and the queue refuses a reused id whose content\n // changed, so an alert stamped with the reading time would become\n // permanently undeliverable the moment a tick refused and time moved on.\n firedAt: condition.since + rule.afterMs,\n severity,\n coalescingKey: coalescingKey(condition.runId, condition.condition),\n ...(owner === undefined ? {} : { owner }),\n ...(runbook === undefined ? {} : { runbook })\n }]\n })\n"]}
|