Parser expression can parse and extract labels from the log content. Those extracted labels can then be used for filtering using label filter expressions or for metric aggregations.
Extracted label keys are automatically sanitized by all parsers, to follow Prometheus metric name convention.(They can only contain ASCII letters and digits, as well as underscores and colons. They cannot start with a digit.)
Loki supports JSON, logfmt, pattern, regexp and unpack parsers and each one is also supported by ParserExpression
🎉
class ParserExpression {}
Initializes a new instance of the ParserExpression
class with an optional LogQL query string.
Available init
types are:
string
- Create an instance ofParserExpression
given a LogQL query to parse.ParserExpression
- Create an instance ofParserExpression
based on anotherParserExpression
class.
Add a json
parser expression.
Argument can be a string representing a label equal to a json field or an object reprensenting a map from each label to its json field.
Add a logfmt
parser expression.
Argument can be a string representing a label to extract or an object reprensenting a map from each label to its renamed label.
Add a pattern
parser expression.
Argument can be a string representing a pattern expression or an array of strings reprensenting multiple pattern expressions.
Add a regexp
parser expression.
Argument can be a string representing a regexp expression or an array of strings reprensenting multiple regexp expressions.
Enable unpack
parser, unpacking all embedded labels from Promtail’s pack stage.
When we transform the LogQL to a string we use two distinct methods:
- lowStringEnd (includes pattern and regexp)
- highStringEnd (includes json, logfmt and unpack)
toString() {
return `
${this.streamSelector.toString()}
${this.lineFilters.toString()}
${this.parserExpression.lowStringEnd()}
${this.labelFilters.toString()}
${this.parserExpression.highStringEnd()}
`.trim().replace(/\s\s+/g, " ");
}
We need to do this because LabelFilters need pattern and regexp to be declared before.
Here a real example;
{app=\"discussion\",env=\"production\"} |= `returned ` | regexp `((?P<execTime>[0-9.]+)ms)` | execTime > 500
The regexp need to be ahead of the filter execTime > 500
. Else the filter will consider the label execTime
to be not defined.