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

Show how to return several metrics with the Starlark processor #8423

Merged
merged 1 commit into from
Nov 17, 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 @@ -187,6 +187,8 @@ def failing(metric):
- [scale](/plugins/processors/starlark/testdata/scale.star) - Multiply any field by a number
- [value filter](/plugins/processors/starlark/testdata/value_filter.star) - remove a metric based on a field value.
- [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.

[All examples](/plugins/processors/starlark/testdata) are in the testdata folder.

Expand Down
64 changes: 64 additions & 0 deletions plugins/processors/starlark/starlark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,70 @@ func TestScript(t *testing.T) {
),
},
},
{
name: "multiple_metrics",
plugin: &Starlark{
Script: "testdata/multiple_metrics.star",
Log: testutil.Logger{},
},
input: []telegraf.Metric{
testutil.MustMetric("mm",
map[string]string{},
map[string]interface{}{
"value": "a",
},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("mm2",
map[string]string{},
map[string]interface{}{
"value": "b",
},
time.Unix(0, 0),
),
testutil.MustMetric("mm1",
map[string]string{},
map[string]interface{}{
"value": "a",
},
time.Unix(0, 0),
),
},
},
{
name: "multiple_metrics_with_json",
plugin: &Starlark{
Script: "testdata/multiple_metrics_with_json.star",
Log: testutil.Logger{},
},
input: []telegraf.Metric{
testutil.MustMetric("json",
map[string]string{},
map[string]interface{}{
"value": "[{\"label\": \"hello\"}, {\"label\": \"world\"}]",
},
time.Unix(0, 0),
),
},
expected: []telegraf.Metric{
testutil.MustMetric("json",
map[string]string{},
map[string]interface{}{
"value": "hello",
},
time.Unix(0, 0),
),
testutil.MustMetric("json",
map[string]string{},
map[string]interface{}{
"value": "world",
},
time.Unix(0, 0),
),
},
},
}

for _, tt := range tests {
Expand Down
26 changes: 26 additions & 0 deletions plugins/processors/starlark/testdata/multiple_metrics.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Example showing how to create several metrics using the Starlark processor.
#
# Example Input:
# mm value="a" 1465839830100400201
#
# Example Output:
# mm2 value="b" 1465839830100400201
# mm1 value="a" 1465839830100400201

def apply(metric):
# Initialize a list of metrics
metrics = []
# Create a new metric whose name is "mm2"
metric2 = Metric("mm2")
# Set the field "value" to b
metric2.fields["value"] = "b"
# Reset the time (only needed for testing purpose)
metric2.time = 0
# Add metric2 to the list of metrics
metrics.append(metric2)
# Rename the original metric to "mm1"
metric.name = "mm1"
# Add metric to the list of metrics
metrics.append(metric)
# Return the created list of metrics
return metrics
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Example showing how to create several metrics from a json array.
#
# Example Input:
# json value="[{\"label\": \"hello\"}, {\"label\": \"world\"}]"
#
# Example Output:
# json value="hello" 1465839830100400201
# json value="world" 1465839830100400201

# loads json.encode(), json.decode(), json.indent()
load("json.star", "json")

def apply(metric):
# Initialize a list of metrics
metrics = []
# Loop over the json array stored into the field
for obj in json.decode(metric.fields['value']):
# Create a new metric whose name is "json"
current_metric = Metric("json")
# Set the field "value" to the label extracted from the current json object
current_metric.fields["value"] = obj["label"]
# Reset the time (only needed for testing purpose)
current_metric.time = 0
# Add metric to the list of metrics
metrics.append(current_metric)
return metrics