forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show how to return several metrics with the Starlark processor (influ…
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
plugins/processors/starlark/testdata/multiple_metrics.star
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
26 changes: 26 additions & 0 deletions
26
plugins/processors/starlark/testdata/multiple_metrics_with_json.star
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |