fix: starlark pop operation for non-existing keys (#9954)
This commit is contained in:
parent
e50b415ffd
commit
112ef7fc26
|
|
@ -175,6 +175,7 @@ func (d FieldDict) Delete(k starlark.Value) (v starlark.Value, found bool, err e
|
||||||
sv, err := asStarlarkValue(value)
|
sv, err := asStarlarkValue(value)
|
||||||
return sv, ok, err
|
return sv, ok, err
|
||||||
}
|
}
|
||||||
|
return starlark.None, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return starlark.None, false, errors.New("key must be of type 'str'")
|
return starlark.None, false, errors.New("key must be of type 'str'")
|
||||||
|
|
|
||||||
|
|
@ -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",
|
name: "popitem tags",
|
||||||
source: `
|
source: `
|
||||||
|
|
@ -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",
|
name: "popitem field",
|
||||||
source: `
|
source: `
|
||||||
|
|
|
||||||
|
|
@ -162,6 +162,7 @@ func (d TagDict) Delete(k starlark.Value) (v starlark.Value, found bool, err err
|
||||||
v := starlark.String(value)
|
v := starlark.String(value)
|
||||||
return v, ok, err
|
return v, ok, err
|
||||||
}
|
}
|
||||||
|
return starlark.None, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return starlark.None, false, errors.New("key must be of type 'str'")
|
return starlark.None, false, errors.New("key must be of type 'str'")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue