Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add date time sprig template functions in logql label/line formatter #4603

Merged
merged 3 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [4570](https://github.com/grafana/loki/pull/4570) **DylanGuedes**: Loki: Append loopback to ingester net interface default list
* [4594](https://github.com/grafana/loki/pull/4594) **owen-d**: Configures unordered_writes=true by default
* [4574](https://github.com/grafana/loki/pull/4574) **slim-bean**: Loki: Add a ring to the compactor used to control concurrency when not running standalone
* [4603](https://github.com/grafana/loki/pull/4603) **garrettlish**: Add date time sprig template functions in logql label/line formatter

# 2.3.0 (2021/08/06)

Expand Down
37 changes: 37 additions & 0 deletions docs/sources/logql/template_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,40 @@ Example of a query to print a newline per queries stored as a json array in the
```logql
{job="cortex/querier"} |= "finish in prometheus" | logfmt | line_format "{{ range $q := fromJson .queries }} {{ $q.query }} {{ end }}"
```

## now

`now` returns the current local time.

```template
{{ now }}
```

## toDate

`toDate` parses a formatted string and returns the time value it represents.

```template
{{ toDate "2006-01-02" "2021-11-02" }}
```

## date

`date` returns a textual representation of the time value formatted according to the provided [golang datetime layout](https://pkg.go.dev/time#pkg-constants).

```template
{ date "2006-01-02" now }}
```

## unixEpoch

`unixEpoch` returns the number of seconds elapsed since January 1, 1970 UTC.

```template
{ unixEpoch now }}
```

Example of a query to filter cortex querier jobs which create time is 1 day before:
```logql
{job="cortex/querier"} | label_format nowEpoch=`{{(unixEpoch now)}}`,createDateEpoch=`{{unixEpoch (toDate "2006-01-02" .createDate)}}` | label_format dateTimeDiff="{{sub .nowEpoch .createDateEpoch}}" | dateTimeDiff > 86400
```
4 changes: 4 additions & 0 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ var (
"floor",
"round",
"fromJson",
"date",
"toDate",
"now",
"unixEpoch",
}
)

Expand Down
21 changes: 21 additions & 0 deletions pkg/logql/log/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ func Test_lineFormatter_Format(t *testing.T) {
[]byte("12"),
labels.Labels{{Name: "foo", Value: "2.5"}},
},
{
"datetime",
newMustLineFormatter("{{ sub (unixEpoch (toDate \"2006-01-02\" \"2021-11-02\")) (unixEpoch (toDate \"2006-01-02\" \"2021-11-01\")) }}"),
labels.Labels{},
[]byte("86400"),
labels.Labels{},
},
{
"dateformat",
newMustLineFormatter("{{ date \"2006-01-02\" (toDate \"2006-01-02\" \"2021-11-02\") }}"),
labels.Labels{},
[]byte("2021-11-02"),
labels.Labels{},
},
{
"now",
newMustLineFormatter("{{ div (unixEpoch now) (unixEpoch now) }}"),
labels.Labels{},
[]byte("1"),
labels.Labels{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down