mimir icon indicating copy to clipboard operation
mimir copied to clipboard

ruler: support explore URL format in the GeneratorURL

Open FUSAKLA opened this issue 2 years ago • 8 comments

From original discussion on slack

Current state

In Prometheus, an alert does have a GeneratorURL which points to the graph view of the particular Prometheus which sent it, with the query from the alert already filled in.

With Mimir ruler this is possible the same way: Currently, ruler allows configuring the external_url and then it generates the GeneratorURL using the Prometheus code which means appending /graph?g0.expr=%s&g0.tab=0 to the external_url.

Problem

The issue is that Mimir does not have the Prometheus compatible UI, so there is nowhere to point the URL to. Naturally, I'd expect this to be compatible with the Grafana explore since that is the native UI for querying Mimir, but it appears not.

Suggestion

Add probably 2 new configuration options to the ruler something like

ruler:
  external_url: <Grafana URL>
  # Default prometheus so no breaking change, if set to grafana-explore, generator_datasource_id would be required
  generator_url_format: [prometheus(default),grafana-explore]
  # Explore requires setting the datasource ID in the URL if I'm not mistaken, so it would have to be configurable here
  generator_datasource_id: xyz

Workaround

I found quite easy running only Prometheus react UI pointing to a Mimir. This way I'm capable of doing an "adapter" to make the URL work (and as a side effect give users the classic Prometheus UI which would ease the Migration for them)

If agreed on the solution, I'd be happy to implement it :)

FUSAKLA avatar Sep 20 '22 18:09 FUSAKLA

You raised a very good point, and I agree with you that the Mimir ruler should be able to generate URLs which work with Grafana. However, I'm wondering if we should integrate it with Grafana Alerting, instead of the Grafana explore, but I don't have any concern supporting Grafana explore too (or begin just supporting Grafana explore).

Few comments about your proposal:

  • generator_url_format: I'm wondering if we should call it something like generator_url_backend because it specifies the type of backend.
  • generator_datasource_id: I would check if we can avoid it (e.g. relying on the default one). If not, I would rather call it generator_url_grafana_datasource_id to clarify it's the Grafana datasource it used in the generator URL.

pracucci avatar Sep 23 '22 09:09 pracucci

Right, linking to Grafana Alerting would probably make sense too, but I don't have much experience with that yet so I'm not able to suggest how that would work.

Regarding your comments on the proposal, both sound reasonable and make the configuration clearer, so :+1:.

Regarding the datasource, if you set "datasource":"default" in the Explore URL it works, but I do recall that those datasource references are being somehow revisited, so not sure if this won't be deprecated in the future. But I do think it would be better to make this configurable for cases such as having 2 Mimir clusters in a single Grafana instance or something like that.

FUSAKLA avatar Sep 23 '22 09:09 FUSAKLA

We eventually ended up running just the React part of Prometheus UI pointing to a Mimir API, and it works really well and easy to make and solves the issue for us in combination with the injection of tenant ID in the alert template.

But if we agree the way to do this, I'd be happy to implement it

FUSAKLA avatar Dec 16 '22 20:12 FUSAKLA

We do this in a Lambda (which is fed JSON-formatted alerts from Alertmanager via SNS). Here's the relevant Python code. Not perfect, but it mostly works for us. Hope it's helpful to someone.

def prometheus_graph_url_to_grafana_explore(input):
    parsed = urlparse(input)
    qs = parse_qs(parsed.query)
    expr = qs["g0.expr"][0]

    out = {
        "datasource": "Prometheus (OBS APS instance)",
        "queries": [
            {
                "refId": "A",
                "expr": expr,
                "editorMode": "code",
                "range": True,
                "instant": False,
                "format": "time_series",
                "exemplar": False,
                "interval": "",
            }
        ],
        "range": {"from": "now-12h", "to": "now"},
    }

    return f"{GRAFANA_URL}/explore?orgId=1&left={quote(json.dumps(out))}"

Packetslave avatar Dec 21 '22 21:12 Packetslave

In theory, it could be an additional template function since it is quite easy to add it after changes being made in https://github.com/grafana/mimir/pull/3758

Something like grafanaExploreURL <grafana_url> <datasource> <from> <to> <expr>

That would leave the generatorURL of the alert to be still useless, but would simplify construction of the explore URL :shrug:

FUSAKLA avatar Jan 03 '23 14:01 FUSAKLA

I like the idea of a custom function. We can still leave the door open to properly fix generatorURL but having the option to also generate the explore URL via function looks neat to me.

pracucci avatar Jan 04 '23 07:01 pracucci

Ok, I'll create new PR with the agreed function.

Thanks @Packetslave, your example will make it easier and hopefully this will make it easier for you also.

FUSAKLA avatar Jan 04 '23 10:01 FUSAKLA

Here, as promised https://github.com/grafana/mimir/pull/3849

FUSAKLA avatar Jan 04 '23 13:01 FUSAKLA

Closed by #3849.

pracucci avatar Apr 18 '23 07:04 pracucci

Yes, closing as done :tada:

FUSAKLA avatar Apr 28 '23 19:04 FUSAKLA