From edd160a04e6fe46fadeffe285870b8a2604a79a9 Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:47:20 +0530 Subject: [PATCH 1/2] fix(api): prevent Slack notification failures when alert message exceeds 3000 chars Fixes #3452 Slack section blocks enforce a strict 3000-character limit on the text field. Inline alert links encode large chartConfig objects in query params, causing the message block text to exceed 3000 chars and fail delivery with WEBHOOK_ERROR. Format Slack section text using formatSlackSectionText, which falls back to the shorter /alerts link if the link causes an overflow, and truncates the body safely if necessary. Signed-off-by: Mohd Quamar Tyagi <347892697+Tyagiquamar@users.noreply.github.com> --- .../transports/__tests__/slack.test.ts | 103 ++++++++++++++++++ .../src/tasks/checkAlerts/transports/slack.ts | 49 ++++++++- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts diff --git a/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts new file mode 100644 index 0000000000..63e3423fd0 --- /dev/null +++ b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts @@ -0,0 +1,103 @@ +import { + formatSlackSectionText, + getFallbackHdxLink, + handleSendSlackWebhook, +} from '@/tasks/checkAlerts/transports/slack'; +import type { Message, WebhookChannel } from '@/tasks/checkAlerts/transports/types'; +import * as slack from '@/utils/slack'; + +jest.mock('@/utils/slack'); +jest.mock('@/utils/validators'); + +describe('Slack transport', () => { + describe('getFallbackHdxLink', () => { + it('returns origin + /alerts for valid URLs', () => { + expect( + getFallbackHdxLink('https://app.hyperdx.io/chart?config=%7B%22very%22%3A%22long%22%7D'), + ).toBe('https://app.hyperdx.io/alerts'); + }); + + it('returns original input if not a valid URL', () => { + expect(getFallbackHdxLink('not-a-url')).toBe('not-a-url'); + }); + }); + + describe('formatSlackSectionText', () => { + it('keeps standard link and body when under character limit', () => { + const link = 'https://app.hyperdx.io/alerts/123'; + const title = 'High Error Rate'; + const body = 'Error rate exceeded 5%'; + expect(formatSlackSectionText(link, title, body)).toBe( + `*<${link} | ${title}>*\n${body}`, + ); + }); + + it('falls back to /alerts when large query string exceeds limit', () => { + const longQuery = 'x'.repeat(3100); + const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; + const title = 'High Latency'; + const body = 'p99 latency > 500ms'; + const formatted = formatSlackSectionText(longLink, title, body, 3000); + + expect(formatted).toBe(`**\n${body}`); + expect(formatted.length).toBeLessThanOrEqual(3000); + }); + + it('truncates body when text still exceeds limit after fallback link', () => { + const longQuery = 'x'.repeat(2900); + const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; + const title = 'High Latency'; + const longBody = 'b'.repeat(3500); + const formatted = formatSlackSectionText(longLink, title, longBody, 3000); + + expect(formatted.startsWith(`**\n`)).toBe(true); + expect(formatted.endsWith('...')).toBe(true); + expect(formatted.length).toBeLessThanOrEqual(3000); + }); + }); + + describe('handleSendSlackWebhook', () => { + it('passes formatted section block text to postMessageToWebhook', async () => { + const mockPostMessage = jest.spyOn(slack, 'postMessageToWebhook').mockResolvedValue({} as any); + + const channel: WebhookChannel = { + type: 'webhook', + channel: { + _id: 'wh-1' as any, + name: 'Slack Alerts', + service: 'slack' as any, + url: 'https://hooks.slack.com/services/T00/B00/X00', + } as any, + }; + + const message: Message = { + hdxLink: 'https://app.hyperdx.io/chart?config=test', + title: 'CPU Alert', + body: 'CPU > 90%', + state: 'ALERT' as any, + startTime: 0, + endTime: 1000, + eventId: 'evt-1', + }; + + await handleSendSlackWebhook(channel, message); + + expect(mockPostMessage).toHaveBeenCalledTimes(1); + expect(mockPostMessage).toHaveBeenCalledWith( + 'https://hooks.slack.com/services/T00/B00/X00', + { + text: 'CPU Alert', + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: '**\nCPU > 90%', + }, + }, + ], + }, + ); + }); + }); +}); diff --git a/packages/api/src/tasks/checkAlerts/transports/slack.ts b/packages/api/src/tasks/checkAlerts/transports/slack.ts index d1f069ade6..cacef85d69 100644 --- a/packages/api/src/tasks/checkAlerts/transports/slack.ts +++ b/packages/api/src/tasks/checkAlerts/transports/slack.ts @@ -13,6 +13,49 @@ import type { import * as slack from '@/utils/slack'; import { validateWebhookUrl } from '@/utils/validators'; +const MAX_SLACK_SECTION_LENGTH = 3000; + +export const getFallbackHdxLink = (hdxLink: string): string => { + try { + const url = new URL(hdxLink); + return `${url.origin}/alerts`; + } catch { + return hdxLink; + } +}; + +export const formatSlackSectionText = ( + hdxLink: string, + title: string, + body: string, + maxLength = MAX_SLACK_SECTION_LENGTH, +): string => { + let link = hdxLink; + let text = `*<${link} | ${title}>*\n${body}`; + if (text.length <= maxLength) { + return text; + } + + // If text exceeds limit, try falling back to the shorter /alerts link + const fallbackLink = getFallbackHdxLink(hdxLink); + if (fallbackLink !== link) { + text = `*<${fallbackLink} | ${title}>*\n${body}`; + if (text.length <= maxLength) { + return text; + } + link = fallbackLink; + } + + // If still exceeding limit, truncate body with an ellipsis + const header = `*<${link} | ${title}>*\n`; + const availableBodyLen = maxLength - header.length - 3; + if (availableBodyLen > 0) { + return `${header}${body.slice(0, availableBodyLen)}...`; + } + + return `*<${link} | ${title}>*`.slice(0, maxLength); +}; + export const handleSendSlackWebhook = async ( channel: WebhookChannel, message: Message, @@ -29,7 +72,11 @@ export const handleSendSlackWebhook = async ( type: 'section', text: { type: 'mrkdwn', - text: `*<${message.hdxLink} | ${message.title}>*\n${message.body}`, + text: formatSlackSectionText( + message.hdxLink, + message.title, + message.body, + ), }, }, ], From 2455498cd40d40b11ce0e73ab958da72c5d1464f Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:56:58 +0530 Subject: [PATCH 2/2] chore: add changeset for slack block limit fix Signed-off-by: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> --- .changeset/slack-block-limit.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/slack-block-limit.md diff --git a/.changeset/slack-block-limit.md b/.changeset/slack-block-limit.md new file mode 100644 index 0000000000..18de699cc1 --- /dev/null +++ b/.changeset/slack-block-limit.md @@ -0,0 +1,5 @@ +--- +"@hyperdx/api": patch +--- + +fix(api): prevent Slack notification failures when alert message exceeds 3000 chars