diff --git a/go.mod b/go.mod index 6bf359ff70ae2..91ec88fa382a6 100644 --- a/go.mod +++ b/go.mod @@ -124,7 +124,7 @@ require ( github.com/wvanbergen/kazoo-go v0.0.0-20180202103751-f72d8611297a // indirect github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c github.com/yuin/gopher-lua v0.0.0-20180630135845-46796da1b0b4 // indirect - go.starlark.net v0.0.0-20210312235212-74c10e2c17dc + go.starlark.net v0.0.0-20210406145628-7a1108eaa012 go.uber.org/multierr v1.6.0 // indirect golang.org/x/net v0.0.0-20201209123823-ac852fbbde11 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d diff --git a/go.sum b/go.sum index 2fcbe42fd5fde..d67550751eed1 100644 --- a/go.sum +++ b/go.sum @@ -1134,8 +1134,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.starlark.net v0.0.0-20210312235212-74c10e2c17dc h1:pVkptfeOTFfx+zXZo7HEHN3d5LmhatBFvHdm/f2QnpY= -go.starlark.net v0.0.0-20210312235212-74c10e2c17dc/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0= +go.starlark.net v0.0.0-20210406145628-7a1108eaa012 h1:4RGobP/iq7S22H0Bb92OEt+M8/cfBQnW+T+a2MC0sQo= +go.starlark.net v0.0.0-20210406145628-7a1108eaa012/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= diff --git a/plugins/processors/starlark/README.md b/plugins/processors/starlark/README.md index e30ea506c13f7..6372aedcea3b9 100644 --- a/plugins/processors/starlark/README.md +++ b/plugins/processors/starlark/README.md @@ -230,7 +230,8 @@ def apply(metric): - [scale](/plugins/processors/starlark/testdata/scale.star) - Multiply any field by a number - [time date](/plugins/processors/starlark/testdata/time_date.star) - Parse a date and extract the year, month and day from it. - [time duration](/plugins/processors/starlark/testdata/time_duration.star) - Parse a duration and convert it into a total amount of seconds. -- [time timestamp](/plugins/processors/starlark/testdata/time_timestamp.star) - Filter metrics based on the timestamp. +- [time timestamp](/plugins/processors/starlark/testdata/time_timestamp.star) - Filter metrics based on the timestamp in seconds. +- [time timestamp nanoseconds](/plugins/processors/starlark/testdata/time_timestamp_nanos.star) - Filter metrics based on the timestamp with nanoseconds. - [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. diff --git a/plugins/processors/starlark/testdata/time_timestamp.star b/plugins/processors/starlark/testdata/time_timestamp.star index dc1cbaea0296d..73e885b26c3dc 100644 --- a/plugins/processors/starlark/testdata/time_timestamp.star +++ b/plugins/processors/starlark/testdata/time_timestamp.star @@ -1,5 +1,4 @@ -# Example of filtering metrics based on the timestamp. Beware the built-in function from_timestamp -# only supports timestamps in seconds. +# Example of filtering metrics based on the timestamp in seconds. # # Example Input: # time result="KO" 1616020365100400201 diff --git a/plugins/processors/starlark/testdata/time_timestamp_nanos.star b/plugins/processors/starlark/testdata/time_timestamp_nanos.star new file mode 100644 index 0000000000000..d305cb1f22e9f --- /dev/null +++ b/plugins/processors/starlark/testdata/time_timestamp_nanos.star @@ -0,0 +1,22 @@ +# Example of filtering metrics based on the timestamp with nanoseconds. +# +# Example Input: +# time result="KO" 1617900602123455999 +# time result="OK" 1617900602123456789 +# +# Example Output: +# time result="OK" 1617900602123456789 + +load('time.star', 'time') +# loads time.parse_duration(), time.is_valid_timezone(), time.now(), time.time(), +# time.parse_time() and time.from_timestamp() + +def apply(metric): + # 1617900602123457000 nanosec = Thursday, April 8, 2021 16:50:02.123457000 GMT + refDate = time.from_timestamp(1617900602, 123457000) + # 1617900602123455999 nanosec = Thursday, April 8, 2021 16:50:02.123455999 GMT + # 1617900602123456789 nanosec = Thursday, April 8, 2021 16:50:02.123456789 GMT + metric_date = time.from_timestamp(int(metric.time / 1e9), int(metric.time % 1e9)) + # Only keep metrics with a timestamp that is not more than 1 microsecond before the reference date + if refDate - time.parse_duration("1us") < metric_date: + return metric