Skip to content

Commit

Permalink
Show how to return a custom error with the Starlark processor (influx…
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored Nov 20, 2020
1 parent f520249 commit a64e426
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions plugins/processors/starlark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def failing(metric):
- [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 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.

Expand Down
47 changes: 45 additions & 2 deletions plugins/processors/starlark/starlark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -2937,7 +2955,11 @@ func TestAllScriptTestData(t *testing.T) {
require.NoError(t, err)
lines := strings.Split(string(b), "\n")
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{
Script: fn,
Log: testutil.Logger{},
Expand All @@ -2951,7 +2973,11 @@ func TestAllScriptTestData(t *testing.T) {

for _, m := range inputMetrics {
err = plugin.Add(m, acc)
require.NoError(t, err)
if expectedErrorStr != "" {
require.EqualError(t, err, expectedErrorStr)
} else {
require.NoError(t, err)
}
}

err = plugin.Stop()
Expand Down Expand Up @@ -2992,3 +3018,20 @@ func parseMetricsFrom(t *testing.T, lines []string, header string) (metrics []te
}
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], "# ")
}
13 changes: 13 additions & 0 deletions plugins/processors/starlark/testdata/fail.star
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a64e426

Please sign in to comment.