-
Notifications
You must be signed in to change notification settings - Fork 189
fix(notifications): fan out alarm destinations #520
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { | ||
| buildAlarmNotificationConfig, | ||
| buildAlarmNotificationTargets, | ||
| } from "../alarm-config"; | ||
|
|
||
| describe("buildAlarmNotificationTargets", () => { | ||
| test("keeps same-channel destinations as separate delivery targets", () => { | ||
| const firstSlack = "https://hooks.slack.com/services/T000/B000/first"; | ||
| const secondSlack = "https://hooks.slack.com/services/T000/B000/second"; | ||
|
|
||
| const targets = buildAlarmNotificationTargets([ | ||
| { type: "slack", identifier: firstSlack, config: {} }, | ||
| { type: "slack", identifier: secondSlack, config: {} }, | ||
| { | ||
| type: "webhook", | ||
| identifier: "https://example.com/alarm", | ||
| config: { | ||
| headers: { | ||
| authorization: "drop-me", | ||
| "X-Array": ["drop-me"], | ||
| "X-Alarm": "keep-me", | ||
| "X-Bad\r\nName": "drop-me", | ||
| "X-Bad-Value": "drop\r\nme", | ||
| }, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(targets.map((target) => target.channel)).toEqual([ | ||
| "slack", | ||
| "slack", | ||
| "webhook", | ||
| ]); | ||
| expect(targets[0]?.clientConfig.slack?.webhookUrl).toBe(firstSlack); | ||
| expect(targets[1]?.clientConfig.slack?.webhookUrl).toBe(secondSlack); | ||
| expect(targets[2]?.clientConfig.webhook).toEqual({ | ||
| url: "https://example.com/alarm", | ||
| headers: { "X-Alarm": "keep-me" }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildAlarmNotificationConfig", () => { | ||
| test("keeps legacy channels unique when duplicate destination types are provided", () => { | ||
| const firstSlack = "https://hooks.slack.com/services/T000/B000/first"; | ||
| const secondSlack = "https://hooks.slack.com/services/T000/B000/second"; | ||
|
|
||
| const config = buildAlarmNotificationConfig([ | ||
| { type: "slack", identifier: firstSlack, config: {} }, | ||
| { type: "slack", identifier: secondSlack, config: {} }, | ||
| { | ||
| type: "webhook", | ||
| identifier: "https://example.com/alarm", | ||
| config: {}, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(config.channels).toEqual(["slack", "webhook"]); | ||
| expect(config.clientConfig.slack?.webhookUrl).toBe(firstSlack); | ||
| expect(config.clientConfig.webhook?.url).toBe("https://example.com/alarm"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import type { NotificationClientConfig } from "./client"; | ||
| import type { NotificationChannel } from "./types"; | ||
|
|
||
| interface AlarmDestination { | ||
|
|
@@ -6,6 +7,11 @@ interface AlarmDestination { | |
| type: string; | ||
| } | ||
|
|
||
| export interface AlarmNotificationTarget { | ||
| channel: NotificationChannel; | ||
| clientConfig: NotificationClientConfig; | ||
| } | ||
|
|
||
| const FORBIDDEN_WEBHOOK_HEADERS = new Set([ | ||
| "authorization", | ||
| "content-length", | ||
|
|
@@ -56,9 +62,29 @@ function sanitizeWebhookHeaders( | |
| return Object.keys(out).length > 0 ? out : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Legacy adapter for callers that can only represent one provider per channel. | ||
| * Use buildAlarmNotificationTargets for per-destination fanout. | ||
| */ | ||
| export function buildAlarmNotificationConfig(destinations: AlarmDestination[]) { | ||
| const clientConfig: Record<string, Record<string, unknown>> = {}; | ||
| const channels: NotificationChannel[] = []; | ||
| const clientConfig: NotificationClientConfig = {}; | ||
| const channels = new Set<NotificationChannel>(); | ||
|
|
||
| for (const target of buildAlarmNotificationTargets(destinations)) { | ||
| if (channels.has(target.channel)) { | ||
| continue; | ||
| } | ||
| Object.assign(clientConfig, target.clientConfig); | ||
| channels.add(target.channel); | ||
| } | ||
|
|
||
| return { clientConfig, channels: Array.from(channels) }; | ||
| } | ||
|
Comment on lines
69
to
+82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The wrapper rebuilds |
||
|
|
||
| export function buildAlarmNotificationTargets( | ||
| destinations: AlarmDestination[] | ||
| ): AlarmNotificationTarget[] { | ||
| const targets: AlarmNotificationTarget[] = []; | ||
|
|
||
| for (const dest of destinations) { | ||
| const cfg = (dest.config ?? {}) as Record<string, unknown>; | ||
|
|
@@ -67,42 +93,55 @@ export function buildAlarmNotificationConfig(destinations: AlarmDestination[]) { | |
| if (!isAllowedSlackWebhook(dest.identifier)) { | ||
| continue; | ||
| } | ||
| clientConfig.slack = { webhookUrl: dest.identifier }; | ||
| channels.push("slack"); | ||
| targets.push({ | ||
| channel: "slack", | ||
| clientConfig: { slack: { webhookUrl: dest.identifier } }, | ||
| }); | ||
| } else if (dest.type === "webhook") { | ||
| clientConfig.webhook = { | ||
| url: dest.identifier, | ||
| headers: sanitizeWebhookHeaders(cfg.headers), | ||
| }; | ||
| channels.push("webhook"); | ||
| targets.push({ | ||
| channel: "webhook", | ||
| clientConfig: { | ||
| webhook: { | ||
| url: dest.identifier, | ||
| headers: sanitizeWebhookHeaders(cfg.headers), | ||
| }, | ||
| }, | ||
| }); | ||
| } else if (dest.type === "email") { | ||
| clientConfig.email = { | ||
| defaultTo: dest.identifier, | ||
| from: (cfg.from as string) || "Databuddy <alerts@databuddy.cc>", | ||
| sendEmailAction: async (payload: { | ||
| to: string | string[]; | ||
| subject: string; | ||
| html?: string; | ||
| text?: string; | ||
| from?: string; | ||
| }) => { | ||
| const { Resend } = await import("resend"); | ||
| const apiKey = process.env.RESEND_API_KEY; | ||
| if (!apiKey) { | ||
| return; | ||
| } | ||
| const resend = new Resend(apiKey); | ||
| await resend.emails.send({ | ||
| from: payload.from || "Databuddy <alerts@databuddy.cc>", | ||
| to: Array.isArray(payload.to) ? payload.to : [payload.to], | ||
| subject: payload.subject, | ||
| html: payload.html || payload.text || "", | ||
| }); | ||
| targets.push({ | ||
| channel: "email", | ||
| clientConfig: { | ||
| email: { | ||
| defaultTo: dest.identifier, | ||
| from: | ||
| typeof cfg.from === "string" | ||
| ? cfg.from | ||
| : "Databuddy <alerts@databuddy.cc>", | ||
| sendEmailAction: async (payload: { | ||
| to: string | string[]; | ||
| subject: string; | ||
| html?: string; | ||
| text?: string; | ||
| from?: string; | ||
| }) => { | ||
| const { Resend } = await import("resend"); | ||
| const apiKey = process.env.RESEND_API_KEY; | ||
| if (!apiKey) { | ||
| return; | ||
| } | ||
| const resend = new Resend(apiKey); | ||
| await resend.emails.send({ | ||
| from: payload.from || "Databuddy <alerts@databuddy.cc>", | ||
| to: Array.isArray(payload.to) ? payload.to : [payload.to], | ||
| subject: payload.subject, | ||
| html: payload.html || payload.text || "", | ||
| }); | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| channels.push("email"); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { clientConfig, channels }; | ||
| return targets; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,37 @@ | ||
| export { buildAlarmNotificationConfig as toNotificationConfig } from "@databuddy/notifications"; | ||
| import { | ||
| NotificationClient, | ||
| buildAlarmNotificationConfig, | ||
| buildAlarmNotificationTargets, | ||
| type NotificationPayload, | ||
| type NotificationResult, | ||
| } from "@databuddy/notifications"; | ||
|
|
||
| export const toNotificationConfig = buildAlarmNotificationConfig; | ||
| export const toNotificationTargets = buildAlarmNotificationTargets; | ||
|
|
||
| type NotificationTarget = ReturnType< | ||
| typeof buildAlarmNotificationTargets | ||
| >[number]; | ||
|
|
||
| function getErrorMessage(error: unknown): string { | ||
| return error instanceof Error ? error.message : String(error); | ||
| } | ||
|
|
||
| export async function sendNotificationTarget( | ||
| target: NotificationTarget, | ||
| payload: NotificationPayload | ||
| ): Promise<NotificationResult[]> { | ||
| try { | ||
| return await new NotificationClient(target.clientConfig).send(payload, { | ||
| channels: [target.channel], | ||
| }); | ||
| } catch (error) { | ||
| return [ | ||
| { | ||
| success: false, | ||
| channel: target.channel, | ||
| error: getErrorMessage(error), | ||
| }, | ||
| ]; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.