logql: Support `urlencode` and `urldecode` template functions (#8271)

Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>
pull/8278/head
Kaviraj Kanagaraj 3 years ago committed by GitHub
parent 3472aece09
commit 807aa459d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 30
      docs/sources/logql/template_functions.md
  3. 3
      pkg/logql/log/fmt.go
  4. 22
      pkg/logql/log/fmt_test.go

@ -27,6 +27,7 @@
* [8061](https://github.com/grafana/loki/pull/8061) **kavirajk**: Remove circle from Loki OSS
* [8131](https://github.com/grafana/loki/pull/8131) **jeschkies**: Compile Promtail ARM and ARM64 with journald support.
* [8212](https://github.com/grafana/loki/pull/8212) **kavirajk**: ingester: Add `ingester_memory_streams_labels_bytes metric` for more visibility of size of metadata of in-memory streams.
* [8271](https://github.com/grafana/loki/pull/8271) **kavirajk**: logql: Support urlencode and urldecode template functions
##### Fixes

@ -716,3 +716,33 @@ Example of a query to print how many times XYZ occurs in a line:
```logql
{job="xyzlog"} | line_format `{{ __line__ | count "XYZ"}}`
```
## urlencode
Use this function to encode the URL(s) in log messages.
Signature:
`urlencode(string) string`
Examples:
```template
"{{ .request_url | urlencode }}"
`{{ urlencode .request_url}}`
```
## urldecode
Use this function to decode the URL(s) in log messages.
Signature:
`urldecode(string) string`
Examples:
```template
"{{ .request_url | urldecode }}"
`{{ urldecode .request_url}}`
```

@ -3,6 +3,7 @@ package log
import (
"bytes"
"fmt"
"net/url"
"strings"
"text/template"
"text/template/parse"
@ -57,6 +58,8 @@ var (
matches := r.FindAllStringIndex(s, -1)
return len(matches), nil
},
"urldecode": url.QueryUnescape,
"urlencode": url.QueryEscape,
}
// sprig template functions

@ -111,6 +111,28 @@ func Test_lineFormatter_Format(t *testing.T) {
labels.Labels{{Name: "foo", Value: "BLIp"}, {Name: "bar", Value: "blop"}},
nil,
},
{
"urlencode",
newMustLineFormatter(`{{.foo | urlencode }} {{ urlencode .foo }}`), // assert both syntax forms
labels.Labels{
{Name: "foo", Value: `/loki/api/v1/query?query=sum(count_over_time({stream_filter="some_stream",environment="prod", host=~"someec2.*"}`},
},
0,
[]byte("%2Floki%2Fapi%2Fv1%2Fquery%3Fquery%3Dsum%28count_over_time%28%7Bstream_filter%3D%22some_stream%22%2Cenvironment%3D%22prod%22%2C+host%3D~%22someec2.%2A%22%7D %2Floki%2Fapi%2Fv1%2Fquery%3Fquery%3Dsum%28count_over_time%28%7Bstream_filter%3D%22some_stream%22%2Cenvironment%3D%22prod%22%2C+host%3D~%22someec2.%2A%22%7D"),
labels.Labels{{Name: "foo", Value: `/loki/api/v1/query?query=sum(count_over_time({stream_filter="some_stream",environment="prod", host=~"someec2.*"}`}},
nil,
},
{
"urldecode",
newMustLineFormatter(`{{.foo | urldecode }} {{ urldecode .foo }}`), // assert both syntax forms
labels.Labels{
{Name: "foo", Value: `%2Floki%2Fapi%2Fv1%2Fquery%3Fquery%3Dsum%28count_over_time%28%7Bstream_filter%3D%22some_stream%22%2Cenvironment%3D%22prod%22%2C+host%3D~%22someec2.%2A%22%7D`},
},
0,
[]byte(`/loki/api/v1/query?query=sum(count_over_time({stream_filter="some_stream",environment="prod", host=~"someec2.*"} /loki/api/v1/query?query=sum(count_over_time({stream_filter="some_stream",environment="prod", host=~"someec2.*"}`),
labels.Labels{{Name: "foo", Value: `%2Floki%2Fapi%2Fv1%2Fquery%3Fquery%3Dsum%28count_over_time%28%7Bstream_filter%3D%22some_stream%22%2Cenvironment%3D%22prod%22%2C+host%3D~%22someec2.%2A%22%7D`}},
nil,
},
{
"repeat",
newMustLineFormatter(`{{ "foo" | repeat 3 }}`),

Loading…
Cancel
Save