Skip to content

Commit edd160a

Browse files
committed
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>
1 parent 89ad276 commit edd160a

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import {
2+
formatSlackSectionText,
3+
getFallbackHdxLink,
4+
handleSendSlackWebhook,
5+
} from '@/tasks/checkAlerts/transports/slack';
6+
import type { Message, WebhookChannel } from '@/tasks/checkAlerts/transports/types';
7+
import * as slack from '@/utils/slack';
8+
9+
jest.mock('@/utils/slack');
10+
jest.mock('@/utils/validators');
11+
12+
describe('Slack transport', () => {
13+
describe('getFallbackHdxLink', () => {
14+
it('returns origin + /alerts for valid URLs', () => {
15+
expect(
16+
getFallbackHdxLink('https://app.hyperdx.io/chart?config=%7B%22very%22%3A%22long%22%7D'),
17+
).toBe('https://app.hyperdx.io/alerts');
18+
});
19+
20+
it('returns original input if not a valid URL', () => {
21+
expect(getFallbackHdxLink('not-a-url')).toBe('not-a-url');
22+
});
23+
});
24+
25+
describe('formatSlackSectionText', () => {
26+
it('keeps standard link and body when under character limit', () => {
27+
const link = 'https://app.hyperdx.io/alerts/123';
28+
const title = 'High Error Rate';
29+
const body = 'Error rate exceeded 5%';
30+
expect(formatSlackSectionText(link, title, body)).toBe(
31+
`*<${link} | ${title}>*\n${body}`,
32+
);
33+
});
34+
35+
it('falls back to /alerts when large query string exceeds limit', () => {
36+
const longQuery = 'x'.repeat(3100);
37+
const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`;
38+
const title = 'High Latency';
39+
const body = 'p99 latency > 500ms';
40+
const formatted = formatSlackSectionText(longLink, title, body, 3000);
41+
42+
expect(formatted).toBe(`*<https://app.hyperdx.io/alerts | ${title}>*\n${body}`);
43+
expect(formatted.length).toBeLessThanOrEqual(3000);
44+
});
45+
46+
it('truncates body when text still exceeds limit after fallback link', () => {
47+
const longQuery = 'x'.repeat(2900);
48+
const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`;
49+
const title = 'High Latency';
50+
const longBody = 'b'.repeat(3500);
51+
const formatted = formatSlackSectionText(longLink, title, longBody, 3000);
52+
53+
expect(formatted.startsWith(`*<https://app.hyperdx.io/alerts | ${title}>*\n`)).toBe(true);
54+
expect(formatted.endsWith('...')).toBe(true);
55+
expect(formatted.length).toBeLessThanOrEqual(3000);
56+
});
57+
});
58+
59+
describe('handleSendSlackWebhook', () => {
60+
it('passes formatted section block text to postMessageToWebhook', async () => {
61+
const mockPostMessage = jest.spyOn(slack, 'postMessageToWebhook').mockResolvedValue({} as any);
62+
63+
const channel: WebhookChannel = {
64+
type: 'webhook',
65+
channel: {
66+
_id: 'wh-1' as any,
67+
name: 'Slack Alerts',
68+
service: 'slack' as any,
69+
url: 'https://hooks.slack.com/services/T00/B00/X00',
70+
} as any,
71+
};
72+
73+
const message: Message = {
74+
hdxLink: 'https://app.hyperdx.io/chart?config=test',
75+
title: 'CPU Alert',
76+
body: 'CPU > 90%',
77+
state: 'ALERT' as any,
78+
startTime: 0,
79+
endTime: 1000,
80+
eventId: 'evt-1',
81+
};
82+
83+
await handleSendSlackWebhook(channel, message);
84+
85+
expect(mockPostMessage).toHaveBeenCalledTimes(1);
86+
expect(mockPostMessage).toHaveBeenCalledWith(
87+
'https://hooks.slack.com/services/T00/B00/X00',
88+
{
89+
text: 'CPU Alert',
90+
blocks: [
91+
{
92+
type: 'section',
93+
text: {
94+
type: 'mrkdwn',
95+
text: '*<https://app.hyperdx.io/chart?config=test | CPU Alert>*\nCPU > 90%',
96+
},
97+
},
98+
],
99+
},
100+
);
101+
});
102+
});
103+
});

‎packages/api/src/tasks/checkAlerts/transports/slack.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ import type {
1313
import * as slack from '@/utils/slack';
1414
import { validateWebhookUrl } from '@/utils/validators';
1515

16+
const MAX_SLACK_SECTION_LENGTH = 3000;
17+
18+
export const getFallbackHdxLink = (hdxLink: string): string => {
19+
try {
20+
const url = new URL(hdxLink);
21+
return `${url.origin}/alerts`;
22+
} catch {
23+
return hdxLink;
24+
}
25+
};
26+
27+
export const formatSlackSectionText = (
28+
hdxLink: string,
29+
title: string,
30+
body: string,
31+
maxLength = MAX_SLACK_SECTION_LENGTH,
32+
): string => {
33+
let link = hdxLink;
34+
let text = `*<${link} | ${title}>*\n${body}`;
35+
if (text.length <= maxLength) {
36+
return text;
37+
}
38+
39+
// If text exceeds limit, try falling back to the shorter /alerts link
40+
const fallbackLink = getFallbackHdxLink(hdxLink);
41+
if (fallbackLink !== link) {
42+
text = `*<${fallbackLink} | ${title}>*\n${body}`;
43+
if (text.length <= maxLength) {
44+
return text;
45+
}
46+
link = fallbackLink;
47+
}
48+
49+
// If still exceeding limit, truncate body with an ellipsis
50+
const header = `*<${link} | ${title}>*\n`;
51+
const availableBodyLen = maxLength - header.length - 3;
52+
if (availableBodyLen > 0) {
53+
return `${header}${body.slice(0, availableBodyLen)}...`;
54+
}
55+
56+
return `*<${link} | ${title}>*`.slice(0, maxLength);
57+
};
58+
1659
export const handleSendSlackWebhook = async (
1760
channel: WebhookChannel,
1861
message: Message,
@@ -29,7 +72,11 @@ export const handleSendSlackWebhook = async (
2972
type: 'section',
3073
text: {
3174
type: 'mrkdwn',
32-
text: `*<${message.hdxLink} | ${message.title}>*\n${message.body}`,
75+
text: formatSlackSectionText(
76+
message.hdxLink,
77+
message.title,
78+
message.body,
79+
),
3380
},
3481
},
3582
],

0 commit comments

Comments
 (0)