Repository navigation
fix(api): prevent Slack notification failures when alert message exceeds 3000 chars #3473
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
kodiakhq
merged 3 commits into
ClickHouse:main
from
Tyagiquamar:fix/3452-slack-block-limit
Dec 2, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,5 @@ | ||
| --- | ||
| "@hyperdx/api": patch | ||
| --- | ||
|
|
||
| fix(api): prevent Slack notification failures when alert message exceeds 3000 chars |
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
123 changes: 123 additions & 0 deletions
123
packages/api/src/tasks/checkAlerts/transports/__tests__/slack.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,123 @@ | ||
| import { WebhookService } from '@hyperdx/common-utils/dist/types'; | ||
| import { fromPartial } from '@total-typescript/shoehorn'; | ||
| import { ObjectId } from 'mongodb'; | ||
|
|
||
| import { AlertState } from '@/models/alert'; | ||
| import { | ||
| formatSlackSectionText, | ||
| 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('formatSlackSectionText', () => { | ||
| const link = 'https://app.hyperdx.io/alerts/123'; | ||
| const title = 'High Error Rate'; | ||
| const header = `*<${link} | ${title}>*\n`; | ||
|
|
||
| it('keeps the body when the text fits', () => { | ||
| const body = 'Error rate exceeded 5%'; | ||
| expect(formatSlackSectionText(link, title, body)).toBe( | ||
| `${header}${body}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps a body that exactly fills the limit', () => { | ||
| const body = 'b'.repeat(3000 - header.length); | ||
| expect(formatSlackSectionText(link, title, body)).toBe( | ||
| `${header}${body}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('truncates the body to fit the limit, keeping the link', () => { | ||
| const formatted = formatSlackSectionText(link, title, 'b'.repeat(3500)); | ||
|
|
||
| expect(formatted).toBe( | ||
| `${header}${'b'.repeat(3000 - header.length - 7)}...`, | ||
| ); | ||
| expect(formatted.length).toBeLessThanOrEqual(3000); | ||
| }); | ||
|
|
||
| it('re-closes a code block the cut lands inside', () => { | ||
| const body = `5 lines found\n\`\`\`\n${'log line\n'.repeat(400)}\`\`\``; | ||
| const formatted = formatSlackSectionText(link, title, body); | ||
|
|
||
| expect(formatted.endsWith('...\n```')).toBe(true); | ||
| expect(formatted.split('```')).toHaveLength(3); | ||
| expect(formatted.length).toBeLessThanOrEqual(3000); | ||
| }); | ||
|
|
||
| it('does not add a fence when the cut lands after a closed code block', () => { | ||
| const body = `\`\`\`\nsample\n\`\`\`\n${'m'.repeat(3500)}`; | ||
| const formatted = formatSlackSectionText(link, title, body); | ||
|
|
||
| expect(formatted.endsWith('m...')).toBe(true); | ||
| expect(formatted.split('```')).toHaveLength(3); | ||
| }); | ||
|
|
||
| it('does not split an emoji at the cut', () => { | ||
| const prefix = 'b'.repeat(3000 - header.length - 7 - 1); | ||
| const formatted = formatSlackSectionText( | ||
| link, | ||
| title, | ||
| `${prefix}🚨${'b'.repeat(100)}`, | ||
| ); | ||
|
|
||
| expect(formatted).toBe(`${header}${prefix}...`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleSendSlackWebhook', () => { | ||
| it('passes formatted section block text to postMessageToWebhook', async () => { | ||
| const mockPostMessage = jest | ||
| .spyOn(slack, 'postMessageToWebhook') | ||
| .mockResolvedValue({ text: 'ok' }); | ||
|
|
||
| const channel = fromPartial<WebhookChannel>({ | ||
| type: 'webhook', | ||
| channel: { | ||
| _id: new ObjectId(), | ||
| name: 'Slack Alerts', | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| service: WebhookService.Slack, | ||
| url: 'https://hooks.slack.com/services/T00/B00/X00', | ||
| }, | ||
| }); | ||
|
|
||
| const message: Message = { | ||
| hdxLink: 'https://app.hyperdx.io/alerts/123', | ||
| title: 'CPU Alert', | ||
| body: 'CPU > 90%', | ||
| state: AlertState.ALERT, | ||
| 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: [ | ||
| { | ||
|
pulpdrew marked this conversation as resolved.
Resolved
|
||
| type: 'section', | ||
| text: { | ||
| type: 'mrkdwn', | ||
| text: '*<https://app.hyperdx.io/alerts/123 | CPU Alert>*\nCPU > 90%', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.