Project: Grafana Loki (Observability and Analysis / Observability)
File: metrics.md

title: metrics menuTitle: description: The 'metrics' Promtail pipeline stage. aliases: ../../../clients/promtail/stages/metrics/ weight: The `metrics` stage is an action stage that allows for defining and updating metrics based on data from the extracted map. Note that created metrics are not pushed to Loki and are instead exposed via Promtail's `/metrics` endpoint. Prometheus should be configured to scrape Promtail to be able to retrieve the metrics configured by this stage. If Promtail's configuration is reloaded, all metrics will be reset. ```yaml metrics: [<string>: [ <metriccounter> | <metricgauge> | <metric_histogram> ] ...] ``` Defines a counter metric whose value only goes up. ```yaml type: Counter [description: <string>] [prefix: <string>] [source: <string>] [maxidleduration: <string>] config: [match_all: <bool>] [countentrybytes: <bool>] [value: <string>] action: <string> ``` Defines a gauge metric whose value can go up or down. ```yaml type: Gauge [description: <string>] [prefix: <string>] [source: <string>] [maxidleduration: <string>] config: [value: <string>] action: <string> ``` Defines a histogram metric whose values are bucketed. ```yaml type: Histogram [description: <string>] [prefix: <string>] [source: <string>] [maxidleduration: <string>] config: [value: <string>] buckets: <int> ``` ```yaml metrics: loglinestotal: type: Counter description: "total number of log lines" prefix: mypromtailcustom_ maxidleduration: 24h config: match_all: true action: inc logbytestotal: type: Counter description: "total bytes of log lines" prefix: mypromtailcustom_ maxidleduration: 24h config: match_all: true countentrybytes: true action: add ``` This pipeline creates a `loglinestotal` counter which increments for every log line received by using the `match_all: true` parameter. It also creates a `logbytestotal` counter which adds the byte size of every log line received to the counter by using the `countentrybytes: true` parameter. Those two metrics will disappear after 24h if they don't receive new entries, this is useful to reduce the building up of stage metrics. The combination of these two metric stages will give you two counters to track the volume of every log stream in both number of lines and bytes, which can be useful in identifying sources of very high volume, as well as helping understand why you may have too much cardinality. These stages should be placed towards the end of your pipeline after any `labels` stages ```yaml regex: expression: "^.(?P<order_success>order

metrics: successfulorderstotal: type: Counter description: "log lines with the message `order successful`" source: order_success config: action: inc ``` This pipeline first tries to find `order successful` in the log line, extracting it as the `order_success` field in the extracted map. The metrics stage then creates a metric called `successfulorderstotal` whose value only increases when `order_success` was found in the extracted map. The result of this pipeline is a metric whose value only increases when a log line with the text `order successful` was scraped by Promtail. ```yaml regex: expression: "^. order_status=(?P<order_status>.?) .*$" metrics: successfulorderstotal: type: Counter description: "successful orders" source: order_status config: value: success action: inc failedorderstotal: type: Counter description: "failed orders" source: order_status config: value: fail action: inc ``` This pipeline first tries to find text in the format `order_status=<value>` in the log line, pulling out the `<value>` into the extracted map with the key `order_status`. The metric stages creates `successfulorderstotal` and `failedorderstotal` metrics that only increment when the value of `order_status` in the extracted map is `success` or `fail` respectively. Gauge examples will be very similar to Counter examples with additional `action` values. ```yaml regex: expression: "^. retries=(?P<retries>\d+) .$" metrics: retries_total: type: Gauge description: "total retries" source: retries config: action: add ``` This pipeline first tries to find text in the format `retries=<value>` in the log line, pulling out the `<value>` into the extracted map with the key `retries`. Note that the regex only parses numbers for the value in `retries`. The metrics stage then creates a Gauge whose current value will be added to the number in the `retries` field from the extracted map. ```yaml metrics: httpresponsetime_seconds: type: Histogram description: "length of each log line" source: response_time config: buckets: [0.001,0.0025,0.005,0.010,0.025,0.050] ``` This pipeline creates a histogram that reads `response_time` from the extracted map and places it into a bucket, both increasing the count of the bucket and the sum for that particular bucket. The metric values extracted from the log data are internally converted to floating points. The supported values are the following: integers, floating point numbers string - two types of string formats are supported: strings that represent floating point numbers: e.g., `"0.804"` is converted to `0.804`. duration format strings. Valid time units are "ns", "us", "ms", "s", "m", "h". Values in this format are converted as a floating point number of seconds. E.g., `"0.5ms"` is converted to `0.0005`. boolean: `true` is converted to `1` `false` is converted to `0`