Skip to content

Extend URL handling for Alertmanager Templates #4616

@StopMotionCuber

Description

@StopMotionCuber

We have a setup where we are using Alertmanager in combination with Prometheus and Grafana. I would like to share a "See in Grafana" Link and convert the URL that prometheus sends within a template to a link in our internal Grafana instance.

What I would like to do here is the following:

  1. Extract the expr Part of the URL query
  2. Convert all + to /%20 (this is just a difference between Grafana and Prometheus)
  3. Embed this in a link in a template

The issue I'm facing now is that the URL I'm getting from Prometheus is URL-encoded. When parsing these with html/template, that encoding is encoded again. Now I see 3 possible solutions:

  1. Decode the URL with some kind of urldecode (not possible within a template, as that function is not exposed)
  2. Don't do a urldecode, but mark the string as safe (also not possible within a template, as that function is not exposed. We do have safeHtml and would need something like safeUrl)
  3. Manually do the URL decoding with a lot of reReplaceAll calls (note: this is not exhaustive and doesn't do a full decode, it's just "good enough" for some of our queries. Still exploring what else needs to be replaced):
<a href="https://<grafana_url>/explore?left=%7B%22datasource%22%3A%22prometheus%22%2C%22queries%22%3A%5B%7B%22expr%22%3A%22{{ ( reReplaceAll ".*expr=([^&]+).*" "$1" .GeneratorURL ) | reReplaceAll "+" " " | reReplaceAll "%28" "(" | reReplaceAll "%29" ")" | reReplaceAll "%7B" "{" | reReplaceAll "%7D" "}" | reReplaceAll "%3D" "=" | reReplaceAll "%2F" "/" | reReplaceAll "%3A" ":" | reReplaceAll "%2C" "," | reReplaceAll "%3C" "<" | reReplaceAll "%3E" ">" | reReplaceAll "%5C" "\\" | reReplaceAll "%22" "\"" | reReplaceAll "\\\\" "\\\\\\\\" | reReplaceAll "\"" "\\\"" }}%22%2C%22refId%22%3A%22A%22%7D%5D%2C%22range%22%3A%7B%22from%22%3A%22now-6h%22%2C%22to%22%3A%22now%22%7D%7D">See in Grafana</a><br>

I settled now for Option 3, as that is the only option possible currently with alertmanager. But I would like to be able to go for either option 1 or option 2.

Is this something that is desirable for the alertmanager, and if yes, would it make more sense to go for 1, 2 or implement both? I think 2 should be really easy to implement (basically the same as safeHtml), while 1 is probably also not hard to do.

I would be happy to contribute here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions