Skip to content

Commit 8948ce1

Browse files
authored
clarify logcli commands and output (grafana#1712)
* clarify logcli commands and output This commit attempts to do two things: 1. Differentiate between log entries and raw log lines, where the latter is what is produced by `-oraw`. 2. Explain the difference between `logcli query` and `logcli instant-query`. Fixes grafana#1676. * address feedback * address more feedback * explain that instant-query is not useful for log lines * address review feedback This commit leaves logcli.md unmodified; that will be done in a later commit if the changes are approved. * sync logcli.md with command output
1 parent ba51aad commit 8948ce1

File tree

3 files changed

+190
-51
lines changed

3 files changed

+190
-51
lines changed

cmd/logcli/main.go

+43-7
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,55 @@ import (
2020

2121
var (
2222
app = kingpin.New("logcli", "A command-line for loki.").Version(version.Print("logcli"))
23-
quiet = app.Flag("quiet", "suppress everything but log lines").Default("false").Short('q').Bool()
23+
quiet = app.Flag("quiet", "suppress query metadata").Default("false").Short('q').Bool()
2424
statistics = app.Flag("stats", "show query statistics").Default("false").Bool()
25-
outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]").Default("default").Short('o').Enum("default", "raw", "jsonl")
25+
outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp.").Default("default").Short('o').Enum("default", "raw", "jsonl")
2626
timezone = app.Flag("timezone", "Specify the timezone to use when formatting output timestamps [Local, UTC]").Default("Local").Short('z').Enum("Local", "UTC")
2727

2828
queryClient = newQueryClient(app)
2929

30-
queryCmd = app.Command("query", "Run a LogQL query.")
30+
queryCmd = app.Command("query", `Run a LogQL query.
31+
32+
The "query" command is useful for querying for logs. Logs can be
33+
returned in a few output modes:
34+
35+
raw: log line
36+
default: log timestamp + log labels + log line
37+
jsonl: JSON response from Loki API of log line
38+
39+
The output of the log can be specified with the "-o" flag, for
40+
example, "-o raw" for the raw output format.
41+
42+
The "query" command will output extra information about the query
43+
and its results, such as the API URL, set of common labels, and set
44+
of excluded labels. This extra information can be suppressed with the
45+
--quiet flag.
46+
47+
While "query" does support metrics queries, its output contains multiple
48+
data points between the start and end query time. This output is used to
49+
build graphs, like what is seen in the Grafana Explore graph view. If
50+
you are querying metrics and just want the most recent data point
51+
(like what is seen in the Grafana Explore table view), then you should use
52+
the "instant-query" command instead.`)
3153
rangeQuery = newQuery(false, queryCmd)
3254
tail = queryCmd.Flag("tail", "Tail the logs").Short('t').Default("false").Bool()
3355
delayFor = queryCmd.Flag("delay-for", "Delay in tailing by number of seconds to accumulate logs for re-ordering").Default("0").Int()
3456

35-
instantQueryCmd = app.Command("instant-query", "Run an instant LogQL query")
36-
instantQuery = newQuery(true, instantQueryCmd)
57+
instantQueryCmd = app.Command("instant-query", `Run an instant LogQL query.
58+
59+
The "instant-query" command is useful for evaluating a metric query for
60+
a single point in time. This is equivalent to the Grafana Explore table
61+
view; if you want a metrics query that is used to build a Grafana graph,
62+
you should use the "query" command instead.
63+
64+
This command does not produce useful output when querying for log lines;
65+
you should always use the "query" command when you are running log queries.
66+
67+
For more information about log queries and metric queries, refer to the
68+
LogQL documentation:
69+
70+
https://github.com/grafana/loki/blob/master/docs/logql.md`)
71+
instantQuery = newQuery(true, instantQueryCmd)
3772

3873
labelsCmd = app.Command("labels", "Find values for a given label.")
3974
labelName = labelsCmd.Arg("label", "The name of the label.").HintAction(hintActionLabelNames).String()
@@ -118,7 +153,7 @@ func newQueryClient(app *kingpin.Application) *client.Client {
118153
app.Flag("tls-skip-verify", "Server certificate TLS skip verify.").Default("false").BoolVar(&client.TLSConfig.InsecureSkipVerify)
119154
app.Flag("cert", "Path to the client certificate. Can also be set using LOKI_CLIENT_CERT_PATH env var.").Default("").Envar("LOKI_CLIENT_CERT_PATH").StringVar(&client.TLSConfig.CertFile)
120155
app.Flag("key", "Path to the client certificate key. Can also be set using LOKI_CLIENT_KEY_PATH env var.").Default("").Envar("LOKI_CLIENT_KEY_PATH").StringVar(&client.TLSConfig.KeyFile)
121-
app.Flag("org-id", "org ID header to be substituted for auth").StringVar(&client.OrgID)
156+
app.Flag("org-id", "adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway.").StringVar(&client.OrgID)
122157

123158
return client
124159
}
@@ -153,11 +188,12 @@ func newQuery(instant bool, cmd *kingpin.CmdClause) *query.Query {
153188
return nil
154189
})
155190

156-
cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&query.QueryString)
157191
cmd.Flag("limit", "Limit on number of entries to print.").Default("30").IntVar(&query.Limit)
158192
if instant {
193+
cmd.Arg("query", "eg 'rate({foo=\"bar\"} |~ \".*error.*\" [5m])'").Required().StringVar(&query.QueryString)
159194
cmd.Flag("now", "Time at which to execute the instant query.").StringVar(&now)
160195
} else {
196+
cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&query.QueryString)
161197
cmd.Flag("since", "Lookback window.").Default("1h").DurationVar(&since)
162198
cmd.Flag("from", "Start looking for logs at this absolute time (inclusive)").StringVar(&from)
163199
cmd.Flag("to", "Stop looking for logs at this absolute time (exclusive)").StringVar(&to)

