This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
expression_not_match.go
117 lines (96 loc) · 2.81 KB
/
expression_not_match.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tagquery
import (
"io"
"strings"
"sync"
"sync/atomic"
"github.com/grafana/metrictank/schema"
)
type expressionNotMatch struct {
expressionCommonRe
}
func (e *expressionNotMatch) Equals(other Expression) bool {
return e.key == other.GetKey() && e.GetOperator() == other.GetOperator() && e.value == other.GetValue()
}
func (e *expressionNotMatch) GetDefaultDecision() FilterDecision {
// if the pattern matches "" (f.e. "tag!=~.*) then a metric which
// does not have the tag "tag" at all should not be part of the
// result set
// docs: https://graphite.readthedocs.io/en/latest/tags.html
// > Any tag spec that matches an empty value is considered to
// > match series that don’t have that tag
if e.matchesEmpty {
return Fail
}
return Pass
}
func (e *expressionNotMatch) GetOperator() ExpressionOperator {
return NOT_MATCH
}
func (e *expressionNotMatch) GetOperatorCost() uint32 {
return 10
}
func (e *expressionNotMatch) RequiresNonEmptyValue() bool {
return e.matchesEmpty
}
func (e *expressionNotMatch) ResultIsSmallerWhenInverted() bool {
return true
}
func (e *expressionNotMatch) Matches(value string) bool {
return !e.valueRe.MatchString(value)
}
func (e *expressionNotMatch) GetMetricDefinitionFilter(_ IdTagLookup) MetricDefinitionFilter {
if e.key == "name" {
if e.value == "" {
// every metric has a name
return func(_ schema.MKey, _ string, _ []string) FilterDecision { return Pass }
}
return func(_ schema.MKey, name string, _ []string) FilterDecision {
if e.valueRe.MatchString(schema.SanitizeNameAsTagValue(name)) {
return Fail
}
return Pass
}
}
resultIfTagIsAbsent := None
if !MetaTagSupport {
resultIfTagIsAbsent = Pass
}
prefix := e.key + "="
var matchCache, missCache sync.Map
var currentMatchCacheSize, currentMissCacheSize int32
return func(_ schema.MKey, _ string, tags []string) FilterDecision {
for _, tag := range tags {
if !strings.HasPrefix(tag, prefix) {
continue
}
value := tag[len(prefix):]
// reduce regex matching by looking up cached non-matches
if _, ok := missCache.Load(value); ok {
return Pass
}
// reduce regex matching by looking up cached matches
if _, ok := matchCache.Load(value); ok {
return Fail
}
if e.valueRe.MatchString(value) {
if atomic.LoadInt32(¤tMatchCacheSize) < int32(MatchCacheSize) {
matchCache.Store(value, struct{}{})
atomic.AddInt32(¤tMatchCacheSize, 1)
}
return Fail
}
if atomic.LoadInt32(¤tMissCacheSize) < int32(MatchCacheSize) {
missCache.Store(value, struct{}{})
atomic.AddInt32(¤tMissCacheSize, 1)
}
return Pass
}
return resultIfTagIsAbsent
}
}
func (e *expressionNotMatch) StringIntoWriter(writer io.Writer) {
writer.Write([]byte(e.key))
writer.Write([]byte("!=~"))
writer.Write([]byte(e.value))
}