Skip to content
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

Slack alerts : can now specify members to tag in the channel #1832

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion flow/alerting/slack_alert_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package alerting
import (
"context"
"fmt"
"strings"

"github.com/slack-go/slack"
)
Expand All @@ -11,6 +12,7 @@ type SlackAlertSender struct {
AlertSender
client *slack.Client
channelIDs []string
members []string
slotLagMBAlertThreshold uint32
openConnectionsAlertThreshold uint32
}
Expand All @@ -26,6 +28,7 @@ func (s *SlackAlertSender) getOpenConnectionsAlertThreshold() uint32 {
type slackAlertConfig struct {
AuthToken string `json:"auth_token"`
ChannelIDs []string `json:"channel_ids"`
Members []string `json:"members"`
SlotLagMBAlertThreshold uint32 `json:"slot_lag_mb_alert_threshold"`
OpenConnectionsAlertThreshold uint32 `json:"open_connections_alert_threshold"`
}
Expand All @@ -36,14 +39,25 @@ func newSlackAlertSender(config *slackAlertConfig) *SlackAlertSender {
channelIDs: config.ChannelIDs,
slotLagMBAlertThreshold: config.SlotLagMBAlertThreshold,
openConnectionsAlertThreshold: config.OpenConnectionsAlertThreshold,
members: config.Members,
}
}

func (s *SlackAlertSender) sendAlert(ctx context.Context, alertTitle string, alertMessage string) error {
for _, channelID := range s.channelIDs {
var ccMembersPart strings.Builder
if len(s.members) == 0 {
ccMembersPart.WriteString("cc: <!channel>")
} else {
ccMembersPart.WriteString("cc:")
for _, member := range s.members {
ccMembersPart.WriteString(" @")
ccMembersPart.WriteString(member)
}
}
_, _, _, err := s.client.SendMessageContext(ctx, channelID, slack.MsgOptionBlocks(
slack.NewHeaderBlock(slack.NewTextBlockObject("plain_text", ":rotating_light:Alert:rotating_light:: "+alertTitle, true, false)),
slack.NewSectionBlock(slack.NewTextBlockObject("mrkdwn", alertMessage+"\ncc: <!channel>", false, false), nil, nil),
slack.NewSectionBlock(slack.NewTextBlockObject("mrkdwn", alertMessage+"\n"+ccMembersPart.String(), false, false), nil, nil),
))
if err != nil {
return fmt.Errorf("failed to send message to Slack channel %s: %w", channelID, err)
Expand Down
34 changes: 33 additions & 1 deletion ui/app/alert-config/new.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Button } from '@/lib/Button';
import { Label } from '@/lib/Label/Label';
import { TextField } from '@/lib/TextField';
import Image from 'next/image';
import Link from 'next/link';
import { Dispatch, SetStateAction, useState } from 'react';
import ReactSelect from 'react-select';
import { PulseLoader } from 'react-spinners';
Expand Down Expand Up @@ -81,6 +83,26 @@ function getSlackProps(
}}
/>
</div>
<div>
<p>Members</p>
<Label as='label' style={{ fontSize: 14 }}>
Slack usernames to tag in the channel for these alerts. If left empty,
pings the whole channel
</Label>
<TextField
key={'members'}
style={{ height: '2.5rem', marginTop: '0.5rem' }}
variant='simple'
placeholder='Comma separated'
value={config.members?.join(',')}
onChange={(e) => {
setConfig((previous) => ({
...previous,
members: e.target.value.split(','),
}));
}}
/>
</div>
</>
);
}
Expand Down Expand Up @@ -196,7 +218,7 @@ export function NewConfig(alertProps: AlertConfigProps) {
style={{
display: 'flex',
flexDirection: 'column',
rowGap: '2rem',
rowGap: '1.5rem',
marginTop: '3rem',
width: '40%',
}}
Expand Down Expand Up @@ -225,6 +247,16 @@ export function NewConfig(alertProps: AlertConfigProps) {
theme={SelectTheme}
/>
</div>
{serviceType === 'slack' && (
<Label
as={Link}
target='_blank'
href='https://docs.peerdb.io/features/alerting/slack-alerting'
style={{ color: 'teal', cursor: 'pointer', width: 'fit-content' }}
>
How to setup Slack alerting
</Label>
)}
<div>
<p>Slot Lag Alert Threshold (in GB)</p>
<TextField
Expand Down
1 change: 1 addition & 0 deletions ui/app/alert-config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const slackServiceConfigSchema = z.intersection(
}
)
.min(1, { message: 'Atleast one channel ID is needed' }),
members: z.array(z.string().trim()).optional(),
})
);

Expand Down
Loading