Skip to content

Commit

Permalink
Allow to specify nanoseconds to timestamp in Starlark Processor (infl…
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored Apr 8, 2021
1 parent 8e7da35 commit c66ccee
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion plugins/processors/starlark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions plugins/processors/starlark/testdata/time_timestamp.star
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 22 additions & 0 deletions plugins/processors/starlark/testdata/time_timestamp_nanos.star
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c66ccee

Please sign in to comment.