metrics
注意
Promtail 已被弃用,并将通过长期支持 (LTS) 维护至 2026 年 2 月 28 日。Promtail 将于 2026 年 3 月 2 日终止支持 (EOL)。您可以在此处找到迁移资源。
metrics
阶段是一个动作阶段,它允许基于从提取的映射中获取的数据定义和更新指标。请注意,创建的指标不会推送到 Loki,而是通过 Promtail 的 /metrics
端点暴露。应配置 Prometheus 来抓取 Promtail,以便检索此阶段配置的指标。如果重新加载 Promtail 的配置,所有指标都将重置。
模式
# A map where the key is the name of the metric and the value is a specific
# metric type.
metrics:
[<string>: [ <metric_counter> | <metric_gauge> | <metric_histogram> ] ...]
metric_counter
定义一个只递增的计数器指标。
# The metric type. Must be Counter.
type: Counter
# Describes the metric.
[description: <string>]
# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]
# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <string>]
# Label values on metrics are dynamic which can cause exported metrics
# to go stale (for example when a stream stops receiving logs).
# To prevent unbounded growth of the /metrics endpoint any metrics which
# have not been updated within this time will be removed.
# Must be greater than or equal to '1s', if undefined default is '5m'
[max_idle_duration: <string>]
config:
# If present and true all log lines will be counted without attempting
# to match the `value` to the field specified by `source` in the extracted map.
# It is an error to specify `match_all: true` and also specify a `value`
[match_all: <bool>]
# If present and true all log line bytes will be counted.
# It is an error to specify `count_entry_bytes: true` without specifying `match_all: true`
# It is an error to specify `count_entry_bytes: true` without specifying `action: add`
[count_entry_bytes: <bool>]
# Filters down source data and only changes the metric
# if the targeted value exactly matches the provided string.
# If not present, all data will match.
[value: <string>]
# Must be either "inc" or "add" (case insensitive). If
# inc is chosen, the metric value will increase by 1 for each
# log line received that passed the filter. If add is chosen,
# the extracted value must be convertible to a positive float
# and its value will be added to the metric.
action: <string>
metric_gauge
定义一个值可以增减的仪表盘指标。
# The metric type. Must be Gauge.
type: Gauge
# Describes the metric.
[description: <string>]
# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]
# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <string>]
# Label values on metrics are dynamic which can cause exported metrics
# to go stale (for example when a stream stops receiving logs).
# To prevent unbounded growth of the /metrics endpoint any metrics which
# have not been updated within this time will be removed.
# Must be greater than or equal to '1s', if undefined default is '5m'
[max_idle_duration: <string>]
config:
# Filters down source data and only changes the metric
# if the targeted value exactly matches the provided string.
# If not present, all data will match.
[value: <string>]
# Must be either "set", "inc", "dec"," add", or "sub". If
# add, set, or sub is chosen, the extracted value must be
# convertible to a positive float. inc and dec will increment
# or decrement the metric's value by 1 respectively.
action: <string>
metric_histogram
定义一个值按桶分类的直方图指标。
# The metric type. Must be Histogram.
type: Histogram
# Describes the metric.
[description: <string>]
# Defines custom prefix name for the metric. If undefined, default name "promtail_custom_" will be prefixed.
[prefix: <string>]
# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <string>]
# Label values on metrics are dynamic which can cause exported metrics
# to go stale (for example when a stream stops receiving logs).
# To prevent unbounded growth of the /metrics endpoint any metrics which
# have not been updated within this time will be removed.
# Must be greater than or equal to '1s', if undefined default is '5m'
[max_idle_duration: <string>]
config:
# Filters down source data and only changes the metric
# if the targeted value exactly matches the provided string.
# If not present, all data will match.
[value: <string>]
# Holds all the numbers in which to bucket the metric.
buckets:
- <int>
示例
计数器
- metrics:
log_lines_total:
type: Counter
description: "total number of log lines"
prefix: my_promtail_custom_
max_idle_duration: 24h
config:
match_all: true
action: inc
log_bytes_total:
type: Counter
description: "total bytes of log lines"
prefix: my_promtail_custom_
max_idle_duration: 24h
config:
match_all: true
count_entry_bytes: true
action: add
此管线创建一个 log_lines_total
计数器,通过使用 match_all: true
参数,该计数器对接收到的每行日志进行递增。
它还创建一个 log_bytes_total
计数器,通过使用 count_entry_bytes: true
参数,该计数器会将接收到的每行日志的字节大小加到计数器中。
如果这两个指标在 24 小时内未收到新条目,它们将消失,这有助于减少阶段指标的累积。
这两个指标阶段的组合将为您提供两个计数器,用于以行数和字节数来跟踪每个日志流的容量,这对于识别高容量源以及帮助理解为什么您可能有过多的基数非常有用。
这些阶段应放置在您的管线末尾,在任何 labels
阶段之后。
- regex:
expression: "^.*(?P<order_success>order successful).*$"
- metrics:
successful_orders_total:
type: Counter
description: "log lines with the message `order successful`"
source: order_success
config:
action: inc
此管线首先尝试在日志行中查找 order successful
,并将其作为提取映射中的 order_success
字段提取。然后,指标阶段创建一个名为 successful_orders_total
的指标,其值仅在提取映射中找到 order_success
时才会增加。
此管线的结果是一个指标,其值仅在 Promtail 抓取到包含文本 order successful
的日志行时才会增加。
- regex:
expression: "^.* order_status=(?P<order_status>.*?) .*$"
- metrics:
successful_orders_total:
type: Counter
description: "successful orders"
source: order_status
config:
value: success
action: inc
failed_orders_total:
type: Counter
description: "failed orders"
source: order_status
config:
value: fail
action: inc
此管线首先尝试在日志行中查找格式为 order_status=<value>
的文本,并将 <value>
提取到提取映射中,键为 order_status
。
指标阶段创建 successful_orders_total
和 failed_orders_total
指标,它们的值仅在提取映射中 order_status
的值分别为 success
或 fail
时才会增加。
仪表盘指标
仪表盘指标示例与计数器示例非常相似,并包含额外的 action
值。
- regex:
expression: "^.* retries=(?P<retries>\d+) .*$"
- metrics:
retries_total:
type: Gauge
description: "total retries"
source: retries
config:
action: add
此管线首先尝试在日志行中查找格式为 retries=<value>
的文本,并将 <value>
提取到提取映射中,键为 retries
。请注意,正则表达式只解析 retries
中的数字值。
然后,指标阶段创建一个仪表盘指标 (Gauge),其当前值将添加到从提取映射中 retries
字段获取的数字。
直方图
- metrics:
http_response_time_seconds:
type: Histogram
description: "distribution of log response time"
source: response_time
config:
buckets: [0.001,0.0025,0.005,0.010,0.025,0.050]
此管线创建一个直方图,它从提取映射中读取 response_time
并将其放入一个桶中,同时增加桶的计数以及该特定桶的总和。
支持的值类型
从日志数据中提取的指标值在内部会转换为浮点数。支持的值类型如下:
- 整数,浮点数
- 字符串 - 支持两种字符串格式
- 表示浮点数的字符串:例如,
"0.804"
会转换为0.804
。 - 持续时间格式字符串。有效时间单位为“ns”、“us”、“ms”、“s”、“m”、“h”。此格式的值将转换为以秒为单位的浮点数。例如,
"0.5ms"
会转换为0.0005
。
- 表示浮点数的字符串:例如,
- 布尔值
true
会转换为1
false
会转换为0