Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

Commit c94685a

Browse files
author
Steven Le Roux
authored
Merge pull request #52 from ovh/negFilter
feat(filterWithoutLabels): Add a method to keep series without some label keys
2 parents 45a4be0 + e2e3afe commit c94685a

File tree

4 files changed

+159
-115
lines changed

4 files changed

+159
-115
lines changed

spec/doc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ TSL includes a few methods to filter the metrics result set:
518518
* The **filterByLabels** method to keep only the metrics matching some labels rules defined as parameters. **filterByLabels** expects at least one label clause string, and optionally as many as needed. Use example: _.filterByLabels("label~42.*", "host=server-01")._
519519
* The **filterByName** method to keep only the metrics matching a name rule defined as parameters. **filterByName** expects a single string rule. Use example: _.filterByName("test")_, _.filterByName("~test")_
520520
* The **filterByLastValue** method to keep only the metrics matching a rule on their last value defined as parameters. **filterByLastValue** expects at least one string rule, and optionally as many as needed. Use example: _.filterByLastValue(">=42")_, _.filterByName("!='mystring'")_. The valid **filterByLastValue** parameters are **<=**, **<**, **!=**, **=**, **>=** and **>**.
521+
* The **filterWithoutLabels** method to keep only with metrics without the labels keys specified as parameters. Use example: _.filterWithoutLabels("label", "host")._
521522

522523
### Metrics operators on metrics sets
523524

tsl/generateWarpScript.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ func (protoParser *ProtoParser) getFrameworksOp(selectStatement SelectStatement,
289289
buffer.WriteString(filter)
290290
buffer.WriteString("\n")
291291

292+
case FILTERWITHOUTLABELS:
293+
filter, err := protoParser.filterWithoutLabelsWarpScript(framework)
294+
if err != nil {
295+
return "", err
296+
}
297+
buffer.WriteString(filter)
298+
buffer.WriteString("\n")
299+
292300
case ADDNAMESUFFIX:
293301
framework.operator = RENAME
294302

@@ -1120,6 +1128,39 @@ func (protoParser *ProtoParser) operators(framework FrameworkStatement) string {
11201128
return value + operatorString
11211129
}
11221130

1131+
func (protoParser *ProtoParser) filterWithoutLabelsWarpScript(framework FrameworkStatement) (string, error) {
1132+
1133+
var buffer bytes.Buffer
1134+
1135+
buffer.WriteString(`
1136+
<%
1137+
DUP
1138+
1139+
0 GET
1140+
1141+
SWAP FILTER
1142+
1143+
->SET
1144+
1145+
SWAP ->SET
1146+
1147+
SWAP 2 DUPN DIFFERENCE
1148+
1149+
SET-> SWAP SET->
1150+
1151+
ROT DROP
1152+
%>
1153+
'neg-filter' CSTORE
1154+
`)
1155+
1156+
for _, labelKey := range framework.unNamedAttributes {
1157+
buffer.WriteString(fmt.Sprintf("[ SWAP [] { '%s' '~.*' } filter.bylabels ] @neg-filter\n", labelKey.lit))
1158+
buffer.WriteString(" DROP \n")
1159+
}
1160+
1161+
return buffer.String(), nil
1162+
}
1163+
11231164
// operators generate WarpScript line for an individual statement
11241165
func (protoParser *ProtoParser) filterWarpScript(framework FrameworkStatement) (string, error) {
11251166
value := "[ SWAP [] "

tsl/genericParser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ loop:
737737
return nil, err
738738
}
739739

740-
case REMOVELABELS, FILTERBYLABELS:
740+
case REMOVELABELS, FILTERBYLABELS, FILTERWITHOUTLABELS:
741741
instruction, err = p.parseNStringOperator(tok, pos, lit, -1, instruction)
742742

743743
if err != nil {

tsl/token.go

Lines changed: 116 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const (
7474
FILTERBYLABELS
7575
FILTERBYNAME
7676
FILTERBYLASTVALUE
77+
FILTERWITHOUTLABELS
7778
FINITE
7879
FIRST
7980
FLOOR
@@ -196,120 +197,121 @@ var tokens = [...]string{
196197
SEMICOLON: ";",
197198
DOT: ".",
198199

199-
ABS: "abs",
200-
ADDNAMESUFFIX: "addSuffix",
201-
ADDNAMEPREFIX: "addPrefix",
202-
ADDSERIES: "add",
203-
ANDL: "and",
204-
ATTRIBUTEPOLICY: "attributePolicy",
205-
ATTRIBUTES: "attributes",
206-
BOTTOMN: "bottomN",
207-
BOTTOMNBY: "bottomNBy",
208-
CEIL: "ceil",
209-
CONNECT: "connect",
210-
COUNT: "count",
211-
CREATE: "create",
212-
CUMULATIVE: "cumulative",
213-
CUMULATIVESUM: "cumulativeSum",
214-
DAY: "day",
215-
DELTA: "delta",
216-
DIVSERIES: "div",
217-
EQUAL: "equal",
218-
FILL: "fill",
219-
FILTERBYLABELS: "filterByLabels",
220-
FILTERBYNAME: "filterByName",
221-
FILTERBYLASTVALUE: "filterByLastValue",
222-
FINITE: "finite",
223-
FIRST: "first",
224-
FLOOR: "floor",
225-
FROM: "from",
226-
GREATEROREQUAL: "greaterOrEqual",
227-
GREATERTHAN: "greaterThan",
228-
GROUP: "group",
229-
GROUPLEFT: "groupLeft",
230-
GROUPRIGHT: "groupRight",
231-
GROUPBY: "groupBy",
232-
GROUPWITHOUT: "groupWithout",
233-
HOUR: "hour",
234-
IGNORING: "ignoring",
235-
JOIN: "join",
236-
KEEPFIRSTVALUES: "keepFirstValues",
237-
KEEPLASTVALUES: "keepLastValues",
238-
KEEPFIRSTVALUE: "keepFirstValue",
239-
KEEPLASTVALUE: "keepLastValue",
240-
LAST: "last",
241-
LABELS: "labels",
242-
LESSOREQUAL: "lessOrEqual",
243-
LESSTHAN: "lessThan",
244-
LN: "ln",
245-
LOG2: "log2",
246-
LOG10: "log10",
247-
LOGN: "logN",
248-
MASK: "mask",
249-
MAX: "max",
250-
MAXWITH: "maxWith",
251-
MEAN: "mean",
252-
MEDIAN: "median",
253-
MIN: "min",
254-
MINWITH: "minWith",
255-
MINUTE: "minute",
256-
MONTH: "month",
257-
MULSERIES: "mul",
258-
NEGMASK: "negmask",
259-
NOTEQUAL: "notEqual",
260-
NOW: "now",
261-
NAMES: "names",
262-
ON: "on",
263-
ORL: "or",
264-
PERCENTILE: "percentile",
265-
PROM: "prom",
266-
PROMETHEUS: "prometheus",
267-
QUANTIZE: "quantize",
268-
RATE: "rate",
269-
REMOVELABELS: "removeLabels",
270-
REMOVE: "remove",
271-
RENAME: "rename",
272-
RENAMEBY: "renameBy",
273-
RENAMETEMPLATE: "renameTemplate",
274-
RENAMELABELKEY: "renameLabelKey",
275-
RENAMELABELVALUE: "renameLabelValue",
276-
RESETS: "resets",
277-
ROUND: "round",
278-
SAMPLE: "sample",
279-
SAMPLEBY: "sampleBy",
280-
SELECT: "select",
281-
SELECTORS: "selectors",
282-
SERIES: "series",
283-
SETLABELS: "setLabels",
284-
SETVALUES: "setValues",
285-
SHIFT: "shift",
286-
SHRINK: "shrink",
287-
SORT: "sort",
288-
SORTBY: "sortBy",
289-
SORTDESC: "sortDesc",
290-
SORTDESCBY: "sortDescBy",
291-
SQRT: "sqrt",
292-
STDDEV: "stddev",
293-
STDVAR: "stdvar",
294-
STORE: "store",
295-
SUBSERIES: "sub",
296-
SUM: "sum",
297-
TOBOOLEAN: "toboolean",
298-
TODOUBLE: "todouble",
299-
TOLONG: "tolong",
300-
TOPN: "topN",
301-
TOPNBY: "topNBy",
302-
TOSTRING: "tostring",
303-
TIMECLIP: "timeclip",
304-
TIMEMODULO: "timemodulo",
305-
TIMESTAMP: "timestamp",
306-
TIMESCALE: "timescale",
307-
TIMESPLIT: "timesplit",
308-
WARP: "warp10",
309-
WEEKDAY: "weekday",
310-
WHERE: "where",
311-
WINDOW: "window",
312-
YEAR: "year",
200+
ABS: "abs",
201+
ADDNAMESUFFIX: "addSuffix",
202+
ADDNAMEPREFIX: "addPrefix",
203+
ADDSERIES: "add",
204+
ANDL: "and",
205+
ATTRIBUTEPOLICY: "attributePolicy",
206+
ATTRIBUTES: "attributes",
207+
BOTTOMN: "bottomN",
208+
BOTTOMNBY: "bottomNBy",
209+
CEIL: "ceil",
210+
CONNECT: "connect",
211+
COUNT: "count",
212+
CREATE: "create",
213+
CUMULATIVE: "cumulative",
214+
CUMULATIVESUM: "cumulativeSum",
215+
DAY: "day",
216+
DELTA: "delta",
217+
DIVSERIES: "div",
218+
EQUAL: "equal",
219+
FILL: "fill",
220+
FILTERBYLABELS: "filterByLabels",
221+
FILTERBYNAME: "filterByName",
222+
FILTERBYLASTVALUE: "filterByLastValue",
223+
FILTERWITHOUTLABELS: "filterWithoutLabels",
224+
FINITE: "finite",
225+
FIRST: "first",
226+
FLOOR: "floor",
227+
FROM: "from",
228+
GREATEROREQUAL: "greaterOrEqual",
229+
GREATERTHAN: "greaterThan",
230+
GROUP: "group",
231+
GROUPLEFT: "groupLeft",
232+
GROUPRIGHT: "groupRight",
233+
GROUPBY: "groupBy",
234+
GROUPWITHOUT: "groupWithout",
235+
HOUR: "hour",
236+
IGNORING: "ignoring",
237+
JOIN: "join",
238+
KEEPFIRSTVALUES: "keepFirstValues",
239+
KEEPLASTVALUES: "keepLastValues",
240+
KEEPFIRSTVALUE: "keepFirstValue",
241+
KEEPLASTVALUE: "keepLastValue",
242+
LAST: "last",
243+
LABELS: "labels",
244+
LESSOREQUAL: "lessOrEqual",
245+
LESSTHAN: "lessThan",
246+
LN: "ln",
247+
LOG2: "log2",
248+
LOG10: "log10",
249+
LOGN: "logN",
250+
MASK: "mask",
251+
MAX: "max",
252+
MAXWITH: "maxWith",
253+
MEAN: "mean",
254+
MEDIAN: "median",
255+
MIN: "min",
256+
MINWITH: "minWith",
257+
MINUTE: "minute",
258+
MONTH: "month",
259+
MULSERIES: "mul",
260+
NEGMASK: "negmask",
261+
NOTEQUAL: "notEqual",
262+
NOW: "now",
263+
NAMES: "names",
264+
ON: "on",
265+
ORL: "or",
266+
PERCENTILE: "percentile",
267+
PROM: "prom",
268+
PROMETHEUS: "prometheus",
269+
QUANTIZE: "quantize",
270+
RATE: "rate",
271+
REMOVELABELS: "removeLabels",
272+
REMOVE: "remove",
273+
RENAME: "rename",
274+
RENAMEBY: "renameBy",
275+
RENAMETEMPLATE: "renameTemplate",
276+
RENAMELABELKEY: "renameLabelKey",
277+
RENAMELABELVALUE: "renameLabelValue",
278+
RESETS: "resets",
279+
ROUND: "round",
280+
SAMPLE: "sample",
281+
SAMPLEBY: "sampleBy",
282+
SELECT: "select",
283+
SELECTORS: "selectors",
284+
SERIES: "series",
285+
SETLABELS: "setLabels",
286+
SETVALUES: "setValues",
287+
SHIFT: "shift",
288+
SHRINK: "shrink",
289+
SORT: "sort",
290+
SORTBY: "sortBy",
291+
SORTDESC: "sortDesc",
292+
SORTDESCBY: "sortDescBy",
293+
SQRT: "sqrt",
294+
STDDEV: "stddev",
295+
STDVAR: "stdvar",
296+
STORE: "store",
297+
SUBSERIES: "sub",
298+
SUM: "sum",
299+
TOBOOLEAN: "toboolean",
300+
TODOUBLE: "todouble",
301+
TOLONG: "tolong",
302+
TOPN: "topN",
303+
TOPNBY: "topNBy",
304+
TOSTRING: "tostring",
305+
TIMECLIP: "timeclip",
306+
TIMEMODULO: "timemodulo",
307+
TIMESTAMP: "timestamp",
308+
TIMESCALE: "timescale",
309+
TIMESPLIT: "timesplit",
310+
WARP: "warp10",
311+
WEEKDAY: "weekday",
312+
WHERE: "where",
313+
WINDOW: "window",
314+
YEAR: "year",
313315
}
314316

315317
var keywords map[string]Token

0 commit comments

Comments
 (0)