Try using Grot AI Grot AI for this query ->
Promo banner icon

What’s new: Grafana 13 release, the latest in AI, OSS project updates, and more from GrafanaCON2026

Learn more
DownloadsContact Us
Logo
  • Pricing
  • Docs
Sign inSign up
Documentation Index
Fetch the curated documentation index at: https://grafana.com/llms.txt

Fetch the complete documentation index at: https://grafana.com/llms-full.txt
Use this file to discover all available pages before exploring further.

STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: https://grafana.com/docs/loki/latest/query/metric_queries.md (append .md) or send Accept: text/markdown to https://grafana.com/docs/loki/latest/query/metric_queries/. For the curated documentation index, use https://grafana.com/llms.txt. For the complete documentation index, use https://grafana.com/llms-full.txt.

Menu
Documentationbreadcrumb arrow Grafana Lokibreadcrumb arrow Querybreadcrumb arrow Metric queries
Open source

Metric queries

Metric queries extend log queries by applying a function to log query results. This powerful feature creates metrics from logs.

Metric queries can be used to calculate the rate of error messages or the top N log sources with the greatest quantity of logs over the last 3 hours.

Combined with parsers, metric queries can also be used to calculate metrics from a sample value within the log line, such as latency or request size. All labels, including extracted ones, will be available for aggregations and generation of new series.

Range Vector aggregation

LogQL shares the range vector concept of Prometheus. In Grafana Loki, the selected range of samples is a range of selected log or label values.

The aggregation is applied over a time duration. Loki defines Time Durations with the same syntax as Prometheus.

Loki supports two types of range vector aggregations: log range aggregations and unwrapped range aggregations.

Log range aggregations

A log range aggregation is a query followed by a duration. A function is applied to aggregate the query over the duration. The duration can be placed after the log stream selector or at end of the log pipeline.

The functions:

  • rate(log-range): calculates the number of entries per second
  • count_over_time(log-range): counts the entries for each log stream within the given range.
  • bytes_rate(log-range): calculates the number of bytes per second for each stream.
  • bytes_over_time(log-range): counts the amount of bytes used by each log stream for a given range.
  • absent_over_time(log-range): returns an empty vector if the range vector passed to it has any elements and a 1-element vector with the value 1 if the range vector passed to it has no elements. (absent_over_time is useful for alerting on when no time series and logs stream exist for label combination for a certain amount of time.)

Examples:

  • Count all the log lines within the last five minutes for the MySQL job.

    logql
    count_over_time({job="mysql"}[5m])
  • This aggregation includes filters and parsers. It returns the per-second rate of all non-timeout errors within the last minutes per host for the MySQL job and only includes errors whose duration is above ten seconds.

    logql
    sum by (host) (rate({job="mysql"} |= "error" != "timeout" | json | duration > 10s [1m]))

Offset modifier

The offset modifier allows changing the time offset for individual range vectors in a query.

For example, the following expression counts all the logs within the last ten minutes to five minutes rather than last five minutes for the MySQL job. Note that the offset modifier always needs to follow the range vector selector immediately.

logql
count_over_time({job="mysql"}[5m] offset 5m) // GOOD
count_over_time({job="mysql"}[5m]) offset 5m // INVALID

Unwrapped range aggregations

Unwrapped ranges uses extracted labels as sample values instead of log lines. However to select which label will be used within the aggregation, the log query must end with an unwrap expression and optionally a label filter expression to discard errors.

The unwrap expression is noted | unwrap label_identifier where the label identifier is the label name to use for extracting sample values.

Since label values are string, by default a conversion into a float (64bits) will be attempted, in case of failure the __error__ label is added to the sample. Optionally the label identifier can be wrapped by a conversion function | unwrap <function>(label_identifier), which will attempt to convert the label value from a specific format.

We currently support the functions:

  • duration_seconds(label_identifier) (or its short equivalent duration) which will convert the label value in seconds from the go duration format (e.g 5m, 24s30ms).
  • bytes(label_identifier) which will convert the label value to raw bytes applying the bytes unit (e.g. 5 MiB, 3k, 1G).

