@@ -3,11 +3,13 @@ package tagquery
3
3
import (
4
4
"fmt"
5
5
"strings"
6
+
7
+ "github.com/raintank/schema"
6
8
)
7
9
8
10
type MetaTagRecord struct {
9
- MetaTags Tags
10
- Expressions Expressions
11
+ MetaTags Tags `json:"metaTags"`
12
+ Expressions Expressions `json:"expressions"`
11
13
}
12
14
13
15
func ParseMetaTagRecord (metaTags []string , expressions []string ) (MetaTagRecord , error ) {
@@ -24,6 +26,15 @@ func ParseMetaTagRecord(metaTags []string, expressions []string) (MetaTagRecord,
24
26
return res , err
25
27
}
26
28
29
+ // we don't actually need to instantiate a query at this point, but we want to verify
30
+ // that it is possible to instantiate a query from the given meta record expressions.
31
+ // if we can't instantiate a query from the given expressions, then the meta record
32
+ // upsert request should be considered invalid and should get rejected.
33
+ _ , err = NewQuery (res .Expressions , 0 )
34
+ if err != nil {
35
+ return res , fmt .Errorf ("Failed to instantiate query from given expressions: %s" , err )
36
+ }
37
+
27
38
if len (res .Expressions ) == 0 {
28
39
return res , fmt .Errorf ("Meta Tag Record must have at least one query" )
29
40
}
@@ -87,3 +98,32 @@ func (m *MetaTagRecord) EqualExpressions(other *MetaTagRecord) bool {
87
98
func (m * MetaTagRecord ) HasMetaTags () bool {
88
99
return len (m .MetaTags ) > 0
89
100
}
101
+
102
+ func (m * MetaTagRecord ) GetMetricDefinitionFilter (lookup IdTagLookup ) MetricDefinitionFilter {
103
+ filters := make ([]MetricDefinitionFilter , len (m .Expressions ))
104
+ defaultDecisions := make ([]FilterDecision , len (m .Expressions ))
105
+ for i , expr := range m .Expressions {
106
+ filters [i ] = expr .GetMetricDefinitionFilter (lookup )
107
+ defaultDecisions [i ] = expr .GetDefaultDecision ()
108
+ }
109
+
110
+ return func (id schema.MKey , name string , tags []string ) FilterDecision {
111
+ for i := range filters {
112
+ decision := filters [i ](id , name , tags )
113
+
114
+ if decision == None {
115
+ return defaultDecisions [i ]
116
+ }
117
+
118
+ if decision == Pass {
119
+ continue
120
+ }
121
+
122
+ if decision == Fail {
123
+ return Fail
124
+ }
125
+ }
126
+
127
+ return Pass
128
+ }
129
+ }
0 commit comments