This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
expression_match_tag.go
96 lines (76 loc) · 2.14 KB
/
expression_match_tag.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
package tagquery
import (
"io"
"strings"
"sync"
"sync/atomic"
"github.com/grafana/metrictank/schema"
)
type expressionMatchTag struct {
expressionCommonRe
}
func (e *expressionMatchTag) Equals(other Expression) bool {
return e.key == other.GetKey() && e.GetOperator() == other.GetOperator() && e.value == other.GetValue()
}
func (e *expressionMatchTag) GetDefaultDecision() FilterDecision {
return Fail
}
func (e *expressionMatchTag) GetOperator() ExpressionOperator {
return MATCH_TAG
}
func (e *expressionMatchTag) GetOperatorCost() uint32 {
return 20
}
func (e *expressionMatchTag) OperatesOnTag() bool {
return true
}
func (e *expressionMatchTag) RequiresNonEmptyValue() bool {
return !e.matchesEmpty
}
func (e *expressionMatchTag) Matches(tag string) bool {
return e.valueRe.MatchString(tag)
}
func (e *expressionMatchTag) GetMetricDefinitionFilter(_ IdTagLookup) MetricDefinitionFilter {
if e.valueRe.Match([]byte("name")) {
// every metric has a tag name, so we can always return Pass
return func(_ schema.MKey, _ string, _ []string) FilterDecision { return Pass }
}
resultIfTagIsAbsent := None
if !MetaTagSupport {
resultIfTagIsAbsent = Fail
}
var matchCache, missCache sync.Map
var currentMatchCacheSize, currentMissCacheSize int32
return func(_ schema.MKey, _ string, tags []string) FilterDecision {
for _, tag := range tags {
values := strings.SplitN(tag, "=", 2)
if len(values) < 2 {
continue
}
value := values[0]
if _, ok := missCache.Load(value); ok {
continue
}
if _, ok := matchCache.Load(value); ok {
return Pass
}
if e.valueRe.MatchString(value) {
if atomic.LoadInt32(¤tMatchCacheSize) < int32(MatchCacheSize) {
matchCache.Store(value, struct{}{})
atomic.AddInt32(¤tMatchCacheSize, 1)
}
return Pass
}
if atomic.LoadInt32(¤tMissCacheSize) < int32(MatchCacheSize) {
missCache.Store(value, struct{}{})
atomic.AddInt32(¤tMissCacheSize, 1)
}
continue
}
return resultIfTagIsAbsent
}
}
func (e *expressionMatchTag) StringIntoWriter(writer io.Writer) {
writer.Write([]byte("__tag=~"))
writer.Write([]byte(e.value))
}