Supported function for operating over unwrapped ranges are:

  • rate(unwrapped-range): calculates per second rate of the sum of all values in the specified interval.
  • rate_counter(unwrapped-range): calculates per second rate of the values in the specified interval and treating them as “counter metric”
  • sum_over_time(unwrapped-range): the sum of all values in the specified interval.
  • avg_over_time(unwrapped-range): the average value of all points in the specified interval.
  • max_over_time(unwrapped-range): the maximum value of all points in the specified interval.
  • min_over_time(unwrapped-range): the minimum value of all points in the specified interval
  • first_over_time(unwrapped-range): the first value of all points in the specified interval
  • last_over_time(unwrapped-range): the last value of all points in the specified interval
  • stdvar_over_time(unwrapped-range): the population standard variance of the values in the specified interval.
  • stddev_over_time(unwrapped-range): the population standard deviation of the values in the specified interval.
  • quantile_over_time(scalar,unwrapped-range): the φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval.
  • absent_over_time(unwrapped-range): returns an empty vector if the range vector passed to it has any elements and a 1-element vector with the value 1 if the range vector passed to it has no elements. (absent_over_time is useful for alerting on when no time series and logs stream exist for label combination for a certain amount of time.)

Except for sum_over_time,absent_over_time, rate and rate_counter, unwrapped range aggregations support grouping.

logql
<aggr-op>([parameter,] <unwrapped-range>) [without|by (<label list>)]

Which can be used to aggregate over distinct labels dimensions by including a without or by clause.

without removes the listed labels from the result vector, while all other labels are preserved the output. by does the opposite and drops labels that are not listed in the by clause, even if their label values are identical between all elements of the vector.

See Unwrap examples for query examples that use the unwrap expression.

Built-in aggregation operators

Like PromQL, LogQL supports a subset of built-in aggregation operators that can be used to aggregate the element of a single vector, resulting in a new vector of fewer elements but with aggregated values:

  • sum: Calculate sum over labels
  • avg: Calculate the average over labels
  • min: Select minimum over labels
  • max: Select maximum over labels
  • stddev: Calculate the population standard deviation over labels
  • stdvar: Calculate the population standard variance over labels
  • count: Count number of elements in the vector
  • topk: Select largest k elements by sample value
  • bottomk: Select smallest k elements by sample value
  • sort: returns vector elements sorted by their sample values, in ascending order.
  • sort_desc: Same as sort, but sorts in descending order.

The aggregation operators can either be used to aggregate over all label values or a set of distinct label values by including a without or a by clause:

logql
<aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]

parameter is required when using topk and bottomk. topk and bottomk are different from other aggregators in that a subset of the input samples, including the original labels, are returned in the result vector.

by and without are only used to group the input vector. The without clause removes the listed labels from the resulting vector, keeping all others. The by clause does the opposite, dropping labels that are not listed in the clause, even if their label values are identical between all elements of the vector.

See vector aggregation examples for query examples that use vector aggregation expressions.

Functions

LogQL supports a set of built-in functions.

  • vector(s scalar): returns the scalar s as a vector with no labels. This behaves identically to the Prometheus vector() function. vector is mainly used to return a value for a series that would otherwise return nothing; this can be useful when using LogQL to define an alert.

Examples:

  • Count all the log lines within the last five minutes for the traefik namespace.

    logql
    sum(count_over_time({namespace="traefik"}[5m])) # will return nothing
      or
    vector(0) # will return 0

Probabilistic aggregation

Note

Probabilistic aggregation is an experimental feature. Engineering and on-call support is not available. Documentation is either limited or not provided outside of code comments. No SLA is provided. To use this feature, set limits_config.shard_aggregations:approx_topk in your Loki configuration. To enable this feature in Grafana Cloud, contact Grafana Support.

LogQL’s approx_topk function provides a probabilistic approximation of topk. It is a drop-in replacement for topk that is great for when topk queries time out or hit the maximum series limit. This tends to happen when the list of values that you’re sorting through in order to find the most frequent values is very large. approx_topk is also great in cases where a faster, approximate answer is preferred to a slower, more accurate one.

The function is of the form:

logql
approx_topk(k, <vector expression>)

approx_topk is only supported for instant queries. Grouping is also not supported and should be handled by an inner sum by or sum without even though this might not be the same behavior as topk by.

Under the hood, approx_topk is implemented using sharding. The count-min sketch algorithm and a heap are used to approximate the counts for each shard. The accuracy of the approximation depends on the size of the heap, which is defined by Loki’smax_count_min_sketch_heap_size parameter. Accuracy decreases as k approaches the size of the heap (which has a default size of 10,000).

