layout: post title: "LogQL 学习笔记" date: 2021-11-22 00:37:39 +0800
All LogQL queries contain a log stream selector. Optionally, the log stream selector can be followed by a log pipeline.
example:
{container="query-frontend",namespace="loki-dev"} |= "metrics.go" | logfmt | duration > 10s and throughput_mb < 500
label matching operator:
{app="mysql",name="mysql-backup"}
Log pipeline expressions fall into one of three categories:
{job="mysql"} |= "error" != "timeout"
For instance, the pipeline | json will produce the following mapping:
{ "a.b": {c: "d"}, e: "f" }
->
{a_b_c="d", e="f"}
For example the following log line:
at=info method=GET path=/ host=grafana.net fwd="124.133.124.161" service=8ms status=200
will get those labels extracted:
"at" => "info"
"method" => "GET"
"path" => "/"
"host" => "grafana.net"
"fwd" => "124.133.124.161"
"service" => "8ms"
"status" => "200"
Consider this NGINX log line.
0.191.12.2 - - [10/Jun/2021:09:14:29 +0000] "GET /api/plugins/versioncheck HTTP/1.1" 200 2 "-" "Go-http-client/2.0" "13.76.247.102, 34.120.177.193" "TLSv1.2" "US" ""
This log line can be parsed with the expression
\<ip> - - \<> "\<method> \<uri> \<>" \<status> \<size> \<> "\<agent>" \<>
to extract these fields:
"ip" => "0.191.12.2"
"method" => "GET"
"uri" => "/api/plugins/versioncheck"
"status" => "200"
"size" => "2"
"agent" => "Go-http-client/2.0"
For example the parser | regexp "(?P<method>\\w+) (?P<path>[\\w|/]+) \\((?P<status>\\d+?)\\) (?P<duration>.*)"
will extract from the following line:
POST /api/prom/api/v1/query_range (200) 1.5s
those labels:
"method" => "POST"
"path" => "/api/prom/api/v1/query_range"
"status" => "200"
"duration" => "1.5s"
A predicate contains a label identifier, an operation and a value to compare the label with.
cluster="namespace"
Support multiple value types which are automatically inferred from the query input.
us-central1
.Using Duration, Number and Bytes will convert the label value prior to comparision and support the following comparators:
logfmt | duration > 1m and bytes_consumed > 20MB
The line format expression can rewrite the log line content by using the text/template format. It takes a single string parameter | line_format "{ {.label_name} }"
, which is the template format. All labels are injected variables into the template and are available to use with the {{.label_name}} notation.
example:
If we have the following labels ip=1.1.1.1
, status=200
and duration=3000(ms)
, we can divide the duration by 1000 to get the value in seconds.
{container="frontend"} | logfmt | line_format "{ {.ip} } { {.status} } { {div .duration 1000} }"
The | label_format
expression can rename, modify or add labels. It takes as parameter a comma separated list of equality operations, enabling multiple operations at once.
Note:
The renaming form dst=src will drop the src label after remapping it to the dst label. However, the template form will preserve the referenced labels, such that dst="{{.src}}" results in both dst and src having the same value.
{cluster="ops-tools1", namespace="loki-dev", job="loki-dev/query-frontend"} |= "metrics.go" !="out of order" | logfmt | duration > 30s or status_code!="200"
level=debug ts=2020-10-02T10:10:42.092268913Z caller=logging.go:66 traceID=a9d4d8a928d8db1 msg="POST /api/prom/api/v1/query_range (200) 1.5s"
{job="cortex-ops/query-frontend"} | logfmt | line_format "{ {.msg} }" | regexp "(?P<method>\\w+) (?P<path>[\\w|/]+) \\((?P<status>\\d+?)\\) (?P<duration>.*)"