docs/getting-started/logcli.md

+118-29
Original file line numberDiff line numberDiff line change
@@ -64,69 +64,158 @@ Configuration values are considered in the following order (lowest to highest):
6464

6565
### Details
6666

67-
```bash
67+
```nohighlight
6868
$ logcli help
6969
usage: logcli [<flags>] <command> [<args> ...]
7070
7171
A command-line for loki.
7272
7373
Flags:
74-
--help Show context-sensitive help (also try --help-long and --help-man).
75-
-q, --quiet suppress everything but log lines
76-
-o, --output=default specify output mode [default, raw, jsonl]
77-
--addr="https://logs-us-west1.grafana.net"
78-
Server address.
79-
--username="" Username for HTTP basic auth.
80-
--password="" Password for HTTP basic auth.
81-
--ca-cert="" Path to the server Certificate Authority.
74+
--help Show context-sensitive help (also try --help-long and
75+
--help-man).
76+
--version Show application version.
77+
-q, --quiet suppress query metadata
78+
--stats show query statistics
79+
-o, --output=default specify output mode [default, raw, jsonl]. raw
80+
suppresses log labels and timestamp.
81+
-z, --timezone=Local Specify the timezone to use when formatting output
82+
timestamps [Local, UTC]
83+
--addr="http://localhost:3100"
84+
Server address. Can also be set using LOKI_ADDR env
85+
var.
86+
--username="" Username for HTTP basic auth. Can also be set using
87+
LOKI_USERNAME env var.
88+
--password="" Password for HTTP basic auth. Can also be set using
89+
LOKI_PASSWORD env var.
90+
--ca-cert="" Path to the server Certificate Authority. Can also be
91+
set using LOKI_CA_CERT_PATH env var.
8292
--tls-skip-verify Server certificate TLS skip verify.
83-
--cert="" Path to the client certificate.
84-
--key="" Path to the client certificate key.
85-
--org-id=ORG-ID org ID header to be substituted for auth
93+
--cert="" Path to the client certificate. Can also be set using
94+
LOKI_CLIENT_CERT_PATH env var.
95+
--key="" Path to the client certificate key. Can also be set
96+
using LOKI_CLIENT_KEY_PATH env var.
97+
--org-id=ORG-ID adds X-Scope-OrgID to API requests for representing
98+
tenant ID. Useful for requesting tenant data when
99+
bypassing an auth gateway.
86100
87101
Commands:
88102
help [<command>...]
89103
Show help.
90104
91-
query [<flags>] <query> [<regex>]
105+
query [<flags>] <query>
92106
Run a LogQL query.
93107
108+
The "query" command is useful for querying for logs. Logs can be returned in
109+
a few output modes:
110+
111+
raw: log line
112+
default: log timestamp + log labels + log line
113+
jsonl: JSON response from Loki API of log line
114+
115+
The output of the log can be specified with the "-o" flag, for example, "-o
116+
raw" for the raw output format.
117+
118+
The "query" command will output extra information about the query and its
119+
results, such as the API URL, set of common labels, and set of excluded
120+
labels. This extra information can be suppressed with the --quiet flag.
121+
122+
While "query" does support metrics queries, its output contains multiple
123+
data points between the start and end query time. This output is used to
124+
build graphs, like what is seen in the Grafana Explore graph view. If you
125+
are querying metrics and just want the most recent data point (like what is
126+
seen in the Grafana Explore table view), then you should use the
127+
"instant-query" command instead.
128+
129+
instant-query [<flags>] <query>
130+
Run an instant LogQL query.
131+
132+
The "instant-query" command is useful for evaluating a metric query for a
133+
single point in time. This is equivalent to the Grafana Explore table view;
134+
if you want a metrics query that is used to build a Grafana graph, you
135+
should use the "query" command instead.
136+
137+
This command does not produce useful output when querying for log lines; you
138+
should always use the "query" command when you are running log queries.
139+
140+
For more information about log queries and metric queries, refer to the
141+
LogQL documentation:
142+
143+
https://github.com/grafana/loki/blob/master/docs/logql.md
144+
94145
labels [<label>]
95146
Find values for a given label.
96147
97148
$ logcli help query
98-
usage: logcli query [<flags>] <query> [<regex>]
149+
usage: logcli query [<flags>] <query>
99150
100151
Run a LogQL query.
101152
153+
The "query" command is useful for querying for logs. Logs can be returned in a
154+
few output modes:
155+
156+
raw: log line
157+
default: log timestamp + log labels + log line
158+
jsonl: JSON response from Loki API of log line
159+
160+
The output of the log can be specified with the "-o" flag, for example, "-o raw"
161+
for the raw output format.
162+
163+
The "query" command will output extra information about the query and its
164+
results, such as the API URL, set of common labels, and set of excluded labels.
165+
This extra information can be suppressed with the --quiet flag.
166+
167+
While "query" does support metrics queries, its output contains multiple data
168+
points between the start and end query time. This output is used to build
169+
graphs, like what is seen in the Grafana Explore graph view. If you are querying
170+
metrics and just want the most recent data point (like what is seen in the
171+
Grafana Explore table view), then you should use the "instant-query" command
172+
instead.
173+
102174
Flags:
103-
--help Show context-sensitive help (also try --help-long and --help-man).
104-
-q, --quiet suppress everything but log lines
105-
-o, --output=default specify output mode [default, raw, jsonl]
106-
--addr="https://logs-us-west1.grafana.net"
107-
Server address.
108-
--username="" Username for HTTP basic auth.
109-
--password="" Password for HTTP basic auth.
110-
--ca-cert="" Path to the server Certificate Authority.
175+
--help Show context-sensitive help (also try --help-long and
176+
--help-man).
177+
--version Show application version.
178+
-q, --quiet suppress query metadata
179+
--stats show query statistics
180+
-o, --output=default specify output mode [default, raw, jsonl]. raw
181+
suppresses log labels and timestamp.
182+
-z, --timezone=Local Specify the timezone to use when formatting output
183+
timestamps [Local, UTC]
184+
--addr="http://localhost:3100"
185+
Server address. Can also be set using LOKI_ADDR env
186+
var.
187+
--username="" Username for HTTP basic auth. Can also be set using
188+
LOKI_USERNAME env var.
189+
--password="" Password for HTTP basic auth. Can also be set using
190+
LOKI_PASSWORD env var.
191+
--ca-cert="" Path to the server Certificate Authority. Can also be
192+
set using LOKI_CA_CERT_PATH env var.
111193
--tls-skip-verify Server certificate TLS skip verify.
112-
--cert="" Path to the client certificate.
113-
--key="" Path to the client certificate key.
194+
--cert="" Path to the client certificate. Can also be set using
195+
LOKI_CLIENT_CERT_PATH env var.
196+
--key="" Path to the client certificate key. Can also be set
197+
using LOKI_CLIENT_KEY_PATH env var.
198+
--org-id=ORG-ID adds X-Scope-OrgID to API requests for representing
199+
tenant ID. Useful for requesting tenant data when
200+
bypassing an auth gateway.
114201
--limit=30 Limit on number of entries to print.
115202
--since=1h Lookback window.
116-
--from=FROM Start looking for logs at this absolute time (inclusive)
203+
--from=FROM Start looking for logs at this absolute time
204+
(inclusive)
117205
--to=TO Stop looking for logs at this absolute time (exclusive)
206+
--step=STEP Query resolution step width
118207
--forward Scan forwards through logs.
119208
--local-config="" Execute the current query using a configured storage from a given Loki configuration file.
120-
-t, --tail Tail the logs
121-
--delay-for=0 Delay in tailing by number of seconds to accumulate logs for re-ordering
122209
--no-labels Do not print any labels
123210
--exclude-label=EXCLUDE-LABEL ...
124211
Exclude labels given the provided key during output.
125212
--include-label=INCLUDE-LABEL ...
126213
Include labels given the provided key during output.
127214
--labels-length=0 Set a fixed padding to labels
215+
-t, --tail Tail the logs
216+
--delay-for=0 Delay in tailing by number of seconds to accumulate
217+
logs for re-ordering
128218
129219
Args:
130-
<query> eg '{foo="bar",baz="blip"}'
131-
[<regex>]
220+
<query> eg '{foo="bar",baz=~".*blip"} |~ ".*error.*"'
132221
```

docs/logql.md

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
# LogQL: Log Query Language
22

3-
Loki comes with its very own language for querying logs called *LogQL*. LogQL
4-
can be considered a distributed `grep` with labels for filtering.
3+
Loki comes with its own PromQL-inspired language for queries called *LogQL*.
4+
LogQL can be considered a distributed `grep` that aggregates log sources and
5+
using labels and operators for filtering.
56

6-
A basic LogQL query consists of two parts: the **log stream selector** and a
7+
There are two types of LogQL queries: *log queries* which return the contents of
8+
log lines, and *metric queries* which extend log queries and calculates values
9+
based on the counts of logs from a log query.
10+
11+
A basic log query consists of two parts: the **log stream selector** and a
712
**filter expression**. Due to Loki's design, all LogQL queries are required to
813
contain a log stream selector.
914

10-
The log stream selector will reduce the number of log streams to a manageable
11-
volume. Depending how many labels you use to filter down the log streams will
12-
affect the relative performance of the query's execution. The filter expression
13-
is then used to do a distributed `grep` over the retrieved log streams.
15+
The log stream selector determines how many log streams (unique sources of log
16+
content, such as files) will be searched. A more granular log stream selector
17+
reduces the number of searched streams to a manageable volume. This means that
18+
the labels passed to the log stream selector will affect the relative
19+
performance of the query's execution. The filter expression is then used to do a
20+
distributed `grep` over the aggregated logs from the matching log streams.
1421

1522
### Log Stream Selector
1623

1724
The log stream selector determines which log streams should be included in your
18-
query. The stream selector is comprised of one or more key-value pairs, where
19-
each key is a **log label** and the value is that label's value.
25+
query results. The stream selector is comprised of one or more key-value pairs,
26+
where each key is a **log label** and the value is that label's value.
2027

21-
The log stream selector is written by wrapping the key-value pairs in a
22-
pair of curly braces:
28+
The log stream selector is written by wrapping the key-value pairs in a pair of
29+
curly braces:
2330

2431
```
2532
{app="mysql",name="mysql-backup"}
2633
```
2734

2835
In this example, log streams that have a label of `app` whose value is `mysql`
2936
_and_ a label of `name` whose value is `mysql-backup` will be included in the
30-
query results.
37+
query results. Note that this will match any log stream whose labels _at least_
38+
contain `mysql-backup` for their name label; if there are multiple streams that
39+
contain that label, logs from all of the matching streams will be shown in the
40+
results.
3141

3242
The `=` operator after the label name is a **label matching operator**. The
3343
following label matching operators are supported:
@@ -74,10 +84,14 @@ When using `|~` and `!~`,
7484
matching is case-sensitive by default and can be switched to case-insensitive
7585
prefixing the regex with `(?i)`.
7686

77-
## Counting logs
87+
## Metric Queries
88+
89+
LogQL also supports wrapping a log query with functions that allows for counting
90+
entries per stream.
7891

79-
LogQL also supports functions that wrap a query and allow for counting entries
80-
per stream.
92+
Metric queries can be used to calculate things such as the rate of error
93+
messages, or the top N log sources with the most amount of logs over the last 3
94+
hours.
8195

8296
### Range Vector aggregation
8397

0 commit comments

Comments
 (0)