Skip to content

Commit

Permalink
Add the math module to the Starlark Processor (#9042)
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo committed Mar 24, 2021
1 parent 8564d92 commit 991efd5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/processors/starlark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def apply(metric):
- [drop fields with unexpected type](/plugins/processors/starlark/testdata/drop_fields_with_unexpected_type.star) - Drop fields containing unexpected value types.
- [iops](/plugins/processors/starlark/testdata/iops.star) - obtain IOPS (to aggregate, to produce max_iops)
- [json](/plugins/processors/starlark/testdata/json.star) - an example of processing JSON from a field in a metric
- [math](/plugins/processors/starlark/testdata/math.star) - Use a math function to compute the value of a field. [The list of the supported math functions and constants](https://pkg.go.dev/go.starlark.net/lib/math).
- [number logic](/plugins/processors/starlark/testdata/number_logic.star) - transform a numerical value to another numerical value
- [pivot](/plugins/processors/starlark/testdata/pivot.star) - Pivots a key's value to be the key for another key.
- [ratio](/plugins/processors/starlark/testdata/ratio.star) - Compute the ratio of two integer fields
Expand Down
5 changes: 5 additions & 0 deletions plugins/processors/starlark/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
"go.starlark.net/lib/math"
"go.starlark.net/lib/time"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
Expand Down Expand Up @@ -253,6 +254,10 @@ func loadFunc(module string, logger telegraf.Logger) (starlark.StringDict, error
return starlark.StringDict{
"log": LogModule(logger),
}, nil
case "math.star":
return starlark.StringDict{
"math": math.Module,
}, nil
case "time.star":
return starlark.StringDict{
"time": time.Module,
Expand Down
14 changes: 14 additions & 0 deletions plugins/processors/starlark/testdata/math.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Example showing how the math module can be used to compute the value of a field.
#
# Example Input:
# math value=10000i 1465839830100400201
#
# Example Output:
# math result=4 1465839830100400201

load('math.star', 'math')
# loads all the functions and constants defined in the math module

def apply(metric):
metric.fields["result"] = math.log(metric.fields.pop('value'), 10)
return metric

0 comments on commit 991efd5

Please sign in to comment.