Hi Callum

For what it's worth, I met the same issue, and I wrote a python lib that 
can split alert rules into multiple parts of expressions, based on grammar, 
so it can handles and/or/group_left/on, etc as well. The split_binary_op 
function takes a string expression and returns the parsed result in json 
format.

https://github.com/laixintao/promqlpy/

Also thanks Julius, your information helped a lot. ;D

On Friday, June 4, 2021 at 1:37:29 AM UTC+8 [email protected] wrote:

> Hi Callum,
>
> In general yes, the PromQL Go library lets you parse an expression and 
> then traverse its abstract syntax tree, using the types from the "ast" 
> package in 
> https://github.com/prometheus/prometheus/blob/main/promql/parser/ast.go. 
> You'd first use 
> https://pkg.go.dev/github.com/prometheus/prometheus/promql/parser#ParseExpr 
> to do the parsing, and that gives you back the AST to look at.
>
> For example, this is how PromLens shows the different sub-expressions of a 
> query (for your expression: https://demo.promlens.com/?l=eF6PYANAlbQ).
>
> However: Not every alerting query is of the shape "<something> <comp-op> 
> <number>". Comparison / filter operators are normal binary expressions that 
> may or may not occur at any part of a query (even deeply nested in a query 
> tree, not at the end), and you will also have alerting queries which do not 
> contain a filter operator at all. So you would have to apply certain 
> assumptions about the structure of a query to be able to extract just the 
> part you are interested in.
>
> Regards,
> Julius
>
> On Thu, Jun 3, 2021 at 1:04 AM Callum Jones <[email protected]> wrote:
>
>> Hi,
>>
>> With alerting rules the Prometheus API (and templating language) has 
>> access to the "Value" of an alert which appears to be the first metric 
>> specified without the comparator.
>>
>> I was wondering if this is something that is easily accessible in the 
>> Prometheus Go packages such that I could give it the expression string and 
>> it would strip out the comparison part and return just the metric query.
>>
>> For example passing in:
>> > sum by (instance) (rate(node_disk_read_bytes_total[2m])) / 1024 / 1024 
>> > 50
>> would return
>> > sum by (instance) (rate(node_disk_read_bytes_total[2m])) / 1024 / 1024
>>
>> Thanks,
>> Callum
>>
>> -- 
>> 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/a5b35b15-ebae-4030-8a9c-f67adb836eacn%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/prometheus-users/a5b35b15-ebae-4030-8a9c-f67adb836eacn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Julius Volz
> PromLabs - promlabs.com
>

-- 
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/62b48cf0-5acf-4eab-8cb8-e4c702938a44n%40googlegroups.com.

Reply via email to