Since rate() is a function, not a metric, you can't pass it to parser.ParseMetric() and expect it to work. You need to stick to ParseExpr and walk down the entire AST. What you need to do is find all instances of parser.VectorSelector, which is the data structure that hold time series selectors in queries, and append labels there. Here's an example: https://gist.github.com/prymitive/e021f48cda4744de24bce9c4b523c315
If you need more example code check https://github.com/cloudflare/pint/blob/main/cmd/pint/parse.go On Tuesday, 1 February 2022 at 19:47:16 UTC [email protected] wrote: > Hi everyone, > > I'm automating a way to modify Grafana Dashboards to our liking. For > example to use a public Grafana dashboard and append certain query labels > to every query in a dashboard. > For this, I figured to use the Prometheus Parser, which works well to some > extend for my use-case. However certain 'expressions' (or queries) don't > parse that well (or I'm doing it wrong). > > As a quick draft I have the following code: > ------ > parsedExpr, _ := parser.ParseExpr(expr) > childExprs := parser.Children(parsedExpr) > parsedMainMetric, _ := parser.ParseMetric(expr) > > fmt.Println(parsedMainMetric) > for _, childExpr := range childExprs { > > parsedChildExpr, _ := parser.ParseMetric(childExpr.String()) > fmt.Println(parsedChildExpr) > } > ------ > Which works for: > > > input: "rate(node_disk_io_time_seconds_total{bar=\"foo\"}[2m])", > > As this gives me the following output: > > > {__name__="rate"} > > {__name__="node_disk_io_time_seconds_total", bar="foo"} > > However, lets say I change the input to: > > > input: "rate(node_disk_io_time_seconds_total{bar=\"foo\"}[2m]) > 0", > > Then it does not exactly go how I expected it to be: > > > {__name__="rate"} > > {__name__="rate"} > > {} > > (obviously this is mainly due to how I wrote my draft code). > > I have seen https://demo.promlens.com/?l=eF6PYANAlbQ - and I was > wondering if someone could give me some pointers on how I should continue > (and if this is actually a viable option to update labels). > > Thanks in advance, > > Wiard > -- You received this message because you are subscribed to the Google Groups "Prometheus Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/7220852a-f5c3-49c7-af9a-1a491b290089n%40googlegroups.com.

