Skip to content

Commit

Permalink
Add fromJson to the template stage. (#3736)
Browse files Browse the repository at this point in the history
Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored May 25, 2021
1 parent bc10123 commit e5a3afe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/sources/logql/template_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,19 @@ Signature: `toFloat64(v interface{}) float64`
```template
{{ "3.5" | float64 }} //output 3.5
```

## fromJson

> **Note:** Added in Loki 2.3.
fromJson decodes a JSON document into a structure. If the input cannot be decoded as JSON the function will return an empty string.

```template
fromJson "{\"foo\": 55}"
```

Example of a query to print a newline per queries stored as a json array in the log line:

```logql
{job="cortex/querier"} |= "finish in prometheus" | logfmt | line_format "{{ range $q := fromJson .queries }} {{ $q.query }} {{ end }}"
```
1 change: 1 addition & 0 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var (
"ceil",
"floor",
"round",
"fromJson",
}
)

Expand Down

0 comments on commit e5a3afe

Please sign in to comment.