2023-07-14 10:12:23 +00:00
---
2023-10-05 08:40:55 +00:00
sort: 4
2023-07-14 10:12:23 +00:00
title: Querying
2023-10-05 08:40:55 +00:00
weight: 4
2023-07-14 20:21:58 +00:00
menu:
docs:
identifier: victorialogs-querying
parent: "victorialogs"
2023-10-05 08:40:55 +00:00
weight: 4
2023-10-14 11:45:57 +00:00
aliases:
- /VictoriaLogs/querying/
- /VictoriaLogs/querying/index.html
2023-07-14 10:12:23 +00:00
---
2023-06-21 05:08:19 +00:00
# Querying
2023-10-16 11:39:23 +00:00
[VictoriaLogs ](https://docs.victoriametrics.com/VictoriaLogs/ ) can be queried with [LogsQL ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html )
2023-06-22 00:28:28 +00:00
via the following ways:
2023-06-22 00:54:42 +00:00
- [Web UI ](#web-ui ) - a web-based UI for querying logs
2023-06-22 00:28:28 +00:00
- [HTTP API ](#http-api )
- [Command-line interface ](#command-line )
## HTTP API
2023-06-22 01:31:50 +00:00
VictoriaLogs can be queried at the `/select/logsql/query` HTTP endpoint.
2023-10-16 11:39:23 +00:00
The [LogsQL ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html ) query must be passed via `query` argument.
2023-06-21 05:08:19 +00:00
For example, the following query returns all the log entries with the `error` word:
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error'
```
2023-10-16 11:39:23 +00:00
The response by default contains [`_msg` ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#message-field ),
[`_stream` ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#stream-fields ) and
[`_time` ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#time-field ) fields plus the explicitly mentioned fields.
See [these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#querying-specific-fields ) for details.
2023-07-10 22:00:10 +00:00
2023-06-21 05:08:19 +00:00
The `query` argument can be passed either in the request url itself (aka HTTP GET request) or via request body
with the `x-www-form-urlencoded` encoding (aka HTTP POST request). The HTTP POST is useful for sending long queries
when they do not fit the maximum url length of the used clients and proxies.
2023-10-16 11:39:23 +00:00
See [LogsQL docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html ) for details on what can be passed to the `query` arg.
2023-06-21 05:08:19 +00:00
The `query` arg must be properly encoded with [percent encoding ](https://en.wikipedia.org/wiki/URL_encoding ) when passing it to `curl`
or similar tools.
The `/select/logsql/query` endpoint returns [a stream of JSON lines ](http://ndjson.org/ ),
where each line contains JSON-encoded log entry in the form `{field1="value1",...,fieldN="valueN"}` .
Example response:
```
{"_msg":"error: disconnect from 19.54.37.22: Auth fail [preauth]","_stream":"{}","_time":"2023-01-01T13:32:13Z"}
{"_msg":"some other error","_stream":"{}","_time":"2023-01-01T13:32:15Z"}
```
The matching lines are sent to the response stream as soon as they are found in VictoriaLogs storage.
This means that the returned response may contain billions of lines for queries matching too many log entries.
The response can be interrupted at any time by closing the connection to VictoriaLogs server.
This allows post-processing the returned lines at the client side with the usual Unix commands such as `grep` , `jq` , `less` , `head` , etc.
See [these docs ](#command-line ) for more details.
2023-10-16 11:39:23 +00:00
The returned lines are sorted by [`_time` field ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#time-field )
2023-06-21 08:10:45 +00:00
if their total size doesn't exceed `-select.maxSortBufferSize` command-line flag value (by default it is set to one megabyte).
Otherwise the returned lines aren't sorted, since sorting disables the ability to send matching log entries to response stream as soon as they are found.
2023-10-16 11:39:23 +00:00
Query results can be sorted either at VictoriaLogs side according [to these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#sorting )
2023-06-21 05:08:19 +00:00
or at client side with the usual `sort` command according to [these docs ](#command-line ).
2023-10-16 11:39:23 +00:00
By default the `(AccountID=0, ProjectID=0)` [tenant ](https://docs.victoriametrics.com/VictoriaLogs/#multitenancy ) is queried.
2023-06-21 05:08:19 +00:00
If you need querying other tenant, then specify the needed tenant via http request headers. For example, the following query searches
for log messages at `(AccountID=12, ProjectID=34)` tenant:
```bash
curl http://localhost:9428/select/logsql/query -H 'AccountID: 12' -H 'ProjectID: 34' -d 'query=error'
```
2023-10-16 11:39:23 +00:00
The number of requests to `/select/logsql/query` can be [monitored ](https://docs.victoriametrics.com/VictoriaLogs/#monitoring )
2023-06-21 05:08:19 +00:00
with `vl_http_requests_total{path="/select/logsql/query"}` metric.
2023-06-22 00:54:42 +00:00
## Web UI
2023-06-21 17:37:36 +00:00
2023-10-16 11:39:23 +00:00
VictoriaLogs provides a simple Web UI for logs [querying ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html ) and exploration
2023-06-22 02:52:48 +00:00
at `http://localhost:9428/select/vmui` . The UI allows exploring query results:
2023-06-21 17:37:36 +00:00
< img src = "vmui.png" width = "800" / >
There are three modes of displaying query results:
- `Group` - results are displayed as a table with rows grouped by stream and fields for filtering.
- `Table` - displays query results as a table.
2023-06-22 00:28:28 +00:00
- `JSON` - displays raw JSON response from [HTTP API ](#http-api ).
2023-06-21 17:37:36 +00:00
This is the first version that has minimal functionality. It comes with the following limitations:
2023-06-22 00:28:28 +00:00
- The number of query results is always limited to 1000 lines. Iteratively add
2023-10-16 11:39:23 +00:00
more specific [filters ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#filters ) to the query
2023-06-22 00:28:28 +00:00
in order to get full response with less than 1000 lines.
2023-10-16 11:39:23 +00:00
- Queries are always executed against [tenant ](https://docs.victoriametrics.com/VictoriaLogs/#multitenancy ) `0` .
2023-06-21 17:37:36 +00:00
These limitations will be removed in future versions.
To get around the current limitations, you can use an alternative - the [command line interface ](#command-line ).
2023-06-21 05:08:19 +00:00
## Command-line
VictoriaLogs integrates well with `curl` and other command-line tools during querying because of the following features:
- VictoriaLogs sends the matching log entries to the response stream as soon as they are found.
This allows forwarding the response stream to arbitrary [Unix pipes ](https://en.wikipedia.org/wiki/Pipeline_(Unix )).
- VictoriaLogs automatically adjusts query execution speed to the speed of the client, which reads the response stream.
For example, if the response stream is piped to `less` command, then the query is suspended
until the `less` command reads the next block from the response stream.
- VictoriaLogs automatically cancels query execution when the client closes the response stream.
For example, if the query response is piped to `head` command, then VictoriaLogs stops executing the query
when the `head` command closes the response stream.
These features allow executing queries at command-line interface, which potentially select billions of rows,
without the risk of high resource usage (CPU, RAM, disk IO) at VictoriaLogs server.
For example, the following query can return very big number of matching log entries (e.g. billions) if VictoriaLogs contains
2023-10-16 11:39:23 +00:00
many log messages with the `error` [word ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#word ):
2023-06-21 05:08:19 +00:00
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error'
```
If the command returns "never-ending" response, then just press `ctrl+C` at any time in order to cancel the query.
VictoriaLogs notices that the response stream is closed, so it cancels the query and instantly stops consuming CPU, RAM and disk IO for this query.
Then just use `head` command for investigating the returned log messages and narrowing down the query:
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error' | head -10
```
The `head -10` command reads only the first 10 log messages from the response and then closes the response stream.
This automatically cancels the query at VictoriaLogs side, so it stops consuming CPU, RAM and disk IO resources.
Sometimes it may be more convenient to use `less` command instead of `head` during the investigation of the returned response:
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error' | less
```
The `less` command reads the response stream on demand, when the user scrolls down the output.
VictoriaLogs suspends query execution when `less` stops reading the response stream.
It doesn't consume CPU and disk IO resources during this time. It resumes query execution
when the `less` continues reading the response stream.
Suppose that the initial investigation of the returned query results helped determining that the needed log messages contain
2023-10-16 11:39:23 +00:00
`cannot open file` [phrase ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#phrase-filter ).
2023-06-21 05:08:19 +00:00
Then the query can be narrowed down to `error AND "cannot open file"`
2023-10-16 11:39:23 +00:00
(see [these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#logical-filter ) about `AND` operator).
2023-06-21 05:08:19 +00:00
Then run the updated command in order to continue the investigation:
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error AND "cannot open file"' | head
```
Note that the `query` arg must be properly encoded with [percent encoding ](https://en.wikipedia.org/wiki/URL_encoding ) when passing it to `curl`
or similar tools.
The `pipe the query to "head" or "less" -> investigate the results -> refine the query` iteration
can be repeated multiple times until the needed log messages are found.
The returned VictoriaLogs query response can be post-processed with any combination of Unix commands,
which are usually used for log analysis - `grep` , `jq` , `awk` , `sort` , `uniq` , `wc` , etc.
For example, the following command uses `wc -l` Unix command for counting the number of log messages
2023-10-16 11:39:23 +00:00
with the `error` [word ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#word )
received from [streams ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#stream-fields ) with `app="nginx"` field
2023-06-21 05:08:19 +00:00
during the last 5 minutes:
```bash
2023-07-18 01:17:43 +00:00
curl http://localhost:9428/select/logsql/query -d 'query=_stream:{app="nginx"} AND _time:5m AND error' | wc -l
2023-06-21 05:08:19 +00:00
```
2023-10-16 11:39:23 +00:00
See [these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#stream-filter ) about `_stream` filter,
[these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#time-filter ) about `_time` filter
and [these docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#logical-filter ) about `AND` operator.
2023-06-21 05:08:19 +00:00
2023-10-16 11:39:23 +00:00
The following example shows how to sort query results by the [`_time` field ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#time-field ):
2023-06-21 05:08:19 +00:00
```bash
curl http://localhost:9428/select/logsql/query -d 'query=error' | jq -r '._time + " " + ._msg' | sort | less
```
2023-10-16 11:39:23 +00:00
This command uses `jq` for extracting [`_time` ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#time-field )
and [`_msg` ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#message-field ) fields from the returned results,
2023-06-21 05:08:19 +00:00
and piping them to `sort` command.
Note that the `sort` command needs to read all the response stream before returning the sorted results. So the command above
can take non-trivial amounts of time if the `query` returns too many results. The solution is to narrow down the `query`
2023-10-16 11:39:23 +00:00
before sorting the results. See [these tips ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#performance-tips )
2023-06-21 05:08:19 +00:00
on how to narrow down query results.
The following example calculates stats on the number of log messages received during the last 5 minutes
2023-10-16 11:39:23 +00:00
grouped by `log.level` [field ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html#data-model ):
2023-06-21 05:08:19 +00:00
```bash
2023-07-18 01:17:43 +00:00
curl http://localhost:9428/select/logsql/query -d 'query=_time:5m log.level:*' | jq -r '."log.level"' | sort | uniq -c
2023-06-21 05:08:19 +00:00
```
2023-10-16 11:39:23 +00:00
The query selects all the log messages with non-empty `log.level` field via ["any value" filter ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html#any-value-filter ),
2023-06-21 05:08:19 +00:00
then pipes them to `jq` command, which extracts the `log.level` field value from the returned JSON stream, then the extracted `log.level` values
are sorted with `sort` command and, finally, they are passed to `uniq -c` command for calculating the needed stats.
See also:
2023-10-16 11:39:23 +00:00
- [Key concepts ](https://docs.victoriametrics.com/VictoriaLogs/keyConcepts.html ).
- [LogsQL docs ](https://docs.victoriametrics.com/VictoriaLogs/LogsQL.html ).