Ravindra BagaleCourses & study guides मराठी

Chapter 95: Logging with Loki, Elastic Stack and CloudWatch

DevOps

95.12 Logs Insights QL — fields, filter, stats and time scope

Story (fictional teaching scenario). At a fictional Chennai shop, Yuki from Osaka first queries a short incident window rather than scanning every group for a month.

Logs Insights QL — fields, filter, stats and time scope
Logs Insights QL — fields, filter, stats and time scope

Why

The same question can cost more and take longer when the scope is unnecessarily broad.

What

  • Logs Insights QL is a CloudWatch query language; this lesson uses QL, not SQL or another supported interface.
  • fields selects fields, filter keeps matching events, and stats aggregates.
  • Aggregate means summarize many records into counts or other values.
  • @timestamp is a service-provided discovered field; custom JSON fields may also be discovered.
  • A query scope includes log groups and time range, not just query text.

How

  1. Select only the necessary groups and the narrowest useful time window.

  2. Choose fields before investigating individual records.

  3. Filter exact categories or numeric conditions.

  4. Aggregate when you need a total; do not confuse result limits with a guaranteed scan-cost cap.

Example 1: Read selected error events

  1. Read the command or request.
fields @timestamp, service, level, request_id, latency_ms
| filter level = "ERROR"
| sort @timestamp asc
| limit 20
  1. Read the result.

Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)

Illustrative fixture rows: req-demo-003 and req-demo-004.
  1. Line by line: fields lists output columns. The filter selects our synthetic severity. sort orders by the service timestamp. limit 20 caps returned rows; it is not a universal promise of scanning only 20 events. No query, paid scan or AWS call was made.

Example 2: Aggregate counts

  1. Read the command or request.
filter level = "ERROR"
| stats count(*) as errors by service
  1. Read the result.

Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)

service=bakery-api, errors=2
  1. Line by line: The filter keeps error records first. count(*) counts those records. as errors names the result column. by service groups the count. This output assumes only the six fixture events in the chosen scope, not a real CloudWatch result.

Ravindra Bagale's Tip

Query scope is a cost and correctness decision.

Did you understand? Let us try it once more.