cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/absent.go
63lines · modecode
| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "github.com/cloudflare/pint/internal/parser" |
| 5 | |
| 6 | promParser "github.com/prometheus/prometheus/promql/parser" |
| 7 | "github.com/rs/zerolog/log" |
| 8 | ) |
| 9 | |
| 10 | type PromQLFragment struct { |
| 11 | Fragment *parser.PromQLNode |
| 12 | BinExpr *promParser.BinaryExpr |
| 13 | } |
| 14 | |
| 15 | func HasOuterAbsent(node *parser.PromQLNode) (calls []PromQLFragment) { |
| 16 | if n, ok := node.Node.(*promParser.Call); ok && n.Func.Name == "absent" { |
| 17 | calls = append(calls, PromQLFragment{Fragment: node}) |
| 18 | return calls |
| 19 | } |
| 20 | |
| 21 | if n, ok := node.Node.(*promParser.BinaryExpr); ok { |
| 22 | if n.VectorMatching != nil { |
| 23 | switch n.VectorMatching.Card { |
| 24 | // bar / absent(foo) |
| 25 | // absent(foo) / bar |
| 26 | case promParser.CardOneToOne: |
| 27 | // CardManyToOne: |
| 28 | // absent(foo{job="bar"}) * on(job) group_left(xxx) bar |
| 29 | // bar * on() group_left(xxx) absent(foo{job="bar"}) |
| 30 | // CardOneToMany |
| 31 | // bar * on() group_right(xxx) absent(foo{job="bar"}) |
| 32 | // absent(foo{job="bar"}) * on(job) group_right(xxx) bar |
| 33 | case promParser.CardManyToOne, promParser.CardOneToMany: |
| 34 | if ln, ok := n.LHS.(*promParser.Call); ok && ln.Func.Name == "absent" { |
| 35 | calls = append(calls, PromQLFragment{ |
| 36 | Fragment: node.Children[0], |
| 37 | BinExpr: n, |
| 38 | }) |
| 39 | } |
| 40 | if rn, ok := n.RHS.(*promParser.Call); ok && rn.Func.Name == "absent" { |
| 41 | calls = append(calls, PromQLFragment{ |
| 42 | Fragment: node.Children[1], |
| 43 | BinExpr: n, |
| 44 | }) |
| 45 | } |
| 46 | // bar AND absent(foo{job="bar"}) |
| 47 | case promParser.CardManyToMany: |
| 48 | for _, child := range node.Children { |
| 49 | calls = append(calls, HasOuterAbsent(child)...) |
| 50 | } |
| 51 | default: |
| 52 | log.Warn().Str("matching", n.VectorMatching.Card.String()).Msg("Unsupported VectorMatching operation") |
| 53 | } |
| 54 | return |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | for _, child := range node.Children { |
| 59 | calls = append(calls, HasOuterAbsent(child)...) |
| 60 | } |
| 61 | |
| 62 | return calls |
| 63 | } |
| 64 | |