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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat alertmanager: add new template function grafanaExploreURL #3849

Merged
merged 8 commits into from
Jan 10, 2023

Conversation

FUSAKLA
Copy link
Contributor

@FUSAKLA FUSAKLA commented Jan 4, 2023

As discussed in #2997 (comment)

Not sure about the naming 馃し
Moved the custom template functions to a separate file for more clarity, tests are passing and generated URL seems to be working.

Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

Nice one :) left one suggestion, otherwise LGTM

},
})
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit reluctant of including this especially for something that runs on a regular basis in the alertmanager. WDYT about returning a string which contains the error? It will result in ugly templates, but won't crash the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm not sure about that one. But the panic is wrong either way, the function should return error probably as described in https://pkg.go.dev/text/template#FuncMap

I do see your point on not failing, on the other way Alertmanager does expose metrics about failed template renders and if it does not fail, it might stay unnoticed otherwise you should have an alert on failed template renders anyway 馃し

Alertmanager itself panics or returns errors but does not render the error as a result, but I do not have a strong opinion, so if you prefer rendering it, I'll be happy to change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, refactored to return the error properly in 57d203b

pkg/alertmanager/alertmanager_template.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Thanks! I have a question about the actual generated URL and a couple of nits 馃檱

pkg/alertmanager/alertmanager_template.go Outdated Show resolved Hide resolved
{
name: "generate grafana explore URL",
template: `{{ grafanaExploreURL "https://foo.bar" "test_datasoruce" "now-12h" "now" "up{foo!=\"bar\"}" }}`,
result: `https://foo.bar/explore?left=` + url.QueryEscape(`{"datasource":"test_datasoruce","range":{"from":"now-12h","to":"now"},"queries":[{"datasource":"test_datasoruce","expr":"up{foo!=\"bar\"}","instant":false,"range":true,"refId":"A"}]}`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've tried to compare it with the URL generated by Grafana 9.4 and I got it slightly different (look at the datasource inside queries):

{
    "datasource":"P1809F7CD0C75ACF3",
    "queries":[{"refId":"A","datasource":{"type":"prometheus","uid":"P1809F7CD0C75ACF3"},"editorMode":"code","expr":"count(up)","legendFormat":"__auto","range":true,"instant":false}],
    "range":{"from":"now-1h","to":"now"}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, yes, there are more somehow backward compatible ways to refer to datasource (even in the dashboard JSON IIRC).
But yes, you're right. I updated it and seems to be working even with our 9.2 Grafana instance, so hopefully will be compatible enough.

PTAL e7abe5c

@FUSAKLA FUSAKLA requested review from pracucci and removed request for dimitarvdimitrov January 9, 2023 22:34
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
@FUSAKLA
Copy link
Contributor Author

FUSAKLA commented Jan 9, 2023

Rebased and added changelog note

@FUSAKLA FUSAKLA requested review from dimitarvdimitrov and pracucci and removed request for pracucci and dimitarvdimitrov January 9, 2023 22:40
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
Copy link
Collaborator

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

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