The expression approx_topk(k,inner) becomes

topk(
  k,
  eval_cms(
    __count_min_sketch__(inner, shard=1) ++ __count_min_sketch__(inner, shard=2)...
  )
)

__count_min_sketch__ is calculated for each shard and merged on the frontend. Then eval_cms iterates through the labels list and determines the count for each. Then topk selects the top items.

Further resources

  • Watch: How to turn logs into metrics with Grafana Loki (Loki Community Call July 2025)

Was this page helpful?

Suggest an edit in GitHub
Create a GitHub issue
Email docs@grafana.com
Help and support
Community

Related resources from Grafana Labs

Additional helpful documentation, links, and articles:
webinar icon
Video
Getting started with logging and Grafana Loki
Getting started with logging and Grafana Loki
See a demo of the updated features in Loki, and how to create metrics from logs and alert on your logs with powerful Prometheus-style alerting rules.
video icon
Video
Essential Grafana Loki configuration settings
Essential Grafana Loki configuration settings
This webinar focuses on Grafana Loki configuration including agents Promtail and Docker; the Loki server; and Loki storage for popular backends.
video icon
Video
Scaling and securing your logs with Grafana Loki
Scaling and securing your logs with Grafana Loki
This webinar covers the challenges of scaling and securing logs, and how Grafana Cloud Logs powered by Grafana Loki can help, cost-effectively.
Technical documentation Plugin catalog
Choose a product
Viewing: v3.7.x (latest) Find another version
  • Grafana Loki
  • Release notes
    • Release cadence
    • v3.7
    • v3.6
    • v3.5
    • v3.4
    • v3.3
    • v3.2
    • v3.1
    • v3.0
    • V2.9
    • V2.8
    • V2.7
    • V2.6
    • V2.5
    • V2.4
    • V2.3
  • Get started
    • Loki overview
    • Quick Start
      • Loki quickstart
      • Loki Tutorial
    • Architecture
    • Components
    • Deployment modes
    • Labels
      • Label best practices
      • Cardinality
      • Structured metadata
      • Modify default labels
    • Hash rings
  • Set up
    • Size the cluster
    • Install
      • Install using Helm
        • Helm chart components
        • Install monolithic Loki
        • Install microservice Loki
        • Install scalable Loki
        • Cloud Deployment Guides
          • Deploy on AWS
          • Deploy on Azure
          • Deploy on GCP
        • Configure storage
        • Helm chart values
        • Monitoring
      • Install using Tanka
      • Install using Docker
      • Install locally
      • Install on Istio
      • Install from source
    • Migrate
      • Migrate to Alloy
      • Migrate from SSD to distributed
      • Migrate to TSDB
      • Migrate from `loki-distributed`
      • Migrate to three targets
      • Migrate to Thanos storage clients
    • Upgrade
      • Upgrade the Helm chart to 3.0
      • Upgrade the Helm chart to 6.0
      • Upgrade to Community Helm chart
  • Configure
    • Best practices
    • Storage
    • Usage statistics
    • Examples
      • Configuration
      • Thanos storage examples
      • Query frontend example
  • Send data
    • Grafana Alloy
      • Sending Logs to Loki via Kafka using Alloy
      • Sending OpenTelemetry logs to Loki using Alloy
    • OpenTelemetry
      • Native OTLP endpoint vs Loki Exporter
      • OTel Collector tutorial
    • Kubernetes Monitoring Helm
    • Promtail
    • Docker driver
      • Configure Docker driver
    • Fluent Bit
      • Fluent Bit tutorial
      • Fluent Bit Community Plugin
      • Fluent Bit
    • Fluentd
    • Lambda Promtail
    • Logstash plugin
    • k6 load testing
      • Log generation
      • Write path testing
      • Query testing
  • Query
    • Query best practices
    • LogQL simulator
    • Log queries
    • Metric queries
    • Template functions
    • LogCLI
      • Getting started
      • LogCLI tutorial
    • Matching IP addresses
    • Query examples
    • Query acceleration
    • Query Reference
    • Troubleshoot queries
  • Visualize
  • Alert
  • Manage
    • Loki Canary
    • Block unwanted queries
    • Caching
    • Rate limits
    • Query fairness
    • Shuffle sharding
    • Monitor Loki
      • Deploy Loki Meta Monitoring
      • Install Mixins
      • Single Binary Meta Monitoring
      • Key Metrics
    • Troubleshooting
      • Troubleshoot operations
      • Troubleshoot ingestion
      • Troubleshoot drilldown
      • Troubleshoot queries
    • Authentication
    • Bloom filters
    • Automatic stream sharding
    • Scale Loki
    • Recording rules
    • Storage
      • TSDB
      • BoltDB Shipper
      • Filesystem object store
      • Storage schema
      • Write Ahead Log
      • Log retention
      • Log entry deletion
      • Horizontal scaling of Compactor
      • Legacy storage
      • Table manager
    • Multi-tenancy
    • Autoscaling queriers
    • Upgrade
    • Overrides Exporter
    • Zone aware ingesters
  • Reference
    • Loki configuration reference
    • Loki HTTP API
    • Python examples
  • Community
    • Contacting the Loki Team
    • Contributing to Loki
    • Governance
    • Maintaining
      • Releasing Grafana Loki
        • Backport commits
        • Create Release Branch
        • Document metrics and configurations changes
        • Merge Release PR
        • Patch Go version
        • Patch vulnerabilities
        • Prepare Major Release
        • Prepare Release
        • Prepare Upgrade guide
        • Update version numbers
        • Version
      • Releasing Loki Build Image
    • Loki Improvement Documents (LIDs)
      • 0001: Introducing LIDs
      • 0002: Remote Rule Evaluation
      • 0003: Query fairness across users within tenants
      • 0004: Index Gateway Sharding
      • 0005: Loki mixin configuration improvements
      • 0006: Expose Split Logic in API
    • Design documents
      • Labels
      • Promtail Push API
      • Write-Ahead Logs
      • Ordering Constraint Removal
  • Copyright notice
