@checkstack/slo-backend 0.3.4 → 0.4.0
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 +168 -0
- package/package.json +23 -20
- package/src/index.ts +14 -0
- package/src/router.ts +1 -1
- package/src/service.ts +5 -5
- package/src/slo-engine.ts +4 -4
- package/src/slo-gitops-kinds.test.ts +238 -0
- package/src/slo-gitops-kinds.ts +174 -0
- package/src/streak-calculator.ts +1 -1
- package/tsconfig.json +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,173 @@
|
|
|
1
1
|
# @checkstack/slo-backend
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f6f9a5c: Add a GitOps `SLO` kind so reliability targets can be declared in YAML.
|
|
8
|
+
|
|
9
|
+
The kind references its target system via `systemRef` and may optionally
|
|
10
|
+
narrow to a single healthcheck via `healthcheckRef`. Excluded
|
|
11
|
+
dependencies are referenced by ref and resolved to system IDs at
|
|
12
|
+
reconcile time.
|
|
13
|
+
|
|
14
|
+
```yaml
|
|
15
|
+
apiVersion: checkstack.io/v1alpha1
|
|
16
|
+
kind: SLO
|
|
17
|
+
metadata:
|
|
18
|
+
name: payments-availability
|
|
19
|
+
spec:
|
|
20
|
+
systemRef: { kind: System, name: payments-api }
|
|
21
|
+
target: 99.9
|
|
22
|
+
windowDays: 30
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Reconcile maps to `SloService.createObjective` /
|
|
26
|
+
`updateObjective` / `deleteObjective`; the entity ID stored in
|
|
27
|
+
provenance is the SLO objective UUID, so renames in YAML preserve
|
|
28
|
+
identity.
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- Updated dependencies [42abfff]
|
|
33
|
+
- Updated dependencies [f6f9a5c]
|
|
34
|
+
- Updated dependencies [1ef2e79]
|
|
35
|
+
- Updated dependencies [aa89bc5]
|
|
36
|
+
- @checkstack/common@0.9.0
|
|
37
|
+
- @checkstack/gitops-common@0.3.0
|
|
38
|
+
- @checkstack/gitops-backend@0.3.0
|
|
39
|
+
- @checkstack/catalog-common@2.1.0
|
|
40
|
+
- @checkstack/catalog-backend@1.1.0
|
|
41
|
+
- @checkstack/queue-api@0.3.0
|
|
42
|
+
- @checkstack/cache-api@0.3.0
|
|
43
|
+
- @checkstack/backend-api@0.15.1
|
|
44
|
+
- @checkstack/command-backend@0.1.25
|
|
45
|
+
- @checkstack/dependency-common@1.0.2
|
|
46
|
+
- @checkstack/healthcheck-backend@1.0.4
|
|
47
|
+
- @checkstack/healthcheck-common@1.0.2
|
|
48
|
+
- @checkstack/integration-backend@0.1.25
|
|
49
|
+
- @checkstack/integration-common@0.3.2
|
|
50
|
+
- @checkstack/signal-common@0.2.2
|
|
51
|
+
- @checkstack/slo-common@0.3.3
|
|
52
|
+
- @checkstack/cache-utils@0.2.5
|
|
53
|
+
|
|
54
|
+
## 0.3.5
|
|
55
|
+
|
|
56
|
+
### Patch Changes
|
|
57
|
+
|
|
58
|
+
- 50e5f5f: Runtime plugin system: install + uninstall plugins from npm, GitHub releases
|
|
59
|
+
(including private GitHub Enterprise instances), or tarball uploads at
|
|
60
|
+
runtime, with multi-package bundles, dependency-derived compatibility checks,
|
|
61
|
+
multi-instance coordination via a Postgres artifact store, and
|
|
62
|
+
single-coordinator destructive cleanup.
|
|
63
|
+
|
|
64
|
+
Highlights:
|
|
65
|
+
|
|
66
|
+
- New `PluginSource` discriminated union and `PluginInstaller` /
|
|
67
|
+
`PluginInstallerRegistry` interfaces in `@checkstack/backend-api`. The
|
|
68
|
+
GitHub variant accepts an optional `apiBaseUrl` so deployments backed by
|
|
69
|
+
GitHub Enterprise can install from `https://ghe.example.com/api/v3`
|
|
70
|
+
instead of `api.github.com`.
|
|
71
|
+
- New `installPackageMetadataSchema` (Zod) in `@checkstack/common` validates
|
|
72
|
+
every plugin's `package.json` at install time. Required fields: `name`,
|
|
73
|
+
`version`, `description`, `author`, `license`, `checkstack.type`,
|
|
74
|
+
`checkstack.pluginId`. Optional: `checkstack.bundle`,
|
|
75
|
+
`checkstack.usageInstructions`, `checkstack.allowInstallScripts`.
|
|
76
|
+
- New `pluginManagerContract` in `@checkstack/pluginmanager-common` with
|
|
77
|
+
`list`, `previewInstall`, `install`, `previewUninstall`, `uninstall`, and
|
|
78
|
+
`events` procedures.
|
|
79
|
+
- New `@checkstack/pluginmanager-frontend` admin UI: installed-plugins list
|
|
80
|
+
with per-row uninstall (typed-confirmation modal, schema/configs/cascade
|
|
81
|
+
toggles), install page with NPM / Tarball Upload / GitHub Release tabs
|
|
82
|
+
(Catalog tab disabled — coming soon), and an events page surfacing the
|
|
83
|
+
install/uninstall audit log.
|
|
84
|
+
- New `bunx @checkstack/scripts plugin-pack` CLI for plugin authors —
|
|
85
|
+
per-package mode produces an npm-shaped tarball; `--bundle` mode produces
|
|
86
|
+
an outer tarball containing every sibling declared in
|
|
87
|
+
`package.json#checkstack.bundle`. Published to npm so external authors
|
|
88
|
+
can `bunx` it directly without a workspace checkout.
|
|
89
|
+
- Compatibility derived from `package.json#dependencies` ranges
|
|
90
|
+
(`semver.satisfies` against the platform's loaded `@checkstack/*`
|
|
91
|
+
versions) — no separate `compatibility` field.
|
|
92
|
+
- Multi-instance: originator persists artifacts + `plugins` rows + broadcasts
|
|
93
|
+
install/uninstall; receiving instances do in-process register/unregister
|
|
94
|
+
only. Destructive ops (drop schema, delete plugin_configs, delete
|
|
95
|
+
artifacts, delete `plugins` rows) run exactly once on the originator.
|
|
96
|
+
- Fresh-instance bootstrap: `loadPlugins()` hydrates any
|
|
97
|
+
`is_uninstallable=true` plugin missing from `node_modules` from the
|
|
98
|
+
artifact store before normal Phase 1 register.
|
|
99
|
+
- New schema: `plugin_artifacts` (tarball storage), `plugin_install_events`
|
|
100
|
+
(audit/error log). `plugins` extended with `version`, `metadata`,
|
|
101
|
+
`source`, `bundle_id`, `is_primary`. Local plugin sync now writes
|
|
102
|
+
`version` from each plugin's `package.json` so the admin UI shows real
|
|
103
|
+
versions instead of `—`.
|
|
104
|
+
- Tarball-upload endpoint (`POST /api/pluginmanager/upload-tarball`) for
|
|
105
|
+
the install UI; access-gated by `pluginmanager.plugin.manage`.
|
|
106
|
+
- Plugin Manager menu link added to the user menu (main grid, alongside
|
|
107
|
+
Profile / Notification Settings / etc.).
|
|
108
|
+
|
|
109
|
+
Cross-cutting changes:
|
|
110
|
+
|
|
111
|
+
- Backend request/response logging now flows through `rootLogger` (winston)
|
|
112
|
+
instead of `hono/logger`. 5xx responses include the response body inline
|
|
113
|
+
so swallowed early-return errors are visible in the log.
|
|
114
|
+
- The `/api/:pluginId/*` dispatcher now logs which core service is missing
|
|
115
|
+
or which `pluginId` had no metadata when it 500s.
|
|
116
|
+
- New `registerCorePluginMetadata` on `PluginManager` for core routers
|
|
117
|
+
(like the plugin manager itself) that need their metadata visible to the
|
|
118
|
+
RPC dispatcher without going through the full plugin lifecycle.
|
|
119
|
+
- ESLint: `unicorn/no-null` is now disabled globally. Drizzle distinguishes
|
|
120
|
+
between `null` (writes a real SQL NULL) and `undefined` (skip the column
|
|
121
|
+
on insert), so treating them as interchangeable produced latent bugs at
|
|
122
|
+
the persistence boundary. The bulk of the patch-bumped packages above
|
|
123
|
+
reflect lint-fix touches that landed when this rule was relaxed.
|
|
124
|
+
- Workspace-wide license normalization to `Elastic-2.0` (matches
|
|
125
|
+
`LICENSE.md`). Every `package.json` in the workspace now declares the
|
|
126
|
+
same SPDX identifier; the patch bumps capture this.
|
|
127
|
+
|
|
128
|
+
Plugin packages (every `plugins/*`): added a `pack` npm script
|
|
129
|
+
(`bunx @checkstack/scripts plugin-pack`), mirrored each plugin's
|
|
130
|
+
`pluginId` from `plugin-metadata.ts` into `package.json#checkstack.pluginId`
|
|
131
|
+
so install-time validation passes, stubbed any missing required metadata
|
|
132
|
+
fields (`description`, `author`, `license`), and added
|
|
133
|
+
`checkstack.bundle` to multi-package plugin primaries (telegram, rcon, ssh,
|
|
134
|
+
jira, queue-bullmq, queue-memory, cache-memory).
|
|
135
|
+
|
|
136
|
+
Breaking changes:
|
|
137
|
+
|
|
138
|
+
- The legacy single-method `PluginInstaller` interface (`install(packageName)`)
|
|
139
|
+
is removed. Callers must use `coreServices.pluginInstallerRegistry`.
|
|
140
|
+
- The old `pluginAdminContract` and `createPluginAdminRouter` are removed.
|
|
141
|
+
Replaced by `pluginManagerContract` in `@checkstack/pluginmanager-common`
|
|
142
|
+
and `createPluginManagerRouter` in `core/backend`.
|
|
143
|
+
- `@checkstack/test-utils-backend` no longer exports
|
|
144
|
+
`createMockPluginInstaller` / `MockPluginInstaller` (the legacy interface
|
|
145
|
+
it shimmed is gone).
|
|
146
|
+
|
|
147
|
+
Note: bumps are limited to `minor` (for packages with new public API
|
|
148
|
+
surface) and `patch` (for downstream consumers, license normalization,
|
|
149
|
+
and lint fixes). No `major` bumps despite the `PluginInstaller` removal —
|
|
150
|
+
the legacy interface had no third-party consumers in the wild before this
|
|
151
|
+
runtime plugin system landed, and the contract surface is the same shape
|
|
152
|
+
modulo the rename.
|
|
153
|
+
|
|
154
|
+
- Updated dependencies [50e5f5f]
|
|
155
|
+
- @checkstack/backend-api@0.15.0
|
|
156
|
+
- @checkstack/catalog-backend@1.0.2
|
|
157
|
+
- @checkstack/catalog-common@2.0.1
|
|
158
|
+
- @checkstack/common@0.8.0
|
|
159
|
+
- @checkstack/dependency-common@1.0.1
|
|
160
|
+
- @checkstack/healthcheck-backend@1.0.3
|
|
161
|
+
- @checkstack/integration-common@0.3.1
|
|
162
|
+
- @checkstack/queue-api@0.2.18
|
|
163
|
+
- @checkstack/slo-common@0.3.2
|
|
164
|
+
- @checkstack/cache-api@0.2.4
|
|
165
|
+
- @checkstack/cache-utils@0.2.4
|
|
166
|
+
- @checkstack/command-backend@0.1.24
|
|
167
|
+
- @checkstack/healthcheck-common@1.0.1
|
|
168
|
+
- @checkstack/integration-backend@0.1.24
|
|
169
|
+
- @checkstack/signal-common@0.2.1
|
|
170
|
+
|
|
3
171
|
## 0.3.4
|
|
4
172
|
|
|
5
173
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@checkstack/slo-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"license": "Elastic-2.0",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "src/index.ts",
|
|
6
7
|
"checkstack": {
|
|
@@ -13,30 +14,32 @@
|
|
|
13
14
|
"lint:code": "eslint . --max-warnings 0"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
|
-
"@checkstack/backend-api": "0.
|
|
17
|
-
"@checkstack/cache-api": "0.2.
|
|
18
|
-
"@checkstack/cache-utils": "0.2.
|
|
19
|
-
"@checkstack/slo-common": "0.3.
|
|
20
|
-
"@checkstack/healthcheck-common": "1.0.
|
|
21
|
-
"@checkstack/healthcheck-backend": "1.0.
|
|
22
|
-
"@checkstack/dependency-common": "1.0.
|
|
23
|
-
"@checkstack/catalog-common": "2.0.
|
|
24
|
-
"@checkstack/catalog-backend": "1.0.
|
|
25
|
-
"@checkstack/command-backend": "0.1.
|
|
26
|
-
"@checkstack/signal-common": "0.2.
|
|
27
|
-
"@checkstack/integration-backend": "0.1.
|
|
28
|
-
"@checkstack/integration-common": "0.3.
|
|
29
|
-
"@checkstack/
|
|
30
|
-
"@checkstack/
|
|
17
|
+
"@checkstack/backend-api": "0.15.0",
|
|
18
|
+
"@checkstack/cache-api": "0.2.4",
|
|
19
|
+
"@checkstack/cache-utils": "0.2.4",
|
|
20
|
+
"@checkstack/slo-common": "0.3.2",
|
|
21
|
+
"@checkstack/healthcheck-common": "1.0.1",
|
|
22
|
+
"@checkstack/healthcheck-backend": "1.0.3",
|
|
23
|
+
"@checkstack/dependency-common": "1.0.1",
|
|
24
|
+
"@checkstack/catalog-common": "2.0.1",
|
|
25
|
+
"@checkstack/catalog-backend": "1.0.2",
|
|
26
|
+
"@checkstack/command-backend": "0.1.24",
|
|
27
|
+
"@checkstack/signal-common": "0.2.1",
|
|
28
|
+
"@checkstack/integration-backend": "0.1.24",
|
|
29
|
+
"@checkstack/integration-common": "0.3.1",
|
|
30
|
+
"@checkstack/gitops-backend": "0.2.8",
|
|
31
|
+
"@checkstack/gitops-common": "0.2.2",
|
|
32
|
+
"@checkstack/common": "0.8.0",
|
|
33
|
+
"@checkstack/queue-api": "0.2.18",
|
|
31
34
|
"drizzle-orm": "^0.45.0",
|
|
32
35
|
"zod": "^4.2.1",
|
|
33
36
|
"@orpc/server": "^1.13.2"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
"@checkstack/drizzle-helper": "0.0.
|
|
37
|
-
"@checkstack/scripts": "0.
|
|
38
|
-
"@checkstack/test-utils-backend": "0.1.
|
|
39
|
-
"@checkstack/tsconfig": "0.0.
|
|
39
|
+
"@checkstack/drizzle-helper": "0.0.5",
|
|
40
|
+
"@checkstack/scripts": "0.3.0",
|
|
41
|
+
"@checkstack/test-utils-backend": "0.1.24",
|
|
42
|
+
"@checkstack/tsconfig": "0.0.7",
|
|
40
43
|
"@types/bun": "^1.0.0",
|
|
41
44
|
"drizzle-kit": "^0.31.10",
|
|
42
45
|
"typescript": "^5.0.0"
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,8 @@ import { sloHooks } from "./hooks";
|
|
|
24
24
|
import { setupDailySnapshotJob } from "./streak-calculator";
|
|
25
25
|
import { setupWeeklyDigestJob } from "./weekly-digest";
|
|
26
26
|
import { evaluateAchievements } from "./achievement-evaluator";
|
|
27
|
+
import { entityKindExtensionPoint } from "@checkstack/gitops-backend";
|
|
28
|
+
import { registerSloGitOpsKinds } from "./slo-gitops-kinds";
|
|
27
29
|
|
|
28
30
|
// =============================================================================
|
|
29
31
|
// Integration Event Payload Schemas
|
|
@@ -168,6 +170,17 @@ export default createBackendPlugin({
|
|
|
168
170
|
|
|
169
171
|
// Shared references across init/afterPluginsReady (maintenance-backend pattern)
|
|
170
172
|
let sharedEngine: SloEngine;
|
|
173
|
+
let gitopsService: SloService | undefined;
|
|
174
|
+
|
|
175
|
+
// ─── GitOps Entity Kind Registration ─────────────────────────────
|
|
176
|
+
const kindRegistry = env.getExtensionPoint(entityKindExtensionPoint);
|
|
177
|
+
registerSloGitOpsKinds({
|
|
178
|
+
kindRegistry,
|
|
179
|
+
getService: () => {
|
|
180
|
+
if (!gitopsService) throw new Error("SloService not initialized");
|
|
181
|
+
return gitopsService;
|
|
182
|
+
},
|
|
183
|
+
});
|
|
171
184
|
|
|
172
185
|
env.registerInit({
|
|
173
186
|
schema,
|
|
@@ -190,6 +203,7 @@ export default createBackendPlugin({
|
|
|
190
203
|
logger.debug("🔧 Initializing SLO Backend...");
|
|
191
204
|
|
|
192
205
|
const service = new SloService(database as SafeDatabase<typeof schema>);
|
|
206
|
+
gitopsService = service;
|
|
193
207
|
const engine = new SloEngine({
|
|
194
208
|
service,
|
|
195
209
|
signalService,
|
package/src/router.ts
CHANGED
|
@@ -53,7 +53,7 @@ export function createRouter({
|
|
|
53
53
|
cache.wrapObjective(input.id, async () => {
|
|
54
54
|
const objective = await service.getObjective({ id: input.id });
|
|
55
55
|
if (!objective) {
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
return null;
|
|
58
58
|
}
|
|
59
59
|
const status = await engine.computeStatus({ objective });
|
package/src/service.ts
CHANGED
|
@@ -72,7 +72,7 @@ export class SloService {
|
|
|
72
72
|
await this.db.insert(sloObjectives).values({
|
|
73
73
|
id,
|
|
74
74
|
systemId: input.systemId,
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
healthCheckConfigurationId: input.healthCheckConfigurationId ?? null,
|
|
77
77
|
target: input.target,
|
|
78
78
|
windowDays: input.windowDays,
|
|
@@ -184,9 +184,9 @@ export class SloService {
|
|
|
184
184
|
systemId,
|
|
185
185
|
startTime: now,
|
|
186
186
|
attributionType,
|
|
187
|
-
|
|
187
|
+
|
|
188
188
|
upstreamSystemId: upstreamSystemId ?? null,
|
|
189
|
-
|
|
189
|
+
|
|
190
190
|
upstreamSystemName: upstreamSystemName ?? null,
|
|
191
191
|
});
|
|
192
192
|
|
|
@@ -480,7 +480,7 @@ export class SloService {
|
|
|
480
480
|
currentStreak: newCurrent,
|
|
481
481
|
bestStreak: newBest,
|
|
482
482
|
streakStart: streak.streakStart ?? now,
|
|
483
|
-
|
|
483
|
+
|
|
484
484
|
bestStreakEnd: newCurrent > streak.bestStreak ? null : streak.bestStreakEnd,
|
|
485
485
|
})
|
|
486
486
|
.where(eq(sloStreaks.objectiveId, objectiveId));
|
|
@@ -499,7 +499,7 @@ export class SloService {
|
|
|
499
499
|
const now = new Date();
|
|
500
500
|
const updateData: Partial<typeof sloStreaks.$inferInsert> = {
|
|
501
501
|
currentStreak: 0,
|
|
502
|
-
|
|
502
|
+
|
|
503
503
|
streakStart: null,
|
|
504
504
|
};
|
|
505
505
|
|
package/src/slo-engine.ts
CHANGED
|
@@ -325,13 +325,13 @@ export class SloEngine {
|
|
|
325
325
|
const effectiveAvailability =
|
|
326
326
|
totalWindowMinutes > 0
|
|
327
327
|
? ((totalWindowMinutes - consumedMinutes) / totalWindowMinutes) * 100
|
|
328
|
-
|
|
328
|
+
|
|
329
329
|
: null;
|
|
330
330
|
|
|
331
331
|
const strictAvailability =
|
|
332
332
|
totalWindowMinutes > 0
|
|
333
333
|
? ((totalWindowMinutes - downtime.totalMinutes) / totalWindowMinutes) * 100
|
|
334
|
-
|
|
334
|
+
|
|
335
335
|
: null;
|
|
336
336
|
|
|
337
337
|
// Burn rate: how fast are we consuming budget relative to the window?
|
|
@@ -342,7 +342,7 @@ export class SloEngine {
|
|
|
342
342
|
const expectedConsumption =
|
|
343
343
|
(elapsedDays / objective.windowDays) * allowedDowntimeMinutes;
|
|
344
344
|
const burnRate =
|
|
345
|
-
|
|
345
|
+
|
|
346
346
|
expectedConsumption > 0 ? consumedMinutes / expectedConsumption : null;
|
|
347
347
|
|
|
348
348
|
// Check for open downtime events
|
|
@@ -364,7 +364,7 @@ export class SloEngine {
|
|
|
364
364
|
target: objective.target,
|
|
365
365
|
windowDays: objective.windowDays,
|
|
366
366
|
healthCheckConfigurationId: objective.healthCheckConfigurationId,
|
|
367
|
-
|
|
367
|
+
|
|
368
368
|
healthCheckConfigurationName: null,
|
|
369
369
|
currentAvailability: effectiveAvailability,
|
|
370
370
|
strictAvailability,
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { describe, it, expect, mock, beforeEach } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
CHECKSTACK_API_VERSION,
|
|
4
|
+
type ReconcileContext,
|
|
5
|
+
} from "@checkstack/gitops-common";
|
|
6
|
+
import type { SloService } from "./service";
|
|
7
|
+
import type { SloObjective } from "@checkstack/slo-common";
|
|
8
|
+
import { buildSloKind } from "./slo-gitops-kinds";
|
|
9
|
+
|
|
10
|
+
const noopLogger = {
|
|
11
|
+
debug: () => {},
|
|
12
|
+
info: () => {},
|
|
13
|
+
warn: () => {},
|
|
14
|
+
error: () => {},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const buildContext = (
|
|
18
|
+
overrides: Partial<ReconcileContext> = {},
|
|
19
|
+
): ReconcileContext =>
|
|
20
|
+
({
|
|
21
|
+
logger: noopLogger,
|
|
22
|
+
resolveEntityRef: async () => undefined,
|
|
23
|
+
resolveSecretsBySchema: async ({ value }) => ({
|
|
24
|
+
resolved: value,
|
|
25
|
+
warnings: [],
|
|
26
|
+
}),
|
|
27
|
+
...overrides,
|
|
28
|
+
}) as ReconcileContext;
|
|
29
|
+
|
|
30
|
+
const stubService = () => {
|
|
31
|
+
const created: SloObjective = {
|
|
32
|
+
id: "slo-1",
|
|
33
|
+
systemId: "sys-1",
|
|
34
|
+
healthCheckConfigurationId: null,
|
|
35
|
+
target: 99.9,
|
|
36
|
+
windowDays: 30,
|
|
37
|
+
dependencyExclusion: "strict",
|
|
38
|
+
excludedDependencyIds: [],
|
|
39
|
+
burnRateThresholds: {
|
|
40
|
+
warningPercent: 50,
|
|
41
|
+
criticalPercent: 80,
|
|
42
|
+
fastBurnMultiplier: 5,
|
|
43
|
+
},
|
|
44
|
+
createdAt: new Date(),
|
|
45
|
+
updatedAt: new Date(),
|
|
46
|
+
};
|
|
47
|
+
const createObjective = mock(async () => created);
|
|
48
|
+
const updateObjective = mock(async () => created);
|
|
49
|
+
const deleteObjective = mock(async () => true);
|
|
50
|
+
return {
|
|
51
|
+
created,
|
|
52
|
+
createObjective,
|
|
53
|
+
updateObjective,
|
|
54
|
+
deleteObjective,
|
|
55
|
+
service: {
|
|
56
|
+
createObjective,
|
|
57
|
+
updateObjective,
|
|
58
|
+
deleteObjective,
|
|
59
|
+
} as unknown as SloService,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const refResolver =
|
|
64
|
+
(table: Record<string, string>) =>
|
|
65
|
+
async ({ entityName }: { kind: string; entityName: string }) =>
|
|
66
|
+
table[entityName];
|
|
67
|
+
|
|
68
|
+
const mkEntity = <TSpec>(name: string, spec: TSpec) => ({
|
|
69
|
+
apiVersion: CHECKSTACK_API_VERSION,
|
|
70
|
+
kind: "SLO",
|
|
71
|
+
metadata: { name },
|
|
72
|
+
spec,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("buildSloKind", () => {
|
|
76
|
+
let stub: ReturnType<typeof stubService>;
|
|
77
|
+
let kind: ReturnType<typeof buildSloKind>;
|
|
78
|
+
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
stub = stubService();
|
|
81
|
+
kind = buildSloKind({ getService: () => stub.service });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("registers under SLO kind", () => {
|
|
85
|
+
expect(kind.kind).toBe("SLO");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("creates an objective when no existing entity id is supplied", async () => {
|
|
89
|
+
const result = await kind.reconcile({
|
|
90
|
+
entity: mkEntity("payments-availability", {
|
|
91
|
+
systemRef: { kind: "System", name: "payments-api" },
|
|
92
|
+
target: 99.9,
|
|
93
|
+
windowDays: 30,
|
|
94
|
+
}),
|
|
95
|
+
context: buildContext({
|
|
96
|
+
resolveEntityRef: refResolver({ "payments-api": "sys-1" }),
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(result.entityId).toBe("slo-1");
|
|
101
|
+
expect(stub.createObjective).toHaveBeenCalledTimes(1);
|
|
102
|
+
expect(stub.createObjective).toHaveBeenCalledWith({
|
|
103
|
+
input: {
|
|
104
|
+
systemId: "sys-1",
|
|
105
|
+
healthCheckConfigurationId: undefined,
|
|
106
|
+
target: 99.9,
|
|
107
|
+
windowDays: 30,
|
|
108
|
+
dependencyExclusion: "strict",
|
|
109
|
+
excludedDependencyIds: [],
|
|
110
|
+
burnRateThresholds: {
|
|
111
|
+
warningPercent: 50,
|
|
112
|
+
criticalPercent: 80,
|
|
113
|
+
fastBurnMultiplier: 5,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("resolves an optional healthcheck ref into healthCheckConfigurationId", async () => {
|
|
120
|
+
await kind.reconcile({
|
|
121
|
+
entity: mkEntity("payments-http-availability", {
|
|
122
|
+
systemRef: { kind: "System", name: "payments-api" },
|
|
123
|
+
healthcheckRef: { kind: "Healthcheck", name: "payments-http" },
|
|
124
|
+
target: 99.95,
|
|
125
|
+
windowDays: 7,
|
|
126
|
+
}),
|
|
127
|
+
context: buildContext({
|
|
128
|
+
resolveEntityRef: refResolver({
|
|
129
|
+
"payments-api": "sys-1",
|
|
130
|
+
"payments-http": "hc-1",
|
|
131
|
+
}),
|
|
132
|
+
}),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(stub.createObjective).toHaveBeenCalledWith({
|
|
136
|
+
input: expect.objectContaining({
|
|
137
|
+
systemId: "sys-1",
|
|
138
|
+
healthCheckConfigurationId: "hc-1",
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("resolves excluded dependency refs to system ids", async () => {
|
|
144
|
+
await kind.reconcile({
|
|
145
|
+
entity: mkEntity("core", {
|
|
146
|
+
systemRef: { kind: "System", name: "core" },
|
|
147
|
+
target: 99.9,
|
|
148
|
+
windowDays: 30,
|
|
149
|
+
dependencyExclusion: "self-only",
|
|
150
|
+
excludedDependencyRefs: [
|
|
151
|
+
{ kind: "System", name: "third-party-payments" },
|
|
152
|
+
{ kind: "System", name: "third-party-email" },
|
|
153
|
+
],
|
|
154
|
+
}),
|
|
155
|
+
context: buildContext({
|
|
156
|
+
resolveEntityRef: refResolver({
|
|
157
|
+
core: "sys-core",
|
|
158
|
+
"third-party-payments": "sys-pay",
|
|
159
|
+
"third-party-email": "sys-mail",
|
|
160
|
+
}),
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(stub.createObjective).toHaveBeenCalledWith({
|
|
165
|
+
input: expect.objectContaining({
|
|
166
|
+
excludedDependencyIds: ["sys-pay", "sys-mail"],
|
|
167
|
+
dependencyExclusion: "self-only",
|
|
168
|
+
}),
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("updates an existing objective when existingEntityId is supplied", async () => {
|
|
173
|
+
await kind.reconcile({
|
|
174
|
+
entity: mkEntity("payments", {
|
|
175
|
+
systemRef: { kind: "System", name: "payments-api" },
|
|
176
|
+
target: 99.95,
|
|
177
|
+
windowDays: 30,
|
|
178
|
+
}),
|
|
179
|
+
existingEntityId: "slo-existing",
|
|
180
|
+
context: buildContext({
|
|
181
|
+
resolveEntityRef: refResolver({ "payments-api": "sys-1" }),
|
|
182
|
+
}),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(stub.createObjective).not.toHaveBeenCalled();
|
|
186
|
+
expect(stub.updateObjective).toHaveBeenCalledWith({
|
|
187
|
+
input: expect.objectContaining({
|
|
188
|
+
id: "slo-existing",
|
|
189
|
+
target: 99.95,
|
|
190
|
+
windowDays: 30,
|
|
191
|
+
}),
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("treats pending-* entityIds as fresh creates", async () => {
|
|
196
|
+
await kind.reconcile({
|
|
197
|
+
entity: mkEntity("payments", {
|
|
198
|
+
systemRef: { kind: "System", name: "payments-api" },
|
|
199
|
+
target: 99.9,
|
|
200
|
+
windowDays: 30,
|
|
201
|
+
}),
|
|
202
|
+
existingEntityId: "pending-abc",
|
|
203
|
+
context: buildContext({
|
|
204
|
+
resolveEntityRef: refResolver({ "payments-api": "sys-1" }),
|
|
205
|
+
}),
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
expect(stub.createObjective).toHaveBeenCalledTimes(1);
|
|
209
|
+
expect(stub.updateObjective).not.toHaveBeenCalled();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("throws when systemRef cannot be resolved", async () => {
|
|
213
|
+
await expect(
|
|
214
|
+
kind.reconcile({
|
|
215
|
+
entity: mkEntity("broken", {
|
|
216
|
+
systemRef: { kind: "System", name: "nope" },
|
|
217
|
+
target: 99.9,
|
|
218
|
+
windowDays: 30,
|
|
219
|
+
}),
|
|
220
|
+
context: buildContext({ resolveEntityRef: async () => undefined }),
|
|
221
|
+
}),
|
|
222
|
+
).rejects.toThrow(/Cannot resolve system ref "nope"/);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("delegates delete to the service", async () => {
|
|
226
|
+
await kind.delete!({
|
|
227
|
+
entityName: "payments",
|
|
228
|
+
entityId: "slo-1",
|
|
229
|
+
context: buildContext(),
|
|
230
|
+
});
|
|
231
|
+
expect(stub.deleteObjective).toHaveBeenCalledWith({ id: "slo-1" });
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("delete is a no-op when entityId is missing", async () => {
|
|
235
|
+
await kind.delete!({ entityName: "x", context: buildContext() });
|
|
236
|
+
expect(stub.deleteObjective).not.toHaveBeenCalled();
|
|
237
|
+
});
|
|
238
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
CHECKSTACK_API_VERSION,
|
|
4
|
+
entityRefSchema,
|
|
5
|
+
type EntityKindDefinition,
|
|
6
|
+
type EntityKindRegistry,
|
|
7
|
+
type ReconcileContext,
|
|
8
|
+
} from "@checkstack/gitops-common";
|
|
9
|
+
import {
|
|
10
|
+
DependencyExclusionModeSchema,
|
|
11
|
+
BurnRateThresholdsSchema,
|
|
12
|
+
} from "@checkstack/slo-common";
|
|
13
|
+
import type { SloService } from "./service";
|
|
14
|
+
|
|
15
|
+
interface SloGitOpsKindsDeps {
|
|
16
|
+
/** Lazy accessor — populated during init(), invoked at reconcile time. */
|
|
17
|
+
getService: () => SloService;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ─── SLO Spec Schema ───────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* GitOps spec for `kind: SLO`.
|
|
24
|
+
*
|
|
25
|
+
* An SLO targets a specific system (required) and may optionally narrow to a
|
|
26
|
+
* single healthcheck on that system. When `healthcheckRef` is omitted the SLO
|
|
27
|
+
* covers the system's aggregate availability across all of its healthchecks.
|
|
28
|
+
*/
|
|
29
|
+
const sloSpecSchema = z.object({
|
|
30
|
+
systemRef: entityRefSchema,
|
|
31
|
+
healthcheckRef: entityRefSchema.optional(),
|
|
32
|
+
target: z.number().min(0).max(100),
|
|
33
|
+
windowDays: z.number().int().positive(),
|
|
34
|
+
dependencyExclusion: DependencyExclusionModeSchema.optional(),
|
|
35
|
+
excludedDependencyRefs: z.array(entityRefSchema).optional(),
|
|
36
|
+
burnRateThresholds: BurnRateThresholdsSchema.optional(),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
type SloSpec = z.infer<typeof sloSpecSchema>;
|
|
40
|
+
|
|
41
|
+
// ─── Kind builder ───────────────────────────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
const resolveRequiredRef = async ({
|
|
44
|
+
context,
|
|
45
|
+
ref,
|
|
46
|
+
refKindLabel,
|
|
47
|
+
}: {
|
|
48
|
+
context: ReconcileContext;
|
|
49
|
+
ref: { kind: string; name: string };
|
|
50
|
+
refKindLabel: string;
|
|
51
|
+
}): Promise<string> => {
|
|
52
|
+
const id = await context.resolveEntityRef({
|
|
53
|
+
kind: ref.kind,
|
|
54
|
+
entityName: ref.name,
|
|
55
|
+
});
|
|
56
|
+
if (!id) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Cannot resolve ${refKindLabel} ref "${ref.name}" (kind: ${ref.kind}) — ensure the entity exists`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return id;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function buildSloKind(
|
|
65
|
+
deps: SloGitOpsKindsDeps,
|
|
66
|
+
): EntityKindDefinition<SloSpec> {
|
|
67
|
+
return {
|
|
68
|
+
apiVersion: CHECKSTACK_API_VERSION,
|
|
69
|
+
kind: "SLO",
|
|
70
|
+
specSchema: sloSpecSchema,
|
|
71
|
+
|
|
72
|
+
reconcile: async ({
|
|
73
|
+
entity,
|
|
74
|
+
existingEntityId,
|
|
75
|
+
context,
|
|
76
|
+
}: {
|
|
77
|
+
entity: {
|
|
78
|
+
metadata: { name: string; title?: string; description?: string };
|
|
79
|
+
spec: SloSpec;
|
|
80
|
+
};
|
|
81
|
+
existingEntityId?: string;
|
|
82
|
+
context: ReconcileContext;
|
|
83
|
+
}) => {
|
|
84
|
+
const { spec } = entity;
|
|
85
|
+
const service = deps.getService();
|
|
86
|
+
|
|
87
|
+
const systemId = await resolveRequiredRef({
|
|
88
|
+
context,
|
|
89
|
+
ref: spec.systemRef,
|
|
90
|
+
refKindLabel: "system",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
let healthCheckConfigurationId: string | undefined;
|
|
94
|
+
if (spec.healthcheckRef) {
|
|
95
|
+
healthCheckConfigurationId = await resolveRequiredRef({
|
|
96
|
+
context,
|
|
97
|
+
ref: spec.healthcheckRef,
|
|
98
|
+
refKindLabel: "healthcheck",
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const excludedDependencyIds = spec.excludedDependencyRefs
|
|
103
|
+
? await Promise.all(
|
|
104
|
+
spec.excludedDependencyRefs.map((ref) =>
|
|
105
|
+
resolveRequiredRef({
|
|
106
|
+
context,
|
|
107
|
+
ref,
|
|
108
|
+
refKindLabel: "excluded dependency",
|
|
109
|
+
}),
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
: [];
|
|
113
|
+
|
|
114
|
+
if (existingEntityId && !existingEntityId.startsWith("pending-")) {
|
|
115
|
+
await service.updateObjective({
|
|
116
|
+
input: {
|
|
117
|
+
id: existingEntityId,
|
|
118
|
+
target: spec.target,
|
|
119
|
+
windowDays: spec.windowDays,
|
|
120
|
+
dependencyExclusion: spec.dependencyExclusion,
|
|
121
|
+
excludedDependencyIds,
|
|
122
|
+
burnRateThresholds: spec.burnRateThresholds,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
context.logger.info(
|
|
126
|
+
`GitOps: updated SLO "${entity.metadata.name}" (id: ${existingEntityId})`,
|
|
127
|
+
);
|
|
128
|
+
return { entityId: existingEntityId };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const created = await service.createObjective({
|
|
132
|
+
input: {
|
|
133
|
+
systemId,
|
|
134
|
+
healthCheckConfigurationId,
|
|
135
|
+
target: spec.target,
|
|
136
|
+
windowDays: spec.windowDays,
|
|
137
|
+
dependencyExclusion: spec.dependencyExclusion ?? "strict",
|
|
138
|
+
excludedDependencyIds,
|
|
139
|
+
burnRateThresholds: spec.burnRateThresholds ?? {
|
|
140
|
+
warningPercent: 50,
|
|
141
|
+
criticalPercent: 80,
|
|
142
|
+
fastBurnMultiplier: 5,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
context.logger.info(
|
|
147
|
+
`GitOps: created SLO "${entity.metadata.name}" (id: ${created.id})`,
|
|
148
|
+
);
|
|
149
|
+
return { entityId: created.id };
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
delete: async ({
|
|
153
|
+
entityId,
|
|
154
|
+
context,
|
|
155
|
+
}: {
|
|
156
|
+
entityName: string;
|
|
157
|
+
entityId?: string;
|
|
158
|
+
context: ReconcileContext;
|
|
159
|
+
}) => {
|
|
160
|
+
if (!entityId) return;
|
|
161
|
+
await deps.getService().deleteObjective({ id: entityId });
|
|
162
|
+
context.logger.info(`GitOps: deleted SLO (id: ${entityId})`);
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function registerSloGitOpsKinds({
|
|
168
|
+
kindRegistry,
|
|
169
|
+
...deps
|
|
170
|
+
}: SloGitOpsKindsDeps & {
|
|
171
|
+
kindRegistry: EntityKindRegistry;
|
|
172
|
+
}): void {
|
|
173
|
+
kindRegistry.registerKind(buildSloKind(deps));
|
|
174
|
+
}
|
package/src/streak-calculator.ts
CHANGED
|
@@ -79,7 +79,7 @@ export async function runDailySnapshotJob(deps: {
|
|
|
79
79
|
availabilityPercent: status.currentAvailability ?? 100,
|
|
80
80
|
budgetConsumedMinutes: status.errorBudgetConsumedMinutes,
|
|
81
81
|
budgetRemainingPercent: status.errorBudgetRemainingPercent,
|
|
82
|
-
|
|
82
|
+
|
|
83
83
|
burnRate: status.burnRate ?? null,
|
|
84
84
|
streakDays: streak?.currentStreak ?? 0,
|
|
85
85
|
},
|