ELK / OpenSearch and Kibana: KQL
ELK is Elasticsearch for storage and search, Logstash or Beats for collection and Kibana for the interface, and queries in Discover are written in KQL. A query always has a time range. Kibana shows time in the browser's zone while @timestamp is stored in UTC, so empty results often mean a wrong zone or index. Searching by trace id is the most reliable.
Logs · Kibana
A bug was reproduced at 09:14, and Kibana shows "nothing" for that time. Where did the logs go?
A tester reproduced a payment failure at 09:14, opened Kibana, set an absolute range of 09:10-09:20 and saw zero entries. The logs did not vanish: Kibana shows time in the browser’s zone, the reproduction happened in UTC, and on top of that the wrong index was selected. Such small things eat the first hour of an investigation. This lesson covers ELK/OpenSearch, the KQL query language and a short QA workflow in Discover.
How it works and why. ELK (Elastic Stack) is Elasticsearch (storage and search), Logstash or the lighter Beats and Elastic Agent (collection and processing) and Kibana (the interface). OpenSearch is a fork with its own Kibana counterpart, OpenSearch Dashboards. Logs are indexed by time (daily indices or data streams), so a query always has a time range: without it the search would go through the entire store. Hence two switches people forget: the time range and the data source (in classic Discover mode a data view that says which indices to read).
In Kibana Discover you pick a time range and, in classic mode, a data view (ES|QL mode needs no data view). In classic mode you write a query in KQL (Kibana Query Language) or Lucene. KQL examples:
log.level: "error" and service.name: "checkout"
http.response.status_code >= 500
event.action: "login" and not user.id: "svc-monitor"
error.message: *
trace.id: ("4bf92f35..." or "9a1c0d77...")
The rules: field: value; operators and, or, not; parentheses group; >, >= for numbers and dates; * as a wildcard, and field: * means "the field exists"; values with spaces go in quotes. Field names depend on the log schema: in Elastic Common Schema they are log.level, service.name, trace.id, while a "home-grown" format may have level, app. Autocomplete hints in Discover show the real fields.
Example: from "nothing there" to the root cause. A workflow in which each step narrows the result:
# 1. a window around the reproduction: 09:10-09:20 (in Kibana's zone!)
# 2. narrow by service and level
service.name: "checkout" and log.level: "error"
# 3. filter out healthcheck noise
service.name: "checkout" and log.level: "error"
and not message: "healthcheck"
# 4. or go straight by the request id from the response
trace.id: "4bf92f3577b34da6a3ce929d0e0e4736"
Step 1 is most often forgotten: Kibana shows time according to the zone setting (by default the browser’s zone), while in the documents themselves @timestamp is stored in UTC. So "09:14" from a test and "09:14" in Kibana can be different moments. Fix the zone in the test report and check Kibana’s setting. Then columns (@timestamp, service.name, message) are added to the table, sorted by time, and a document is opened to read the stack in the error.stack_trace field. If the search will be needed again, save it, and put the error count over time into a visualisation.
ES|QL. Besides KQL, Kibana has ES|QL, Elastic’s piped query language where steps are joined with the | character. It is convenient for counts and grouping:
FROM logs-*
| WHERE log.level == "error" AND service.name == "checkout"
| STATS errors = COUNT(*) BY error.code
| SORT errors DESC
| LIMIT 5
Two ways to ask the same thing
Find entries
A field filter in Discover: "show ERRORs of checkout". Fast, for finding a specific entry and reading the stack.
Count and group
A pipeline: filter, STATS, sort. For questions like "which error codes are the most frequent".
Limits and alternatives. OpenSearch Dashboards has the similar DQL, and also PPL and SQL, so syntax differs in details. A search depends on how a field is indexed: a text field is split into words and searched by them, while an exact match requires the keyword type (in nonstandard schemas often message.keyword). Recent entries may appear with a delay of a few seconds because they are still being indexed, so an "empty result" a moment after a failure does not prove the logs are missing. For investigating through a command rather than the interface, see the lesson on the command line.
Typical mistakes.
- A forgotten time range or zone: "empty" although the logs exist.
- The wrong data view or index: the query is correct but searches in the wrong place.
- An exact search on a text field: words match partially and the result is unexpected.
- A filter only by level ERROR without a trace id: thousands of other people’s errors on a shared stand.
Check yourself
A test failed at 09:14 UTC and Kibana is set to a UTC+3 zone. How do you find the right window?
Answer
In Kibana this moment appears as 12:14. So either choose an absolute range 12:10-12:20 or change the zone in settings to UTC. The most reliable is to search by the trace id or request id from the test report, then time becomes secondary.
When is ES|QL more convenient than KQL?
Answer
When you need not to find entries but to count or group: for example the five most frequent error codes of a service per hour. A pipeline with STATS ... BY gives a table at once, while KQL only filters documents.