Skip to content

Commit

Permalink
LogQL: add the default sprig template function to logql label/line …
Browse files Browse the repository at this point in the history
…formatter (#6163)

* add the `default` sprig template function to logql label/line formatter

With the `default` sprig template function, we will be able to make it easier to output a default value if the source string is otherwise empty. Useful for json fields that can be missing, like HTTP headers in a log line that aren't required as in the following:

```
{job="access_log"} | json | line_format `{{.http_request_headers_x_forwarded_for | default "-"}}`
```

Previously this could only be done (that I found) with the much more verbose
```
{{regexReplaceAllLiteral "^$" .http_request_headers_x_forwarded_for "-"}}
```

* update changelog entry

* add pr number

* enhance docs

* Update docs/sources/logql/template_functions.md

Co-authored-by: Kaviraj Kanagaraj <kavirajkanagaraj@gmail.com>

* Update docs/sources/logql/template_functions.md

Co-authored-by: Vladyslav Diachenko <82767850+vlad-diachenko@users.noreply.github.com>

Co-authored-by: Kaviraj Kanagaraj <kavirajkanagaraj@gmail.com>
Co-authored-by: Owen Diehl <ow.diehl@gmail.com>
Co-authored-by: Vladyslav Diachenko <82767850+vlad-diachenko@users.noreply.github.com>
  • Loading branch information
4 people authored May 16, 2022
1 parent aa3f06f commit 97c7d7c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* [4860](https://github.com/grafana/loki/pull/4860) **cyriltovena**: Add rate limiting and metrics to hedging
* [4865](https://github.com/grafana/loki/pull/4865) **taisho6339**: Fix duplicate registry.MustRegister call in Promtail Kafka
* [4845](https://github.com/grafana/loki/pull/4845) **chaudum** Return error responses consistently as JSON
* [6163](https://github.com/grafana/loki/pull/6163) **jburnham**: LogQL: add `default` sprig template function in logql label/line formatter
## Unreleased

### All Changes
Expand Down
18 changes: 18 additions & 0 deletions docs/sources/logql/template_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,21 @@ Example of a query to filter Loki querier jobs which create time is 1 day before
```logql
{job="loki/querier"} | label_format nowEpoch=`{{(unixEpoch now)}}`,createDateEpoch=`{{unixEpoch (toDate "2006-01-02" .createDate)}}` | label_format dateTimeDiff="{{sub .nowEpoch .createDateEpoch}}" | dateTimeDiff > 86400
```

## default

`default` checks whether the string(`src`) is set, and returns default(`d`) if not set.

Signature: `default(d string, src string) string`

Examples:

```template
{{ default "-" "" }} // output: -
{{ default "-" "foo" }} // output: foo
```

Example of a query to print a `-` if the `http_request_headers_x_forwarded_for` label is empty:
```logql
{job="access_log"} | json | line_format `{{.http_request_headers_x_forwarded_for | default "-"}}`
```
1 change: 1 addition & 0 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var (
"toDate",
"now",
"unixEpoch",
"default",
}
)

Expand Down
16 changes: 16 additions & 0 deletions pkg/logql/log/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ func Test_lineFormatter_Format(t *testing.T) {
labels.Labels{{Name: "bar", Value: "2"}},
[]byte("1"),
},
{
"default",
newMustLineFormatter(`{{.foo | default "-" }}{{.bar | default "-"}}{{.unknown | default "-"}}`),
labels.Labels{{Name: "foo", Value: "blip"}, {Name: "bar", Value: ""}},
[]byte("blip--"),
labels.Labels{{Name: "foo", Value: "blip"}, {Name: "bar", Value: ""}},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -325,6 +333,14 @@ func Test_labelsFormatter_Format(t *testing.T) {
labels.Labels{{Name: "status", Value: "200"}},
labels.Labels{{Name: "status", Value: "2"}},
},
{
"default",
mustNewLabelsFormatter([]LabelFmt{
NewTemplateLabelFmt("blip", `{{.foo | default "-" }} and {{.bar}}`),
}),
labels.Labels{{Name: "bar", Value: "blop"}},
labels.Labels{{Name: "blip", Value: "- and blop"}, {Name: "bar", Value: "blop"}},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 97c7d7c

Please sign in to comment.