From e5a3afedff93b20745490f00a8896e364fdd04d9 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Tue, 25 May 2021 13:48:43 +0200 Subject: [PATCH] Add fromJson to the template stage. (#3736) Signed-off-by: Cyril Tovena --- docs/sources/logql/template_functions.md | 16 ++++++++++++++++ pkg/logql/log/fmt.go | 1 + 2 files changed, 17 insertions(+) diff --git a/docs/sources/logql/template_functions.md b/docs/sources/logql/template_functions.md index e28827842174d..403ee51e70b7c 100644 --- a/docs/sources/logql/template_functions.md +++ b/docs/sources/logql/template_functions.md @@ -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 }}" +``` diff --git a/pkg/logql/log/fmt.go b/pkg/logql/log/fmt.go index 0551d9557852b..2b5f04d570a7b 100644 --- a/pkg/logql/log/fmt.go +++ b/pkg/logql/log/fmt.go @@ -75,6 +75,7 @@ var ( "ceil", "floor", "round", + "fromJson", } )