Scroll for more
Read this in Grafana Cloud

Is this page helpful?

On this page
  • Range Vector aggregation
  • Log range aggregations
  • Unwrapped range aggregations
  • Built-in aggregation operators
  • Functions
  • Probabilistic aggregation
  • Further resources
Scroll for more

Still have questions?

Ask your questions. Let AI do the heavy lifting.

Ask AI icon
Newsletter icon

Get every update

Subscribe to our newsletter

By submitting, you agree to our Privacy policy

Grafana Cloud

  • Overview
  • Pricing
  • What's in the free tier?
  • AI Assistant
  • Application Observability
  • Kubernetes Monitoring
  • Dashboards & Visualization
  • Database Observability
  • Frontend Observability
  • Synthetic Monitoring
  • Performance & Load Testing
  • Incident Response & Management
  • What’s New
  • Grafana Cloud Status

Solutions

  • AI Observability
  • Full-Stack Observability
  • Infrastructure & Cloud Observability
  • Digital Experience Monitoring
  • Scaled Prometheus
  • Cost Management & Optimization
  • Site Reliability
  • Log Management
  • Migrate to OpenTelemetry

Integrations

  • All Integrations
  • All Plugins
  • AWS
  • Google Cloud
  • Microsoft Azure
  • Kubernetes
  • Datadog
  • New Relic

Open Source

  • Our Projects
  • GitHub
  • Downloads
  • Dashboard Templates

Learn

  • Documentation
  • Blog
  • Community
  • Events
  • Observability Survey & Reports

Company

  • About Grafana Labs
  • Careers
  • Partnerships
  • Newsroom
  • Success Stories
  • Contact Us
  • Getting Help
  • Professional Services
  • Hey AI

Compare

  • Datadog vs. Grafana Cloud
  • Dynatrace vs. Grafana Cloud
  • Elasticsearch vs. Grafana Cloud
  • New Relic vs. Grafana Cloud
  • PagerDuty vs. Grafana Cloud
  • Splunk vs. Grafana Cloud
Grafana Labs x unique logomark

Donut take our word for it. Try Grafana Cloud today.

Grafana Cloud StatusLegal & SecurityTerms of ServicePrivacy PolicyTrademark Policy

Copyright 2026 © Grafana Labs

FacebookXLinkedinGithubYoutubeReddit
Grafana Labs uses cookies for the normal operation of this website. Learn more.