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

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

With date time sprig template functions, we will be able to filter test jobs which create time is 1 day before as the following:
```
{job="test"} | label_format nowEpoch=`{{(unixEpoch now)}}`,createDateEpoch=`{{unixEpoch (toDate "2006-01-02" .createDate)}}` | label_format dateTimeDiff="{{sub .nowEpoch .createDateEpoch}}" | dateTimeDiff > 86400
```

* add documents for date time sprig template functions

* resolve comments

* add golang datetime layout doc reference
* update CHANGELOG.md accordingly to include this PR
pull/4612/head
Garrett 4 years ago committed by GitHub
parent 96130ae389
commit 1c7df620c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 37
      docs/sources/logql/template_functions.md
  3. 4
      pkg/logql/log/fmt.go
  4. 21
      pkg/logql/log/fmt_test.go

@ -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)

@ -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
```

@ -76,6 +76,10 @@ var (
"floor",
"round",
"fromJson",
"date",
"toDate",
"now",
"unixEpoch",
}
)

@ -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) {

Loading…
Cancel
Save