github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/logql/metric_queries.md (about) 1 --- 2 title: Metric queries 3 weight: 20 4 --- 5 6 # Metric queries 7 8 Metric queries extend log queries by applying a function to log query results. 9 This powerful feature creates metrics from logs. 10 11 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. 12 13 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. 14 All labels, including extracted ones, will be available for aggregations and generation of new series. 15 16 ## Range Vector aggregation 17 18 LogQL shares the [range vector](https://prometheus.io/docs/prometheus/latest/querying/basics/#range-vector-selectors) concept of Prometheus. 19 In Grafana Loki, the selected range of samples is a range of selected log or label values. 20 21 The aggregation is applied over a time duration. 22 Loki defines [Time Durations](https://prometheus.io/docs/prometheus/latest/querying/basics/#time-durations) with the same syntax as Prometheus. 23 24 Loki supports two types of range vector aggregations: log range aggregations and unwrapped range aggregations. 25 26 ### Log range aggregations 27 28 A log range aggregation is a query followed by a duration. 29 A function is applied to aggregate the query over the duration. 30 The duration can be placed 31 after the log stream selector or at end of the log pipeline. 32 33 The functions: 34 35 - `rate(log-range)`: calculates the number of entries per second 36 - `count_over_time(log-range)`: counts the entries for each log stream within the given range. 37 - `bytes_rate(log-range)`: calculates the number of bytes per second for each stream. 38 - `bytes_over_time(log-range)`: counts the amount of bytes used by each log stream for a given range. 39 - `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.) 40 41 Examples: 42 43 - Count all the log lines within the last five minutes for the MySQL job. 44 45 ```logql 46 count_over_time({job="mysql"}[5m]) 47 ``` 48 49 - This aggregation includes filters and parsers. 50 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. 51 52 ```logql 53 sum by (host) (rate({job="mysql"} |= "error" != "timeout" | json | duration > 10s [1m])) 54 ``` 55 56 ### Unwrapped range aggregations 57 58 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](../#pipeline-errors). 59 60 The unwrap expression is noted `| unwrap label_identifier` where the label identifier is the label name to use for extracting sample values. 61 62 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. 63 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. 64 65 We currently support the functions: 66 - `duration_seconds(label_identifier)` (or its short equivalent `duration`) which will convert the label value in seconds from the [go duration format](https://golang.org/pkg/time/#ParseDuration) (e.g `5m`, `24s30ms`). 67 - `bytes(label_identifier)` which will convert the label value to raw bytes applying the bytes unit (e.g. `5 MiB`, `3k`, `1G`). 68 69 Supported function for operating over unwrapped ranges are: 70 71 - `rate(unwrapped-range)`: calculates per second rate of the sum of all values in the specified interval. 72 - `rate_counter(unwrapped-range)`: calculates per second rate of the values in the specified interval and treating them as "counter metric" 73 - `sum_over_time(unwrapped-range)`: the sum of all values in the specified interval. 74 - `avg_over_time(unwrapped-range)`: the average value of all points in the specified interval. 75 - `max_over_time(unwrapped-range)`: the maximum value of all points in the specified interval. 76 - `min_over_time(unwrapped-range)`: the minimum value of all points in the specified interval 77 - `first_over_time(unwrapped-range)`: the first value of all points in the specified interval 78 - `last_over_time(unwrapped-range)`: the last value of all points in the specified interval 79 - `stdvar_over_time(unwrapped-range)`: the population standard variance of the values in the specified interval. 80 - `stddev_over_time(unwrapped-range)`: the population standard deviation of the values in the specified interval. 81 - `quantile_over_time(scalar,unwrapped-range)`: the φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval. 82 - `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.) 83 84 Except for `sum_over_time`,`absent_over_time` and `rate`, unwrapped range aggregations support grouping. 85 86 ```logql 87 <aggr-op>([parameter,] <unwrapped-range>) [without|by (<label list>)] 88 ``` 89 90 Which can be used to aggregate over distinct labels dimensions by including a `without` or `by` clause. 91 92 `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. 93 94 See [Unwrap examples](../query_examples/#unwrap-examples) for query examples that use the unwrap expression. 95 96 ## Built-in aggregation operators 97 98 Like [PromQL](https://prometheus.io/docs/prometheus/latest/querying/operators/#aggregation-operators), 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: 99 100 - `sum`: Calculate sum over labels 101 - `avg`: Calculate the average over labels 102 - `min`: Select minimum over labels 103 - `max`: Select maximum over labels 104 - `stddev`: Calculate the population standard deviation over labels 105 - `stdvar`: Calculate the population standard variance over labels 106 - `count`: Count number of elements in the vector 107 - `topk`: Select largest k elements by sample value 108 - `bottomk`: Select smallest k elements by sample value 109 110 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: 111 112 ```logql 113 <aggr-op>([parameter,] <vector expression>) [without|by (<label list>)] 114 ``` 115 116 `parameter` is required when using `topk` and `bottomk`. 117 `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. 118 119 `by` and `without` are only used to group the input vector. 120 The `without` clause removes the listed labels from the resulting vector, keeping all others. 121 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. 122 123 See [vector aggregation examples](../query_examples/#vector-aggregation-examples) for query examples that use vector aggregation expressions.