cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.28.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/parser/utils/absent.go

77lines · 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
28 // absent(foo{job="bar"}) * on(job) group_left(xxx) bar
29 // bar * on() group_left(xxx) absent(foo{job="bar"})
30 case promParser.CardManyToOne:
31 if ln, ok := n.LHS.(*promParser.Call); ok && ln.Func.Name == "absent" {
32 calls = append(calls, PromQLFragment{
33 Fragment: node.Children[0],
34 BinExpr: n,
35 })
36 }
37
38 // bar * on() group_right(xxx) absent(foo{job="bar"})
39 // absent(foo{job="bar"}) * on(job) group_right(xxx) bar
40 case promParser.CardOneToMany:
41 if rn, ok := n.RHS.(*promParser.Call); ok && rn.Func.Name == "absent" {
42 calls = append(calls, PromQLFragment{
43 Fragment: node.Children[1],
44 BinExpr: n,
45 })
46 }
47
48 // bar AND absent(foo{job="bar"})
49 // bar OR absent(foo{job="bar"})
50 // bar UNLESS absent(foo{job="bar"})
51 case promParser.CardManyToMany:
52 if n.Op == promParser.LOR {
53 for _, child := range node.Children {
54 calls = append(calls, HasOuterAbsent(child)...)
55 }
56 } else {
57 if ln, ok := n.LHS.(*promParser.Call); ok && ln.Func.Name == "absent" {
58 calls = append(calls, PromQLFragment{
59 Fragment: node.Children[0],
60 BinExpr: n,
61 })
62 }
63 }
64
65 default:
66 log.Warn().Str("matching", n.VectorMatching.Card.String()).Msg("Unsupported VectorMatching operation")
67 }
68 return
69 }
70 }
71
72 for _, child := range node.Children {
73 calls = append(calls, HasOuterAbsent(child)...)
74 }
75
76 return calls
77}
78