Skip to content

Commit 2271f43

Browse files
authored
added addField option to transform filter (#390)
1 parent 1fe2be0 commit 2271f43

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ Following is the supported API format for filter transformations:
130130
type: (enum) one of the following:
131131
remove_field: removes the field from the entry
132132
remove_entry_if_exists: removes the entry if the field exists
133-
remove_entry_if_doesnt_exist: removes the entry if the field doesnt exist
133+
remove_entry_if_doesnt_exist: removes the entry if the field does not exist
134134
remove_entry_if_equal: removes the entry if the field value equals specified value
135135
remove_entry_if_not_equal: removes the entry if the field value does not equal specified value
136+
add_field_if_doesnt_exist: adds a field to the entry if the field does not exist
136137
value: specified value of input field:
137138
</pre>
138139
## Transform Network API

pkg/api/transform_filter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ type TransformFilter struct {
2424
type TransformFilterOperationEnum struct {
2525
RemoveField string `yaml:"remove_field" json:"remove_field" doc:"removes the field from the entry"`
2626
RemoveEntryIfExists string `yaml:"remove_entry_if_exists" json:"remove_entry_if_exists" doc:"removes the entry if the field exists"`
27-
RemoveEntryIfDoesntExist string `yaml:"remove_entry_if_doesnt_exist" json:"remove_entry_if_doesnt_exist" doc:"removes the entry if the field doesnt exist"`
27+
RemoveEntryIfDoesntExist string `yaml:"remove_entry_if_doesnt_exist" json:"remove_entry_if_doesnt_exist" doc:"removes the entry if the field does not exist"`
2828
RemoveEntryIfEqual string `yaml:"remove_entry_if_equal" json:"remove_entry_if_equal" doc:"removes the entry if the field value equals specified value"`
2929
RemoveEntryIfNotEqual string `yaml:"remove_entry_if_not_equal" json:"remove_entry_if_not_equal" doc:"removes the entry if the field value does not equal specified value"`
30+
AddFieldIfDoesntExist string `yaml:"add_field_if_doesnt_exist" json:"add_field_if_doesnt_exist" doc:"adds a field to the entry if the field does not exist"`
3031
}
3132

3233
func TransformFilterOperationName(operation string) string {

pkg/pipeline/transform/transform_filter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (f *Filter) Transform(entry config.GenericMap) (config.GenericMap, bool) {
5858
return nil, false
5959
}
6060
}
61+
case api.TransformFilterOperationName("AddFieldIfDoesntExist"):
62+
if _, ok := entry[rule.Input]; !ok {
63+
outputEntry[rule.Input] = rule.Value
64+
}
6165
default:
6266
tlog.Panicf("unknown type %s for transform.Filter rule: %v", rule.Type, rule)
6367
}

pkg/pipeline/transform/transform_filter_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ parameters:
101101
value: "test message"
102102
`
103103

104+
const testConfigTransformFilterAddField = `---
105+
log-level: debug
106+
pipeline:
107+
- name: filter1
108+
parameters:
109+
- name: filter1
110+
transform:
111+
type: filter
112+
filter:
113+
rules:
114+
- input: dstPort
115+
type: add_field_if_doesnt_exist
116+
value: dummy_value
117+
- input: dummy_field
118+
type: add_field_if_doesnt_exist
119+
value: dummy_value
120+
`
121+
104122
func getFilterExpectedOutput() config.GenericMap {
105123
return config.GenericMap{
106124
"srcIP": "10.0.0.1",
@@ -183,6 +201,26 @@ func TestNewTransformFilterRemoveEntryIfNotEqual(t *testing.T) {
183201
require.False(t, ok)
184202
}
185203

204+
func TestNewTransformFilterAddField(t *testing.T) {
205+
newTransform := InitNewTransformFilter(t, testConfigTransformFilterAddField)
206+
transformFilter := newTransform.(*Filter)
207+
require.Len(t, transformFilter.Rules, 2)
208+
209+
input := test.GetIngestMockEntry(false)
210+
output, ok := transformFilter.Transform(input)
211+
require.True(t, ok)
212+
require.Equal(t, 22, output["dstPort"])
213+
require.Equal(t, "dummy_value", output["dummy_field"])
214+
215+
input = test.GetIngestMockEntry(false)
216+
input["dstPort"] = 3490
217+
input["dummy_field"] = 1
218+
output, ok = transformFilter.Transform(input)
219+
require.True(t, ok)
220+
require.Equal(t, 3490, output["dstPort"])
221+
require.Equal(t, 1, output["dummy_field"])
222+
}
223+
186224
func InitNewTransformFilter(t *testing.T, configFile string) Transformer {
187225
v, cfg := test.InitConfig(t, configFile)
188226
require.NotNil(t, v)

0 commit comments

Comments
 (0)