Show how to return a custom error with the Starlark processor (#8439)
This commit is contained in:
parent
247230c5c9
commit
bbd4e80409
|
|
@ -189,6 +189,7 @@ def failing(metric):
|
||||||
- [logging](/plugins/processors/starlark/testdata/logging.star) - Log messages with the logger of Telegraf
|
- [logging](/plugins/processors/starlark/testdata/logging.star) - Log messages with the logger of Telegraf
|
||||||
- [multiple metrics](/plugins/processors/starlark/testdata/multiple_metrics.star) - Return multiple metrics by using [a list](https://docs.bazel.build/versions/master/skylark/lib/list.html) of metrics.
|
- [multiple metrics](/plugins/processors/starlark/testdata/multiple_metrics.star) - Return multiple metrics by using [a list](https://docs.bazel.build/versions/master/skylark/lib/list.html) of metrics.
|
||||||
- [multiple metrics from json array](/plugins/processors/starlark/testdata/multiple_metrics_with_json.star) - Builds a new metric from each element of a json array then returns all the created metrics.
|
- [multiple metrics from json array](/plugins/processors/starlark/testdata/multiple_metrics_with_json.star) - Builds a new metric from each element of a json array then returns all the created metrics.
|
||||||
|
- [custom error](/plugins/processors/starlark/testdata/fail.star) - Return a custom error with [fail](https://docs.bazel.build/versions/master/skylark/lib/globals.html#fail).
|
||||||
|
|
||||||
[All examples](/plugins/processors/starlark/testdata) are in the testdata folder.
|
[All examples](/plugins/processors/starlark/testdata) are in the testdata folder.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2624,6 +2624,24 @@ func TestScript(t *testing.T) {
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "fail",
|
||||||
|
plugin: &Starlark{
|
||||||
|
Script: "testdata/fail.star",
|
||||||
|
Log: testutil.Logger{},
|
||||||
|
},
|
||||||
|
input: []telegraf.Metric{
|
||||||
|
testutil.MustMetric("fail",
|
||||||
|
map[string]string{},
|
||||||
|
map[string]interface{}{
|
||||||
|
"value": 1,
|
||||||
|
},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
expected: []telegraf.Metric{},
|
||||||
|
expectedErrorStr: "fail: The field value should be greater than 1",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
@ -2937,7 +2955,11 @@ func TestAllScriptTestData(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
lines := strings.Split(string(b), "\n")
|
lines := strings.Split(string(b), "\n")
|
||||||
inputMetrics := parseMetricsFrom(t, lines, "Example Input:")
|
inputMetrics := parseMetricsFrom(t, lines, "Example Input:")
|
||||||
outputMetrics := parseMetricsFrom(t, lines, "Example Output:")
|
expectedErrorStr := parseErrorMessage(t, lines, "Example Output Error:")
|
||||||
|
outputMetrics := []telegraf.Metric{}
|
||||||
|
if expectedErrorStr == "" {
|
||||||
|
outputMetrics = parseMetricsFrom(t, lines, "Example Output:")
|
||||||
|
}
|
||||||
plugin := &Starlark{
|
plugin := &Starlark{
|
||||||
Script: fn,
|
Script: fn,
|
||||||
Log: testutil.Logger{},
|
Log: testutil.Logger{},
|
||||||
|
|
@ -2951,7 +2973,11 @@ func TestAllScriptTestData(t *testing.T) {
|
||||||
|
|
||||||
for _, m := range inputMetrics {
|
for _, m := range inputMetrics {
|
||||||
err = plugin.Add(m, acc)
|
err = plugin.Add(m, acc)
|
||||||
require.NoError(t, err)
|
if expectedErrorStr != "" {
|
||||||
|
require.EqualError(t, err, expectedErrorStr)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = plugin.Stop()
|
err = plugin.Stop()
|
||||||
|
|
@ -2992,3 +3018,20 @@ func parseMetricsFrom(t *testing.T, lines []string, header string) (metrics []te
|
||||||
}
|
}
|
||||||
return metrics
|
return metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parses error message out of line protocol following a header
|
||||||
|
func parseErrorMessage(t *testing.T, lines []string, header string) string {
|
||||||
|
require.NotZero(t, len(lines), "Expected some lines to parse from .star file, found none")
|
||||||
|
startIdx := -1
|
||||||
|
for i := range lines {
|
||||||
|
if strings.TrimLeft(lines[i], "# ") == header {
|
||||||
|
startIdx = i + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if startIdx == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
require.True(t, startIdx < len(lines), fmt.Sprintf("Expected to find the error message after %q, but found none", header))
|
||||||
|
return strings.TrimLeft(lines[startIdx], "# ")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Example of the way to return a custom error thanks to the built-in function fail
|
||||||
|
# Returning an error will drop the current metric. Consider using logging instead if you want to keep the metric.
|
||||||
|
#
|
||||||
|
# Example Input:
|
||||||
|
# fail value=1 1465839830100400201
|
||||||
|
#
|
||||||
|
# Example Output Error:
|
||||||
|
# fail: The field value should be greater than 1
|
||||||
|
|
||||||
|
def apply(metric):
|
||||||
|
if metric.fields["value"] <= 1:
|
||||||
|
return fail("The field value should be greater than 1")
|
||||||
|
return metric
|
||||||
Loading…
Reference in New Issue