Skip to content

Commit d832255

Browse files
Allow - in eql variable names (#710) (#716)
* fix to allow dashes in variable names in EQL expressions extend eql to allow the '-' char to appear in variable names, i.e., ${data.some-var} and additional test cases to eql, the transpiler, and the k8s provider to verify this works. Note that the bug was caused by the EQL limitation, the otehr test cases were added when attempting to find it. * Regenerate grammer with antlr 4.7.1, add CHANGELOG * Fix linter issue * Fix typo (cherry picked from commit 28721eb) Co-authored-by: Michel Laterman <82832767+michel-laterman@users.noreply.github.com>
1 parent a09594c commit d832255

File tree

9 files changed

+145
-109
lines changed

9 files changed

+145
-109
lines changed

CHANGELOG.next.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
- Update API calls that the agent makes to Kibana when running the container command. {pull}253[253]
103103
- diagnostics collect log names are fixed on Windows machines, command will ignore failures. AgentID is included in diagnostics(and diagnostics collect) output. {issue}81[81] {issue}92[92] {issue}190[190] {pull}262[262]
104104
- Collects stdout and stderr of applications run as a process and logs them. {issue}[88]
105+
- Remove VerificationMode option to empty string. Default value is `full`. {issue}[184]
106+
- diagnostics collect file mod times are set. {pull}570[570]
107+
- Allow the - char to appear as part of variable names in eql expressions. {issue}709[709] {pull}710[710]
105108

106109
==== New features
107110

internal/pkg/agent/transpiler/vars_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
func TestVars_Replace(t *testing.T) {
1818
vars := mustMakeVars(map[string]interface{}{
1919
"un-der_score": map[string]interface{}{
20-
"key1": "data1",
21-
"key2": "data2",
20+
"key1": "data1",
21+
"key2": "data2",
22+
"with-dash": "dash-value",
2223
"list": []string{
2324
"array1",
2425
"array2",
@@ -44,6 +45,12 @@ func TestVars_Replace(t *testing.T) {
4445
false,
4546
false,
4647
},
48+
{
49+
"${un-der_score.with-dash}",
50+
NewStrVal("dash-value"),
51+
false,
52+
false,
53+
},
4754
{
4855
"${un-der_score.missing}",
4956
NewStrVal(""),

internal/pkg/composable/providers/kubernetes/node_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func TestGenerateNodeData(t *testing.T) {
2626
Name: "testnode",
2727
UID: types.UID(uid),
2828
Labels: map[string]string{
29-
"foo": "bar",
29+
"foo": "bar",
30+
"with-dash": "dash-value",
3031
},
3132
Annotations: map[string]string{
3233
"baz": "ban",
@@ -54,7 +55,8 @@ func TestGenerateNodeData(t *testing.T) {
5455
"baz": "ban",
5556
},
5657
"labels": mapstr.M{
57-
"foo": "bar",
58+
"foo": "bar",
59+
"with-dash": "dash-value",
5860
},
5961
}
6062

@@ -64,7 +66,10 @@ func TestGenerateNodeData(t *testing.T) {
6466
"name": "devcluster",
6567
"url": "8.8.8.8:9090"},
6668
}, "kubernetes": mapstr.M{
67-
"labels": mapstr.M{"foo": "bar"},
69+
"labels": mapstr.M{
70+
"foo": "bar",
71+
"with-dash": "dash-value",
72+
},
6873
"annotations": mapstr.M{"baz": "ban"},
6974
"node": mapstr.M{
7075
"ip": "node1",
@@ -123,7 +128,8 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
123128
"ip": "node1",
124129
},
125130
"labels": mapstr.M{
126-
"foo": "bar",
131+
"foo": "bar",
132+
"with-dash": "dash-value",
127133
},
128134
"annotations": mapstr.M{
129135
"baz": "ban",

internal/pkg/composable/providers/kubernetes/pod_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func TestGeneratePodData(t *testing.T) {
2727
UID: types.UID(uid),
2828
Namespace: "testns",
2929
Labels: map[string]string{
30-
"foo": "bar",
30+
"foo": "bar",
31+
"with-dash": "dash-value",
3132
},
3233
Annotations: map[string]string{
3334
"app": "production",
@@ -59,7 +60,8 @@ func TestGeneratePodData(t *testing.T) {
5960
"nsa": "nsb",
6061
},
6162
"labels": mapstr.M{
62-
"foo": "bar",
63+
"foo": "bar",
64+
"with-dash": "dash-value",
6365
},
6466
"annotations": mapstr.M{
6567
"app": "production",
@@ -74,7 +76,8 @@ func TestGeneratePodData(t *testing.T) {
7476
}, "kubernetes": mapstr.M{
7577
"namespace": "testns",
7678
"labels": mapstr.M{
77-
"foo": "bar",
79+
"foo": "bar",
80+
"with-dash": "dash-value",
7881
},
7982
"annotations": mapstr.M{"app": "production"},
8083
"pod": mapstr.M{
@@ -119,7 +122,8 @@ func TestGenerateContainerPodData(t *testing.T) {
119122
UID: types.UID(uid),
120123
Namespace: "testns",
121124
Labels: map[string]string{
122-
"foo": "bar",
125+
"foo": "bar",
126+
"with-dash": "dash-value",
123127
},
124128
Annotations: map[string]string{
125129
"app": "production",
@@ -175,7 +179,8 @@ func TestGenerateContainerPodData(t *testing.T) {
175179
"app": "production",
176180
},
177181
"labels": mapstr.M{
178-
"foo": "bar",
182+
"foo": "bar",
183+
"with-dash": "dash-value",
179184
},
180185
}
181186

@@ -191,7 +196,9 @@ func TestGenerateContainerPodData(t *testing.T) {
191196
}, "kubernetes": mapstr.M{
192197
"namespace": "testns",
193198
"annotations": mapstr.M{"app": "production"},
194-
"labels": mapstr.M{"foo": "bar"},
199+
"labels": mapstr.M{"foo": "bar",
200+
"with-dash": "dash-value",
201+
},
195202
"pod": mapstr.M{
196203
"ip": "127.0.0.5",
197204
"name": "testpod",
@@ -232,7 +239,8 @@ func TestEphemeralContainers(t *testing.T) {
232239
UID: types.UID(uid),
233240
Namespace: "testns",
234241
Labels: map[string]string{
235-
"foo": "bar",
242+
"foo": "bar",
243+
"with-dash": "dash-value",
236244
},
237245
Annotations: map[string]string{
238246
"app": "production",
@@ -274,7 +282,8 @@ func TestEphemeralContainers(t *testing.T) {
274282
"ip": pod.Status.PodIP,
275283
},
276284
"labels": mapstr.M{
277-
"foo": "bar",
285+
"foo": "bar",
286+
"with-dash": "dash-value",
278287
},
279288
"container": mapstr.M{
280289
"id": "asdfghdeadbeef",
@@ -300,8 +309,10 @@ func TestEphemeralContainers(t *testing.T) {
300309
"name": "devcluster",
301310
"url": "8.8.8.8:9090"},
302311
}, "kubernetes": mapstr.M{
303-
"namespace": "testns",
304-
"labels": mapstr.M{"foo": "bar"},
312+
"namespace": "testns",
313+
"labels": mapstr.M{"foo": "bar",
314+
"with-dash": "dash-value",
315+
},
305316
"annotations": mapstr.M{"app": "production"},
306317
"pod": mapstr.M{
307318
"ip": "127.0.0.5",
@@ -383,7 +394,8 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
383394
"ip": k8sPod.Status.PodIP,
384395
},
385396
"labels": mapstr.M{
386-
"foo": "bar",
397+
"foo": "bar",
398+
"with-dash": "dash-value",
387399
},
388400
"annotations": mapstr.M{
389401
"app": "production",

internal/pkg/composable/providers/kubernetes/service_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func TestGenerateServiceData(t *testing.T) {
2525
UID: types.UID(uid),
2626
Namespace: "testns",
2727
Labels: map[string]string{
28-
"foo": "bar",
28+
"foo": "bar",
29+
"with-dash": "dash-value",
2930
},
3031
Annotations: map[string]string{
3132
"baz": "ban",
@@ -64,7 +65,8 @@ func TestGenerateServiceData(t *testing.T) {
6465
"baz": "ban",
6566
},
6667
"labels": mapstr.M{
67-
"foo": "bar",
68+
"foo": "bar",
69+
"with-dash": "dash-value",
6870
},
6971
}
7072

@@ -80,7 +82,8 @@ func TestGenerateServiceData(t *testing.T) {
8082
"ip": "1.2.3.4",
8183
},
8284
"labels": mapstr.M{
83-
"foo": "bar",
85+
"foo": "bar",
86+
"with-dash": "dash-value",
8487
},
8588
"annotations": mapstr.M{
8689
"baz": "ban",
@@ -139,7 +142,8 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
139142
"ip": "1.2.3.4",
140143
},
141144
"labels": mapstr.M{
142-
"foo": "bar",
145+
"foo": "bar",
146+
"with-dash": "dash-value",
143147
},
144148
"annotations": mapstr.M{
145149
"baz": "ban",

internal/pkg/eql/Eql.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ NUMBER: [\-]? [0-9]+;
2222
WHITESPACE: [ \r\n\t]+ -> skip;
2323
NOT: 'NOT' | 'not';
2424
NAME: [a-zA-Z_] [a-zA-Z0-9_]*;
25-
VNAME: [a-zA-Z0-9_.]+('.'[a-zA-Z0-9_]+)*;
25+
VNAME: [a-zA-Z0-9_.-]+('.'[a-zA-Z0-9_-]+)*;
2626
STEXT: '\'' ~[\r\n']* '\'';
2727
DTEXT: '"' ~[\r\n"]* '"';
2828
LPAR: '(';

internal/pkg/eql/eql_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func TestEql(t *testing.T) {
4242
{expression: "${env.MISSING|host.MISSING|true} == true", result: true},
4343
{expression: "${env.MISSING|host.MISSING|false} == false", result: true},
4444
{expression: "${'constant'} == 'constant'", result: true},
45+
{expression: "${data.with-dash} == 'dash-value'", result: true},
46+
{expression: "${'dash-value'} == 'dash-value'", result: true},
4547

4648
// boolean
4749
{expression: "true", result: true},
@@ -306,9 +308,10 @@ func TestEql(t *testing.T) {
306308

307309
store := &testVarStore{
308310
vars: map[string]interface{}{
309-
"env.HOSTNAME": "my-hostname",
310-
"host.name": "host-name",
311-
"data.array": []interface{}{"array1", "array2", "array3"},
311+
"env.HOSTNAME": "my-hostname",
312+
"host.name": "host-name",
313+
"data.array": []interface{}{"array1", "array2", "array3"},
314+
"data.with-dash": "dash-value",
312315
"data.dict": map[string]interface{}{
313316
"key1": "dict1",
314317
"key2": "dict2",
@@ -327,7 +330,7 @@ func TestEql(t *testing.T) {
327330
}
328331
t.Run(title, func(t *testing.T) {
329332
if showDebug == "1" {
330-
debug(test.expression)
333+
debug(t, test.expression)
331334
}
332335

333336
r, err := Eval(test.expression, store)
@@ -343,17 +346,17 @@ func TestEql(t *testing.T) {
343346
}
344347
}
345348

346-
func debug(expression string) {
349+
func debug(t *testing.T, expression string) {
347350
raw := antlr.NewInputStream(expression)
348351

349352
lexer := parser.NewEqlLexer(raw)
350353
for {
351-
t := lexer.NextToken()
352-
if t.GetTokenType() == antlr.TokenEOF {
354+
token := lexer.NextToken()
355+
if token.GetTokenType() == antlr.TokenEOF {
353356
break
354357
}
355-
fmt.Printf("%s (%q)\n",
356-
lexer.SymbolicNames[t.GetTokenType()], t.GetText())
358+
t.Logf("%s (%q)\n",
359+
lexer.SymbolicNames[token.GetTokenType()], token.GetText())
357360
}
358361
}
359362

0 commit comments

Comments
 (0)