This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
expression_prefix.go
78 lines (61 loc) · 1.66 KB
/
expression_prefix.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
package tagquery
import (
"io"
"strings"
"github.com/grafana/metrictank/schema"
)
type expressionPrefix struct {
expressionCommon
}
func (e *expressionPrefix) Equals(other Expression) bool {
return e.key == other.GetKey() && e.GetOperator() == other.GetOperator() && e.value == other.GetValue()
}
func (e *expressionPrefix) GetDefaultDecision() FilterDecision {
return Fail
}
func (e *expressionPrefix) GetOperator() ExpressionOperator {
return PREFIX
}
func (e *expressionPrefix) GetOperatorCost() uint32 {
return 3
}
func (e *expressionPrefix) RequiresNonEmptyValue() bool {
// we know it requires an non-empty value, because the expression
// "__tag^=" would get parsed into the type expressionMatchAll
return true
}
func (e *expressionPrefix) Matches(value string) bool {
return strings.HasPrefix(value, e.value)
}
func (e *expressionPrefix) GetMetricDefinitionFilter(_ IdTagLookup) MetricDefinitionFilter {
prefix := e.key + "="
matchString := prefix + e.value
if e.key == "name" {
return func(_ schema.MKey, name string, _ []string) FilterDecision {
if strings.HasPrefix(schema.SanitizeNameAsTagValue(name), e.value) {
return Pass
}
return Fail
}
}
resultIfTagIsAbsent := None
if !MetaTagSupport {
resultIfTagIsAbsent = Fail
}
return func(_ schema.MKey, _ string, tags []string) FilterDecision {
for _, tag := range tags {
if strings.HasPrefix(tag, matchString) {
return Pass
}
if strings.HasPrefix(tag, prefix) {
return Fail
}
}
return resultIfTagIsAbsent
}
}
func (e *expressionPrefix) StringIntoWriter(writer io.Writer) {
writer.Write([]byte(e.key))
writer.Write([]byte("^="))
writer.Write([]byte(e.value))
}