forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of the time module in Starlark Processor (influxdata#9004)
- Loading branch information
Showing
7 changed files
with
80 additions
and
9 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
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
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,19 @@ | ||
# Example of parsing a date out of a field and modifying the metric to inject the year, month and day. | ||
# | ||
# Example Input: | ||
# time value="2009-06-12T12:06:10.000000099" 1465839830100400201 | ||
# | ||
# Example Output: | ||
# time year=2009i,month=6i,day=12i 1465839830100400201 | ||
|
||
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): | ||
date = time.parse_time(metric.fields.get('value'), format="2006-01-02T15:04:05.999999999", location="UTC") | ||
metric.fields.pop('value') | ||
metric.fields["year"] = date.year | ||
metric.fields["month"] = date.month | ||
metric.fields["day"] = date.day | ||
return metric |
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,17 @@ | ||
# Example of parsing a duration out of a field and modifying the metric to inject the equivalent in seconds. | ||
# | ||
# Example Input: | ||
# time value="3m35s" 1465839830100400201 | ||
# | ||
# Example Output: | ||
# time seconds=215 1465839830100400201 | ||
|
||
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): | ||
duration = time.parse_duration(metric.fields.get('value')) | ||
metric.fields.pop('value') | ||
metric.fields["seconds"] = duration.seconds | ||
return metric |
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,23 @@ | ||
# Example of filtering metrics based on the timestamp. Beware the built-in function from_timestamp | ||
# only supports timestamps in seconds. | ||
# | ||
# Example Input: | ||
# time result="KO" 1616020365100400201 | ||
# time result="OK" 1616150517100400201 | ||
# | ||
# Example Output: | ||
# time result="OK" 1616150517100400201 | ||
|
||
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): | ||
# 1616198400 sec = Saturday, March 20, 2021 0:00:00 GMT | ||
refDate = time.from_timestamp(1616198400) | ||
# 1616020365 sec = Wednesday, March 17, 2021 22:32:45 GMT | ||
# 1616150517 sec = Friday, March 19, 2021 10:41:57 GMT | ||
metric_date = time.from_timestamp(int(metric.time / 1e9)) | ||
# Only keep metrics with a timestamp that is not more than 24 hours before the reference date | ||
if refDate - time.parse_duration("24h") < metric_date: | ||
return metric |