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

fix: starlark pop operation for non-existing keys #9954

Merged
merged 2 commits into from
Oct 21, 2021
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
1 change: 1 addition & 0 deletions plugins/processors/starlark/field_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (d FieldDict) Delete(k starlark.Value) (v starlark.Value, found bool, err e
sv, err := asStarlarkValue(value)
return sv, ok, err
}
return starlark.None, false, nil
}

return starlark.None, false, errors.New("key must be of type 'str'")
Expand Down
90 changes: 90 additions & 0 deletions plugins/processors/starlark/starlark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,49 @@ def apply(metric):
),
},
},
{
name: "pop tag (default)",
source: `
def apply(metric):
metric.tags['host2'] = metric.tags.pop('url', 'foo.org')
return metric
`,
input: []telegraf.Metric{
testutil.MustMetric("cpu",
map[string]string{
"host": "example.org",
},
map[string]interface{}{"time_idle": 0},
time.Unix(0, 0),
),
testutil.MustMetric("cpu",
map[string]string{
"host": "example.org",
"url": "bar.org",
},
map[string]interface{}{"time_idle": 0},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("cpu",
map[string]string{
"host": "example.org",
"host2": "foo.org",
},
map[string]interface{}{"time_idle": 0},
time.Unix(0, 0),
),
testutil.MustMetric("cpu",
map[string]string{
"host": "example.org",
"host2": "bar.org",
},
map[string]interface{}{"time_idle": 0},
time.Unix(0, 0),
),
},
},
{
name: "popitem tags",
source: `
Expand Down Expand Up @@ -1773,6 +1816,53 @@ def apply(metric):
),
},
},
{
name: "pop field (default)",
source: `
def apply(metric):
metric.fields['idle_count'] = metric.fields.pop('count', 10)
return metric
`,
input: []telegraf.Metric{
testutil.MustMetric("cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 0,
"time_guest": 0,
},
time.Unix(0, 0),
),
testutil.MustMetric("cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 0,
"time_guest": 0,
"count": 0,
},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 0,
"time_guest": 0,
"idle_count": 10,
},
time.Unix(0, 0),
),
testutil.MustMetric("cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 0,
"time_guest": 0,
"idle_count": 0,
},
time.Unix(0, 0),
),
},
},
{
name: "popitem field",
source: `
Expand Down
1 change: 1 addition & 0 deletions plugins/processors/starlark/tag_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (d TagDict) Delete(k starlark.Value) (v starlark.Value, found bool, err err
v := starlark.String(value)
return v, ok, err
}
return starlark.None, false, nil
}

return starlark.None, false, errors.New("key must be of type 'str'")
Expand Down