Repository navigation
feat(alerts): silences match an anomaly incident's service and environment #2319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4272aa6
feat(alerts): anomalies page through alert rules
Makisuo 816b9ce
Merge remote-tracking branch 'origin/main' into feat/alerts-unified-i…
Makisuo b518097
feat(alerts): silences match an anomaly incident's service and enviro…
Makisuo d32578e
fix(alerts): review fixes for anomaly rules
Makisuo ba6f119
style(alerts): format review fixes
Makisuo 67a2f17
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo 5cd792c
fix(alerts): only Maple creates the default anomaly rule
Makisuo 8085b14
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo 1351130
Merge remote-tracking branch 'origin/main' into feat/alerts-unified-i…
Makisuo ce36af7
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo d4fd896
test(alerts): scope the anomaly silences test rule now that only Mapl…
Makisuo 0901897
docs(domain): keep the hold reason comment on its schema
Makisuo 720e8cf
fix(alerts): templates show an anomaly's severity as a word
Makisuo 1d1e149
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo c62cebf
fix(alerts): the default anomaly rule message names actions that exist
Makisuo e6381ad
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo 68df751
fix(alerts): anomaly rule test runs and MCP text
Makisuo 6b79565
fix(alerts): default rule message says scoped rules still page
Makisuo 54e709a
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo adb588e
Merge remote-tracking branch 'origin/main' into feat/alerts-unified-i…
Makisuo 48582b6
chore(db): merge the anomaly rules and escalation lease migrations
Makisuo b362bef
Merge branch 'feat/alerts-unified-incidents' into feat/alerts-inciden…
Makisuo e40ff81
chore(db): merge the incident labels migration with main
Makisuo bd86ddc
fix(alerts): anomaly rules record that they ran
Makisuo 6204940
fix(alerts): unrouted anomaly tiers never push; disabling detection r…
Makisuo 97db19f
Merge remote-tracking branch 'origin/feat/alerts-unified-incidents' i…
Makisuo 6b56451
Merge remote-tracking branch 'origin/main' into feat/alerts-incident-…
Makisuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
packages/backend/src/services/alerts/AlertsService.anomaly-silences.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import { afterEach, assert, describe, expect, it } from "@effect/vitest" | ||
| import * as PG from "@maple-dev/effect-orm/postgres" | ||
| import { AnomalyIncidents } from "@maple/db/tables" | ||
| import { | ||
| AlertRuleUpsertRequest, | ||
| AlertSilenceCreateRequest, | ||
| AnomalyIncidentId, | ||
| IsoDateTimeString, | ||
| type AlertSilenceMatchers, | ||
| } from "@maple/domain/http" | ||
| import { Array as Arr, Clock, Duration, Effect, Layer, Schema } from "effect" | ||
| import { TestClock } from "effect/testing" | ||
| import { Database } from "@maple/backend/platform/DatabaseLive" | ||
| import { cleanupTestDbs, createTestDb, type TestDb } from "@maple/backend/platform/test-pglite" | ||
| import { AlertsService, type AlertsServiceApi } from "./AlertsService" | ||
| import { AlertSilencesService } from "./AlertSilencesService" | ||
| import { AnomalyAlertSource } from "./anomaly/AnomalyAlertSource" | ||
| import { | ||
| DEFAULT_CLOCK_EPOCH_MS, | ||
| adminRoles, | ||
| asOrgId, | ||
| asUserId, | ||
| createWebhookDestination, | ||
| makeLayer, | ||
| makeWarehouseStub, | ||
| } from "./alerts-service-test-support" | ||
|
|
||
| const trackedDbs: TestDb[] = [] | ||
| afterEach(() => cleanupTestDbs(trackedDbs)) | ||
|
|
||
| const orgId = asOrgId("org_anomaly_silences") | ||
| const userId = asUserId("user_anomaly_silences") | ||
| const anomalyId = Schema.decodeSync(AnomalyIncidentId)("00000000-0000-4000-8000-00000000b001") | ||
| const iso = Schema.decodeUnknownSync(IsoDateTimeString) | ||
|
|
||
| const harness = () => { | ||
| const testDb = createTestDb(trackedDbs) | ||
| const sent: Array<string> = [] | ||
| const fetchImpl: typeof fetch = async (_input, init) => { | ||
| sent.push(new Headers(init?.headers).get("x-maple-event-type") ?? "") | ||
| return new Response("ok", { status: 200 }) | ||
| } | ||
| const layer = Layer.mergeAll( | ||
| makeLayer(testDb, makeWarehouseStub({}), { fetch: fetchImpl }), | ||
| AnomalyAlertSource.layer.pipe(Layer.provide(testDb.layer)), | ||
| testDb.layer, | ||
| ) | ||
| return { sent, layer } | ||
| } | ||
|
|
||
| /** An open critical error-rate anomaly in checkout (production). */ | ||
| const insertCheckoutAnomaly = Effect.gen(function* () { | ||
| const database = yield* Database | ||
| const now = yield* Clock.currentTimeMillis | ||
| yield* database.execute((db) => | ||
| db.run( | ||
| PG.insertInto(AnomalyIncidents).values({ | ||
| id: anomalyId, | ||
| orgId, | ||
| detectorKey: "error_rate:production:checkout", | ||
| signalType: "error_rate", | ||
| serviceName: "checkout", | ||
| deploymentEnv: "production", | ||
| fingerprintHash: null, | ||
| errorIssueId: null, | ||
| status: "open", | ||
| severity: "critical", | ||
| openedValue: 0.042, | ||
| baselineMedian: 0.003, | ||
| baselineSigma: 0.001, | ||
| thresholdValue: 0.006, | ||
| lastObservedValue: 0.042, | ||
| firstTriggeredAt: now, | ||
| lastTriggeredAt: now, | ||
| resolvedAt: null, | ||
| resolveReason: null, | ||
| dedupeKey: `dedupe-${anomalyId}`, | ||
| createdAt: now, | ||
| updatedAt: now, | ||
| }), | ||
| ), | ||
| ) | ||
| }).pipe(Effect.orDie) | ||
|
|
||
| /** | ||
| * An anomaly rule over every service in production, whose critical tier pages a webhook. Only Maple | ||
| * creates the fully unscoped default, so the environment scope stands in for it here. | ||
| */ | ||
| const createUnscopedAnomalyRule = (alerts: AlertsServiceApi) => | ||
| Effect.gen(function* () { | ||
| const destination = yield* createWebhookDestination(alerts, orgId, userId) | ||
| return yield* alerts.createRule( | ||
| orgId, | ||
| userId, | ||
| adminRoles, | ||
| new AlertRuleUpsertRequest({ | ||
| name: "Production anomalies", | ||
| enabled: true, | ||
| signalType: "anomaly", | ||
| environments: ["production"], | ||
| comparator: "gte", | ||
| windowMinutes: 5, | ||
| thresholds: [ | ||
| { severity: "warning", value: 1, upper: null, recoveryValue: null, destinationIds: [] }, | ||
| { | ||
| severity: "critical", | ||
| value: 2, | ||
| upper: null, | ||
| recoveryValue: null, | ||
| destinationIds: [destination.id], | ||
| }, | ||
| ], | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| const silence = (matchers: AlertSilenceMatchers) => | ||
| Effect.gen(function* () { | ||
| const silences = yield* AlertSilencesService | ||
| const now = yield* Clock.currentTimeMillis | ||
| return yield* silences.create( | ||
| orgId, | ||
| userId, | ||
| adminRoles, | ||
| new AlertSilenceCreateRequest({ | ||
| reason: "checkout maintenance", | ||
| matchers, | ||
| endsAt: iso(new Date(now + 60 * 60_000).toISOString()), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| const ticks = (alerts: AlertsServiceApi, count: number) => | ||
| Effect.forEach( | ||
| Arr.range(1, count), | ||
| () => | ||
| Effect.gen(function* () { | ||
| yield* TestClock.adjust(Duration.minutes(1)) | ||
| yield* alerts.runSchedulerTick() | ||
| }), | ||
| { discard: true }, | ||
| ) | ||
|
|
||
| const deliveries = (alerts: AlertsServiceApi) => | ||
| alerts | ||
| .listDeliveryEvents(orgId) | ||
| .pipe( | ||
| Effect.map((page) => | ||
| Arr.map(Arr.reverse(page.events), (event) => `${event.eventType}:${event.status}`), | ||
| ), | ||
| ) | ||
|
|
||
| describe("alert silences on anomaly rules", () => { | ||
| it.effect("a service silence holds back the page for an anomaly in that service", () => { | ||
| const { sent, layer } = harness() | ||
| return Effect.gen(function* () { | ||
| yield* TestClock.setTime(DEFAULT_CLOCK_EPOCH_MS) | ||
| const alerts = yield* AlertsService | ||
| yield* createUnscopedAnomalyRule(alerts) | ||
| yield* insertCheckoutAnomaly | ||
| const created = yield* silence({ services: ["checkout"] }) | ||
|
|
||
| yield* ticks(alerts, 1) | ||
|
|
||
| const [incident] = (yield* alerts.listIncidents(orgId)).incidents | ||
| expect(incident).toMatchObject({ | ||
| status: "open", | ||
| severity: "critical", | ||
| labels: { service: "checkout", environment: "production" }, | ||
| silenceId: created.id, | ||
| }) | ||
| assert.isNotNull(incident?.silencedAt) | ||
| assert.deepStrictEqual(yield* deliveries(alerts), ["trigger:silenced"]) | ||
| assert.deepStrictEqual(sent, []) | ||
| }).pipe(Effect.provide(layer)) | ||
| }) | ||
|
|
||
| it.effect("a silence on another service leaves the anomaly paging", () => { | ||
| const { sent, layer } = harness() | ||
| return Effect.gen(function* () { | ||
| yield* TestClock.setTime(DEFAULT_CLOCK_EPOCH_MS) | ||
| const alerts = yield* AlertsService | ||
| yield* createUnscopedAnomalyRule(alerts) | ||
| yield* insertCheckoutAnomaly | ||
| yield* silence({ services: ["payments"] }) | ||
|
|
||
| yield* ticks(alerts, 1) | ||
|
|
||
| const [incident] = (yield* alerts.listIncidents(orgId)).incidents | ||
| assert.isNull(incident?.silenceId) | ||
| assert.deepStrictEqual(yield* deliveries(alerts), ["trigger:success"]) | ||
| assert.deepStrictEqual(sent, ["trigger"]) | ||
| }).pipe(Effect.provide(layer)) | ||
| }) | ||
|
|
||
| it.effect("sends the held-back page once when the silence ends while the anomaly is open", () => { | ||
| const { sent, layer } = harness() | ||
| return Effect.gen(function* () { | ||
| yield* TestClock.setTime(DEFAULT_CLOCK_EPOCH_MS) | ||
| const alerts = yield* AlertsService | ||
| const silences = yield* AlertSilencesService | ||
| yield* createUnscopedAnomalyRule(alerts) | ||
| yield* insertCheckoutAnomaly | ||
| const created = yield* silence({ services: ["checkout"] }) | ||
| yield* ticks(alerts, 2) | ||
| assert.deepStrictEqual(sent, []) | ||
|
|
||
| yield* silences.end(orgId, userId, adminRoles, created.id) | ||
| yield* ticks(alerts, 3) | ||
|
|
||
| const [incident] = (yield* alerts.listIncidents(orgId)).incidents | ||
| assert.deepStrictEqual(yield* deliveries(alerts), ["trigger:silenced", "trigger:success"]) | ||
| assert.deepStrictEqual(sent, ["trigger"]) | ||
| assert.isNull(incident?.silencedAt) | ||
| assert.strictEqual(incident?.silenceId, created.id) | ||
| }).pipe(Effect.provide(layer)) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.