@@ -92,14 +92,13 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
92
92
records = append (records , record )
93
93
}
94
94
95
- // if we don't use an inverted set of meta records, then we check if
96
- // all meta records involved in a meta tag filter use the "=" operator.
97
- // if this is the case then it is cheaper to build a set of acceptable tags
95
+ // if a query only involves meta tags of which all underlying expressions
96
+ // use the "=" operator, then it is cheaper to build a set of acceptable tags
98
97
// based on the meta record expressions and just check whether they are present
99
98
// in a metric that gets filtered, compared to doing a full tag index lookup
100
99
// to check whether a metric has one of the necessary meta tags associated
101
100
// with it.
102
- onlyEqualOperators , singleExprPerRecord := viableOptimizations (invertSetOfMetaRecords , records )
101
+ onlyEqualOperators , singleExprPerRecord := viableOptimizations (records )
103
102
104
103
if onlyEqualOperators {
105
104
// there are two different ways how we optimize for the case where all expressions
@@ -108,9 +107,9 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
108
107
// otherwise we use the second way which is a bit more expensive but it also works
109
108
// if some of the involved meta records have multiple expressions.
110
109
if singleExprPerRecord {
111
- res .filters [i ].testByMetaTags = metaRecordFilterBySetOfValidValues (records )
110
+ res .filters [i ].testByMetaTags = metaRecordFilterBySetOfValidValues (records , invertSetOfMetaRecords )
112
111
} else {
113
- res .filters [i ].testByMetaTags = metaRecordFilterBySetOfValidValueSets (records )
112
+ res .filters [i ].testByMetaTags = metaRecordFilterBySetOfValidValueSets (records , invertSetOfMetaRecords )
114
113
}
115
114
} else {
116
115
metaRecordFilters := make ([]tagquery.MetricDefinitionFilter , 0 , len (records ))
@@ -132,16 +131,12 @@ func newIdFilter(expressions tagquery.Expressions, ctx *TagQueryContext) *idFilt
132
131
// viableOptimizations looks at a set of meta tag records and decides whether two possible
133
132
// optimizations can be applied when filtering by these records. it returns two bools to
134
133
// indicate which optimizations are or are not viable.
135
- // if invertSetOfMetaRecords is true then none of these optimizations can be used.
136
134
//
137
135
// * the first bool refers to the optimization for sets of records which all have only one
138
136
// expression and this expression is using the equal operator.
139
137
// * the second bool refers to the optimization for sets of records which all only have
140
138
// expressions using the equal operator, but there may be more than one per record.
141
- func viableOptimizations (invertSetOfMetaRecords bool , records []tagquery.MetaTagRecord ) (bool , bool ) {
142
- if invertSetOfMetaRecords {
143
- return false , false
144
- }
139
+ func viableOptimizations (records []tagquery.MetaTagRecord ) (bool , bool ) {
145
140
singleExprPerRecord := true
146
141
for recordIdx := range records {
147
142
for exprIdx := range records [recordIdx ].Expressions {
@@ -161,7 +156,9 @@ func viableOptimizations(invertSetOfMetaRecords bool, records []tagquery.MetaTag
161
156
// which only involves meta records of which each only has exactly one expression and that
162
157
// expression is using the "=" operator. this is quite a narrow scenario, but since it is
163
158
// a very common use case it makes sense to optimize for it.
164
- func metaRecordFilterBySetOfValidValues (records []tagquery.MetaTagRecord ) tagquery.MetricDefinitionFilter {
159
+ // The invertFilter bool flips the filter logic so that instead of removing metrics which
160
+ // do not have a meta tag it filters metrics which do have a meta tag.
161
+ func metaRecordFilterBySetOfValidValues (records []tagquery.MetaTagRecord , invertFilter bool ) tagquery.MetricDefinitionFilter {
165
162
// we first build a set of valid tags and names.
166
163
// since we know that each of the involved meta records uses exactly one expression
167
164
// which is using the "=" operator we know that if a given metric's name matches a
@@ -183,14 +180,19 @@ func metaRecordFilterBySetOfValidValues(records []tagquery.MetaTagRecord) tagque
183
180
}
184
181
}
185
182
183
+ resultOnHit := tagquery .Pass
184
+ if invertFilter {
185
+ resultOnHit = tagquery .Fail
186
+ }
187
+
186
188
return func (_ schema.MKey , name string , tags []string ) tagquery.FilterDecision {
187
189
for i := range tags {
188
190
if _ , ok := validValues [tags [i ]]; ok {
189
- return tagquery . Pass
191
+ return resultOnHit
190
192
}
191
193
}
192
194
if _ , ok := validNames [name ]; ok {
193
- return tagquery . Pass
195
+ return resultOnHit
194
196
}
195
197
return tagquery .None
196
198
}
@@ -199,7 +201,9 @@ func metaRecordFilterBySetOfValidValues(records []tagquery.MetaTagRecord) tagque
199
201
// metaRecordFilterBySetOfValidValueSets creates a filter function to filter by a meta tag
200
202
// which only involves meta records of which all expressions are only using the "=" operator,
201
203
// it is ok if one meta record uses multiple such expressions.
202
- func metaRecordFilterBySetOfValidValueSets (records []tagquery.MetaTagRecord ) tagquery.MetricDefinitionFilter {
204
+ // The invertFilter bool flips the filter logic so that instead of removing metrics which
205
+ // do not have a meta tag it filters metrics which do have a meta tag.
206
+ func metaRecordFilterBySetOfValidValueSets (records []tagquery.MetaTagRecord , invertFilter bool ) tagquery.MetricDefinitionFilter {
203
207
// we first build a set of tag and name value combinations of which each is sufficient
204
208
// to pass the generated filter when a metric contains all values of one of these
205
209
// combinations
@@ -221,6 +225,11 @@ func metaRecordFilterBySetOfValidValueSets(records []tagquery.MetaTagRecord) tag
221
225
sort .Strings (validValueSets [i ].tags )
222
226
}
223
227
228
+ resultOnHit := tagquery .Pass
229
+ if invertFilter {
230
+ resultOnHit = tagquery .Fail
231
+ }
232
+
224
233
return func (_ schema.MKey , name string , tags []string ) tagquery.FilterDecision {
225
234
// iterate over the acceptable value combinations and check if one matches this metric
226
235
for _ , validValueSet := range validValueSets {
@@ -231,7 +240,7 @@ func metaRecordFilterBySetOfValidValueSets(records []tagquery.MetaTagRecord) tag
231
240
}
232
241
233
242
if sliceContainsElements (validValueSet .tags , tags ) {
234
- return tagquery . Pass
243
+ return resultOnHit
235
244
}
236
245
}
237
246
0 commit comments