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

notify/telegram: Set API URL and ParseMode defaults #2879

Closed
wants to merge 3 commits into from

Conversation

metalmatze
Copy link
Member

For Telegram, there is really just one API URL, and other than for testing I never saw anything changing it. Therefore, as asked in #2876 we can set the default.
The default Telegram template that's shipped with Alertmanager uses HTML and if send to Telegram without the parseMode set to HTML Telegram's API will return an error:

Bad Request: can't parse entities: Character '-' is reserved and must be escaped with the preceding '\\' (400)"

Closes #2866
Closes #2876

Signed-off-by: Matthias Loibl <mail@matthiasloibl.com>
Copy link
Member

@gotjosh gotjosh left a comment

Choose a reason for hiding this comment

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

LGTM

Can you please fix the title of the PR so that it specifies that what we're fixing is the URL?

From notify/telegram: Set API and ParseMode defaults to Telegram: Set API URL and ParseMode defaults

@@ -44,6 +45,19 @@ func New(conf *config.TelegramConfig, t *template.Template, l log.Logger, httpOp
return nil, err
}

if conf.APIUrl == nil {
apiURL, err := url.Parse("https://api.telegram.org")
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can this be a const?

Copy link
Member

Choose a reason for hiding this comment

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

This should be done during unmarhalling of the configuration, not when calling New.

Copy link
Member

Choose a reason for hiding this comment

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

Good shout Julien.

On a second look at

DefaultTelegramConfig = TelegramConfig{
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
DisableNotifications: false,
Message: `{{ template "telegram.default.message" . }}`,
ParseMode: "MarkdownV2",
}

It seems like both of these belong within DefaultTelegramConfig

Copy link
Member

Choose a reason for hiding this comment

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

nit really because the API should come from DefaultGlobalConfig

if conf.ParseMode == "" {
// The default Telegram template is in HTML,
// so we need to set the parse mode to HTML for Telegram not to return a parse error.
conf.ParseMode = "HTML"
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can this also be a const? Perhaps a suggestion is defaultParseMode = "HTML"

Copy link
Member

Choose a reason for hiding this comment

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

This should be done during unmarhalling of the configuration, not when calling New.

Copy link
Member

Choose a reason for hiding this comment

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

Our current default is actually MarkdownV2 this would effectively be a breaking change and I don't think it was the intent of #2827.

Looking at the current template - I don't see how it is HTML, am I missing something? https://github.com/prometheus/alertmanager/blob/main/template/default.tmpl#L105-L114

Copy link
Member

@gotjosh gotjosh left a comment

Choose a reason for hiding this comment

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

On a second look, it seems like some parts of these are not correct.

I suggest we split this into two PRs.

  1. The default URL is correct and seems straightforward - we need to add it to the docs though.

  2. The default parse mode is currently Markdown, and setting the default to HTML would be a breaking change based on what's currently here.

@@ -44,6 +45,19 @@ func New(conf *config.TelegramConfig, t *template.Template, l log.Logger, httpOp
return nil, err
}

if conf.APIUrl == nil {
apiURL, err := url.Parse("https://api.telegram.org")
Copy link
Member

Choose a reason for hiding this comment

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

Good shout Julien.

On a second look at

DefaultTelegramConfig = TelegramConfig{
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
DisableNotifications: false,
Message: `{{ template "telegram.default.message" . }}`,
ParseMode: "MarkdownV2",
}

It seems like both of these belong within DefaultTelegramConfig

if conf.ParseMode == "" {
// The default Telegram template is in HTML,
// so we need to set the parse mode to HTML for Telegram not to return a parse error.
conf.ParseMode = "HTML"
Copy link
Member

Choose a reason for hiding this comment

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

Our current default is actually MarkdownV2 this would effectively be a breaking change and I don't think it was the intent of #2827.

Looking at the current template - I don't see how it is HTML, am I missing something? https://github.com/prometheus/alertmanager/blob/main/template/default.tmpl#L105-L114

@roidelapluie
Copy link
Member

Actually the API URl should come from DefaultGlobalConfig. i do not know why it is not the case.

@metalmatze metalmatze changed the title notify/telegram: Set API and ParseMode defaults notify/telegram: Set API URL and ParseMode defaults Jun 3, 2022
@metalmatze
Copy link
Member Author

I've looked into the parseMode issue again. It seems that Telegram doesn't allow the - in Markdown messages.
This character is used many times in messages.

Screenshot from 2022-06-03 11-46-20

Most notably the - is used to list all labels of an alert:

{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}

Unless we want to change the default template for all receivers, we should probably make HTML the default parseMode...

Signed-off-by: Matthias Loibl <mail@matthiasloibl.com>
Signed-off-by: Matthias Loibl <mail@matthiasloibl.com>
@metalmatze
Copy link
Member Author

Not sure why the tests fail. I can try rebasing later.

@gotjosh
Copy link
Member

gotjosh commented Jun 27, 2022

Once you rebase this should be fixed - it relates to #2899

@metalmatze metalmatze closed this Jul 4, 2022
@metalmatze metalmatze deleted the telegram-defaults branch July 4, 2022 20:21
Sh4kE added a commit to Sh4kE/k8s-projects that referenced this pull request Nov 15, 2022
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.

Issue with Telegram bot ID telegram_config.api_url should be optional
3 participants