Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starlark example dropbytype #8438

Merged
merged 7 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/processors/starlark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Attempting to modify the global scope will fail with an error.

### Examples

- [drop string fields](/plugins/processors/starlark/testdata/drop_string_fields.star) - Drop fields containing string values.
- [drop fields with unexpected type](/plugins/processors/starlark/testdata/drop_fields_with_unexpected_type.star) - Drop fields containing unexpected value types.
- [json](/plugins/processors/starlark/testdata/json.star) - an example of processing JSON from a field in a metric
- [number logic](/plugins/processors/starlark/testdata/number_logic.star) - transform a numerical value to another numerical value
- [pivot](/plugins/processors/starlark/testdata/pivot.star) - Pivots a key's value to be the key for another key.
Expand Down
65 changes: 65 additions & 0 deletions plugins/processors/starlark/starlark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,71 @@ func TestScript(t *testing.T) {
),
},
},
{
name: "drop fields by type",
plugin: &Starlark{
Script: "testdata/drop_string_fields.star",
Log: testutil.Logger{},
},
input: []telegraf.Metric{
testutil.MustMetric("device",
map[string]string{},
map[string]interface{}{
"a": 42,
"b": "42",
"c": 42.0,
"d": "42.0",
"e": true,
},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("device",
map[string]string{},
map[string]interface{}{
"a": 42,
"c": 42.0,
"e": true,
},
time.Unix(0, 0),
),
},
},
{
name: "drop fields with unexpected type",
plugin: &Starlark{
Script: "testdata/drop_fields_with_unexpected_type.star",
Log: testutil.Logger{},
},
input: []telegraf.Metric{
testutil.MustMetric("device",
map[string]string{},
map[string]interface{}{
"a": 42,
"b": "42",
"c": 42.0,
"d": "42.0",
"e": true,
"f": 23.0,
},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("device",
map[string]string{},
map[string]interface{}{
"a": 42,
"c": 42.0,
"d": "42.0",
"e": true,
"f": 23.0,
},
time.Unix(0, 0),
),
},
},
{
name: "scale",
plugin: &Starlark{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Drop fields if they NOT contain values of an expected type.
#
# In this example we ignore fields with an unknown expected type and do not drop them.
#
# Example Input:
# measurement,host=hostname a=1i,b=4.2,c=42.0,d="v3.14",e=true,f=23.0 1597255410000000000
# measurement,host=hostname a=1i,b="somestring",c=42.0,d="v3.14",e=true,f=23.0 1597255410000000000
#
# Example Output:
# measurement,host=hostname a=1i,b=4.2,c=42.0,d="v3.14",e=true,f=23.0 1597255410000000000
# measurement,host=hostname a=1i,c=42.0,d="v3.14",e=true,f=23.0 1597255410000000000

expected_type = {
"a": "int",
"b": "float",
"c": "float",
"d": "string",
"e": "bool"
}

def apply(metric):
for k, v in metric.fields.items():
if type(v) != expected_type.get(k, type(v)):
metric.fields.pop(k)
ssoroka marked this conversation as resolved.
Show resolved Hide resolved

return metric
14 changes: 14 additions & 0 deletions plugins/processors/starlark/testdata/drop_string_fields.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Drop fields if they contain a string.
#
# Example Input:
# measurement,host=hostname a=1,b="somestring" 1597255410000000000
#
# Example Output:
# measurement,host=hostname a=1 1597255410000000000

def apply(metric):
for k, v in metric.fields.items():
if type(v) == "string":
metric.fields.pop(k)

return metric