Skip to content

Commit c66ccee

Browse files
authored
Allow to specify nanoseconds to timestamp in Starlark Processor (influxdata#9105)
1 parent 8e7da35 commit c66ccee

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ require (
124124
github.com/wvanbergen/kazoo-go v0.0.0-20180202103751-f72d8611297a // indirect
125125
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
126126
github.com/yuin/gopher-lua v0.0.0-20180630135845-46796da1b0b4 // indirect
127-
go.starlark.net v0.0.0-20210312235212-74c10e2c17dc
127+
go.starlark.net v0.0.0-20210406145628-7a1108eaa012
128128
go.uber.org/multierr v1.6.0 // indirect
129129
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11
130130
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
11341134
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
11351135
go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8=
11361136
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
1137-
go.starlark.net v0.0.0-20210312235212-74c10e2c17dc h1:pVkptfeOTFfx+zXZo7HEHN3d5LmhatBFvHdm/f2QnpY=
1138-
go.starlark.net v0.0.0-20210312235212-74c10e2c17dc/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=
1137+
go.starlark.net v0.0.0-20210406145628-7a1108eaa012 h1:4RGobP/iq7S22H0Bb92OEt+M8/cfBQnW+T+a2MC0sQo=
1138+
go.starlark.net v0.0.0-20210406145628-7a1108eaa012/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0=
11391139
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
11401140
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
11411141
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=

plugins/processors/starlark/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ def apply(metric):
230230
- [scale](/plugins/processors/starlark/testdata/scale.star) - Multiply any field by a number
231231
- [time date](/plugins/processors/starlark/testdata/time_date.star) - Parse a date and extract the year, month and day from it.
232232
- [time duration](/plugins/processors/starlark/testdata/time_duration.star) - Parse a duration and convert it into a total amount of seconds.
233-
- [time timestamp](/plugins/processors/starlark/testdata/time_timestamp.star) - Filter metrics based on the timestamp.
233+
- [time timestamp](/plugins/processors/starlark/testdata/time_timestamp.star) - Filter metrics based on the timestamp in seconds.
234+
- [time timestamp nanoseconds](/plugins/processors/starlark/testdata/time_timestamp_nanos.star) - Filter metrics based on the timestamp with nanoseconds.
234235
- [value filter](/plugins/processors/starlark/testdata/value_filter.star) - Remove a metric based on a field value.
235236
- [logging](/plugins/processors/starlark/testdata/logging.star) - Log messages with the logger of Telegraf
236237
- [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.

plugins/processors/starlark/testdata/time_timestamp.star

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Example of filtering metrics based on the timestamp. Beware the built-in function from_timestamp
2-
# only supports timestamps in seconds.
1+
# Example of filtering metrics based on the timestamp in seconds.
32
#
43
# Example Input:
54
# time result="KO" 1616020365100400201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Example of filtering metrics based on the timestamp with nanoseconds.
2+
#
3+
# Example Input:
4+
# time result="KO" 1617900602123455999
5+
# time result="OK" 1617900602123456789
6+
#
7+
# Example Output:
8+
# time result="OK" 1617900602123456789
9+
10+
load('time.star', 'time')
11+
# loads time.parse_duration(), time.is_valid_timezone(), time.now(), time.time(),
12+
# time.parse_time() and time.from_timestamp()
13+
14+
def apply(metric):
15+
# 1617900602123457000 nanosec = Thursday, April 8, 2021 16:50:02.123457000 GMT
16+
refDate = time.from_timestamp(1617900602, 123457000)
17+
# 1617900602123455999 nanosec = Thursday, April 8, 2021 16:50:02.123455999 GMT
18+
# 1617900602123456789 nanosec = Thursday, April 8, 2021 16:50:02.123456789 GMT
19+
metric_date = time.from_timestamp(int(metric.time / 1e9), int(metric.time % 1e9))
20+
# Only keep metrics with a timestamp that is not more than 1 microsecond before the reference date
21+
if refDate - time.parse_duration("1us") < metric_date:
22+
return metric

0 commit comments

Comments
 (0)