cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.17.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/utils/absent.go

63lines · modecode

1package utils
2
3import (
4 "github.com/cloudflare/pint/internal/parser"
5
6 promParser "github.com/prometheus/prometheus/promql/parser"
7 "github.com/rs/zerolog/log"
8)
9
10type PromQLFragment struct {
11 Fragment *parser.PromQLNode
12 BinExpr *promParser.BinaryExpr
13}
14
15func 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