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

Extend Discord Notifier #3761

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Extend Discord Notifier #3761

wants to merge 13 commits into from

Conversation

LucaDev
Copy link
Contributor

@LucaDev LucaDev commented Mar 12, 2024

This PR extends the Discord notifier to improve the readability of alerts.
It changes 5 key elements:

  1. A configureable icon as bot Logo (I'm currently using the GitHub Avatar. Is that alright? Is there a better way for doing this?)
  2. A configurable name for the bot
  3. Alert-ID in the footer
  4. Start & end times in the footer (Even if the message is sent with a delay, the correct time will be displayed)
  5. It uses Discords fields-feature to display labels
  6. It sends one message per alert

It also fixes UI builds on apple silicon by forcing the linux/amd86 platform for the elm container
Discord Screenshot

Before:
Screenshot old

@gotjosh
Copy link
Member

gotjosh commented Mar 12, 2024

This looks nice! Thank you very much for your contribution -- can you please show me an example of the after with multiple labels?

@LucaDev
Copy link
Contributor Author

LucaDev commented Mar 12, 2024

This looks nice! Thank you very much for your contribution -- can you please show me an example of the after with multiple labels?

Sure! Desktop:
Desktop
Mobile

I'm also planing to change the title to not include the labels anymore

@LucaDev
Copy link
Contributor Author

LucaDev commented Mar 12, 2024

Small update:
image

@LucaDev
Copy link
Contributor Author

LucaDev commented Mar 14, 2024

I'd love to get some first feedback @gotjosh

@LucaDev
Copy link
Contributor Author

LucaDev commented Apr 26, 2024

Hey. I‘d love to get some feedback on this.

@LucaDev LucaDev marked this pull request as ready for review April 26, 2024 19:03
@LucaDev
Copy link
Contributor Author

LucaDev commented Apr 26, 2024

I‘ll correct a few minor things this weekend. Afterwards it should be ready to be merged

@grobinson-grafana
Copy link
Contributor

The visual changes look nice! You'll need to remove the one message per alert change though as it interferes with alert grouping behavior. For example, if a user wants one message per alert then grouping can be disabled in the route, like so:

route:
    receiver: test
    group_by: ['...']

You can find it documented here.

@LucaDev
Copy link
Contributor Author

LucaDev commented May 23, 2024

Good catch! Thank you!

The visual changes look nice! You'll need to remove the one message per alert change though as it interferes with alert grouping behavior. For example, if a user wants one message per alert then grouping can be disabled in the route, like so:

route:
    receiver: test
    group_by: ['...']

You can find it documented here.

Could you please have a look at the latest changes? Grouping is now allowed by only sending one message per alert - containing as much 'embeds' (those alert boxes you're seeing) as allowed per Discord API limits. Is it okay like that?

I still have to clean the code up and make it a bit more readable before this is ready to be merged tho. (Argh.. and signing off the past commits. I always forget to do it.. I will add a signed-off git message template now 😄)

LucaDev added 10 commits May 23, 2024 18:05
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Signed-off-by: Luca Kröger <l.kroeger01@gmail.com>
Title string `yaml:"title,omitempty" json:"title,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
TitleURL string `yaml:"title_url,omitempty" json:"title_url,omitempty"`
SkipFields bool `yaml:"skip_fields,omitempty" json:"skip_fields,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this as I haven't seen users asking for this in the past. It's much easier to add it later if we need. It's much harder to remove something once it's been added.

Copy link
Contributor Author

@LucaDev LucaDev May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda agree. On the other hand... #3821 wants to add them. So apparently users are asking for them. What do you think? I'm okay with both options. I'll let you guys - as the actual code owners - decide what direction to choose here. It would certainly reduce the huge amount of validation / truncation I had to add 😄 On the other hand - customizability is always a good thing I guess. @gotjosh what do you think?

// 2000 characters per message is the limit (not counting embeds).
maxMessageContentLength = 2000
// 10 embeds per Message is the maximum.
maxEmbedsPerMessage = 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A concern I have here is that it's not uncommon for users to have large groups (more than 10 alerts). If Discord is limited to 10 embeds then this might make notifications less useful for those users as truncation will become a regular occurrence for them. What do you think @gotjosh?

data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
tmpl := notify.TmplText(n.tmpl, data, &err)
if err != nil {
return false, err
}

title, truncated := notify.TruncateInRunes(tmpl(n.conf.Title), maxTitleLenRunes)
author, truncated := notify.TruncateInRunes(tmpl(n.conf.BotUsername), maxEmbedAuthorNameLenRunes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
author, truncated := notify.TruncateInRunes(tmpl(n.conf.BotUsername), maxEmbedAuthorNameLenRunes)
username, truncated := notify.TruncateInRunes(tmpl(n.conf.BotUsername), maxEmbedAuthorNameLenRunes)

if alerts.Status() == model.AlertFiring {
color = colorRed
w := webhook{
Username: author,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Username: author,
Username: username,


alerts := types.Alerts(as...)

for alertIndex, alert := range alerts {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use idiomatic variable name i here:

Suggested change
for alertIndex, alert := range alerts {
for i, alert := range alerts {

break
}

title, truncated := notify.TruncateInRunes(tmpl(n.conf.Title), maxTitleLenRunes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title gets repeated for each embed which is kind of weird. For example: [FIRING:2] but each embed contains just one alert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I'm gonna refactor this - using the embed title for displaying the alert summary (CommonAnnotations.summary) and adding the total alert count to the message above the embeds.

@grobinson-grafana
Copy link
Contributor

I noticed that when the maximum number of embeds is exceeded, just the first embed is sent? Is that expected? Looking at the code I would expect 10 embeds to be sent with the truncation message rather than just 1 embed?

Screenshot 2024-05-29 at 17 22 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants