菜单
开源

配置 Promtail

您可以使用 Grafana Cloud 来避免自行安装、维护和扩展 Grafana Loki 实例。 创建免费账户开始使用,其中包含永久免费访问 1 万指标、50GB 日志、50GB 追踪、500 VUh k6 测试等。

注意

Promtail 已被弃用,并将提供长期支持 (LTS) 至 2026 年 2 月 28 日。Promtail 将于 2026 年 3 月 2 日达到生命周期结束 (EOL)。您可以在此处找到迁移资源。

Promtail 在 YAML 文件中配置(通常称为 config.yaml),该文件包含 Promtail 服务器信息、位置存储位置以及如何从文件抓取日志的信息。

运行时打印 Promtail 配置

如果您向 Promtail 传递 -print-config-stderr-log-config-reverse-order(或 -print-config-stderr=true)标志,Promtail 将转储其创建的完整配置对象,该对象首先结合内置默认值,然后应用配置文件的覆盖,最后应用命令行标志的覆盖。

结果是 Promtail 配置结构中每个配置对象的值。

某些值可能与您的安装无关,这是预期的,因为每个选项无论是否使用都有一个默认值。

这个配置是 Promtail 运行时将使用的配置,对于调试配置相关问题非常宝贵,尤其有助于确保您的配置文件和标志被正确读取和加载。

当直接运行 Promtail(例如 ./promtail)时,-print-config-stderr 很方便,因为您可以快速获取整个 Promtail 配置的输出。

-log-config-reverse-order 是我们在所有环境中运行 Promtail 时使用的标志,配置条目是反向排列的,以便在 Grafana 的 Explore 中查看时,配置的顺序从上到下正确读取。

配置文件参考

要指定加载哪个配置文件,请在命令行传递 -config.file 标志。文件采用 YAML 格式编写,由以下模式定义。方括号表示参数是可选的。对于非列表参数,值设置为指定的默认值。

有关如何配置发现目标并从中抓取日志的更详细信息,请参阅抓取 (Scraping)。有关如何转换从抓取目标获取的日志的更多信息,请参阅Pipeline

运行时重载

Promtail 可以在运行时重载其配置。如果新配置格式不正确,则不会应用更改。通过向 Promtail 进程发送 SIGHUP 或向 /reload 端点发送 HTTP POST 请求(当启用 --server.enable-runtime-reload 标志时)来触发配置重载。

在配置中使用环境变量

您可以在配置文件中使用环境变量引用来设置部署期间需要可配置的值。为此,请传递 -config.expand-env=true 并使用

${VAR}

其中 VAR 是环境变量的名称。

每个变量引用在启动时会被替换为环境变量的值。替换是大小写敏感的,并在解析 YAML 文件之前发生。对未定义变量的引用会被替换为空字符串,除非您指定了默认值或自定义错误文本。

要指定默认值,请使用

${VAR:-default_value}

其中 default_value 是在环境变量未定义时使用的值。

注意

使用 expand-env=true,配置将首先通过 envsubst 运行,该工具会将双反斜杠替换为单反斜杠。因此,每一次使用反斜杠 \ 都需要将其替换为双反斜杠 \\

通用占位符

  • <boolean>: 可取值为 truefalse 的布尔值
  • <int>: 匹配正则表达式 [1-9]+[0-9]* 的任何整数
  • <duration>: 匹配正则表达式 [0-9]+(ms|[smhdwy]) 的持续时间
  • <labelname>: 匹配正则表达式 [a-zA-Z_][a-zA-Z0-9_]* 的字符串
  • <labelvalue>: Unicode 字符组成的字符串
  • <filename>: 相对于当前工作目录的有效路径或绝对路径。
  • <host>: 由主机名或 IP 后跟可选端口号组成的有效字符串
  • <string>: 字符串
  • <secret>: 表示秘密的字符串,例如密码

config.yaml 支持的内容和默认值

yaml
# Configures global settings which impact all targets.
[global: <global_config>]

# Configures the server for Promtail.
[server: <server_config>]

# Describes how Promtail connects to multiple instances
# of Grafana Loki, sending logs to each.
# WARNING: If one of the remote Loki servers fails to respond or responds
# with any error which is retryable, this will impact sending logs to any
# other configured remote Loki servers.  Sending is done on a single thread!
# It is generally recommended to run multiple Promtail clients in parallel
# if you want to send to multiple remote Loki instances.
clients:
  - [<client_config>]

# Describes how to save read file offsets to disk
[positions: <position_config>]

scrape_configs:
  - [<scrape_config>]

# Configures global limits for this instance of Promtail
[limits_config: <limits_config>]

# Configures how tailed targets will be watched.
[target_config: <target_config>]

# Configures additional promtail configurations.
[options: <options_config>]

# Configures tracing support
[tracing: <tracing_config>]

global

global 块配置影响所有抓取目标的全局设置

yaml
# Configure how frequently log files from disk get polled for changes.
[file_watch_config: <file_watch_config>]

file_watch_config

file_watch_config 块配置了从磁盘轮询日志文件以检查更改的频率

yaml
# Minimum frequency to poll for files. Any time file changes are detected, the
# poll frequency gets reset to this duration.
[min_poll_frequency: <duration> | default = "250ms"]

# Maximum frequency to poll for files. Any time no file changes are detected,
# the poll frequency doubles in value up to the maximum duration specified by
# this value.
#
# The default is set to the same as min_poll_frequency.
[max_poll_frequency: <duration> | default = "250ms"]

server

server 块配置 Promtail 作为 HTTP 服务器的行为

yaml
# Disable the HTTP and GRPC server.
[disable: <boolean> | default = false]

# Enable the /debug/fgprof and /debug/pprof endpoints for profiling.
[profiling_enabled: <boolean> | default = false]

# HTTP server listen host
[http_listen_address: <string>]

# HTTP server listen port (0 means random port)
[http_listen_port: <int> | default = 80]

# gRPC server listen host
[grpc_listen_address: <string>]

# gRPC server listen port (0 means random port)
[grpc_listen_port: <int> | default = 9095]

# Register instrumentation handlers (/metrics, etc.)
[register_instrumentation: <boolean> | default = true]

# Timeout for graceful shutdowns
[graceful_shutdown_timeout: <duration> | default = 30s]

# Read timeout for HTTP server
[http_server_read_timeout: <duration> | default = 30s]

# Write timeout for HTTP server
[http_server_write_timeout: <duration> | default = 30s]

# Idle timeout for HTTP server
[http_server_idle_timeout: <duration> | default = 120s]

# Max gRPC message size that can be received
[grpc_server_max_recv_msg_size: <int> | default = 4194304]

# Max gRPC message size that can be sent
[grpc_server_max_send_msg_size: <int> | default = 4194304]

# Limit on the number of concurrent streams for gRPC calls (0 = unlimited)
[grpc_server_max_concurrent_streams: <int> | default = 100]

# Log only messages with the given severity or above. Supported values [debug,
# info, warn, error]
[log_level: <string> | default = "info"]

# Base path to server all API routes from (e.g., /v1/).
[http_path_prefix: <string>]

# Target managers check flag for Promtail readiness, if set to false the check is ignored
[health_check_target: <bool> | default = true]

# Enable reload via HTTP request.
[enable_runtime_reload: <bool> | default = false]

clients

clients 块配置 Promtail 如何连接到 Loki 实例

yaml
# The URL where Loki is listening, denoted in Loki as http_listen_address and
# http_listen_port. If Loki is running in microservices mode, this is the HTTP
# URL for the Distributor. Path to the push API needs to be included.
# Example: http://example.com:3100/loki/api/v1/push
url: <string>

# Custom HTTP headers to be sent along with each push request.
# Be aware that headers that are set by Promtail itself (e.g. X-Scope-OrgID) can't be overwritten.
headers:
  # Example: CF-Access-Client-Id: xxx
  [ <labelname>: <labelvalue> ... ]

# The tenant ID used by default to push logs to Loki. If omitted or empty
# it assumes Loki is running in single-tenant mode and no X-Scope-OrgID header
# is sent.
[tenant_id: <string>]

# Maximum amount of time to wait before sending a batch, even if that
# batch isn't full.
[batchwait: <duration> | default = 1s]

# Maximum batch size (in bytes) of logs to accumulate before sending
# the batch to Loki.
[batchsize: <int> | default = 1048576]

# If using basic auth, configures the username and password
# sent.
basic_auth:
  # The username to use for basic auth
  [username: <string>]

  # The password to use for basic auth
  [password: <string>]

  # The file containing the password for basic auth
  [password_file: <filename>]

# Optional OAuth 2.0 configuration
# Cannot be used at the same time as basic_auth or authorization
oauth2:
  # Client id and secret for oauth2
  [client_id: <string>]
  [client_secret: <secret>]

  # Read the client secret from a file
  # It is mutually exclusive with `client_secret`
  [client_secret_file: <filename>]

  # Optional scopes for the token request
  scopes:
    [ - <string> ... ]

  # The URL to fetch the token from
  token_url: <string>

  # Optional parameters to append to the token URL
  endpoint_params:
    [ <string>: <string> ... ]

# Bearer token to send to the server.
[bearer_token: <secret>]

# File containing bearer token to send to the server.
[bearer_token_file: <filename>]

# HTTP proxy server to use to connect to the server.
[proxy_url: <string>]

# If connecting to a TLS server, configures how the TLS
# authentication handshake will operate.
tls_config:
  # The CA file to use to verify the server
  [ca_file: <string>]

  # The cert file to send to the server for client auth
  [cert_file: <filename>]

  # The key file to send to the server for client auth
  [key_file: <filename>]

  # Validates that the server name in the server's certificate
  # is this value.
  [server_name: <string>]

  # If true, ignores the server certificate being signed by an
  # unknown CA.
  [insecure_skip_verify: <boolean> | default = false]

# Configures how to retry requests to Loki when a request
# fails.
# Default backoff schedule:
# 0.5s, 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s(4.267m)
# For a total time of 511.5s(8.5m) before logs are lost
backoff_config:
  # Initial backoff time between retries
  [min_period: <duration> | default = 500ms]

  # Maximum backoff time between retries
  [max_period: <duration> | default = 5m]

  # Maximum number of retries to do
  [max_retries: <int> | default = 10]

# Disable retries of batches that Loki responds to with a 429 status code (TooManyRequests). This reduces
# impacts on batches from other tenants, which could end up being delayed or dropped due to exponential backoff.
[drop_rate_limited_batches: <boolean> | default = false]

# Static labels to add to all logs being sent to Loki.
# Use map like {"foo": "bar"} to add a label foo with
# value bar.
# These can also be specified from command line:
# -client.external-labels=k1=v1,k2=v2
# (or --client.external-labels depending on your OS)
# labels supplied by the command line are applied
# to all clients configured in the `clients` section.
# NOTE: values defined in the config file will replace values
# defined on the command line for a given client if the
# label keys are the same.
external_labels:
  [ <labelname>: <labelvalue> ... ]

# Maximum time to wait for a server to respond to a request
[timeout: <duration> | default = 10s]

positions

positions 块配置 Promtail 保存一个文件以指示其已读取文件的进度。这在 Promtail 重启时是必需的,以便它可以从上次停止的地方继续。

yaml
# Location of positions file
[filename: <string> | default = "/var/log/positions.yaml"]

# How often to update the positions file
[sync_period: <duration> | default = 10s]

# Whether to ignore & later overwrite positions files that are corrupted
[ignore_invalid_yaml: <boolean> | default = false]

scrape_configs

scrape_configs 块配置 Promtail 如何使用指定的发现方法从一系列目标抓取日志。Promtail 使用与  Prometheus scrape_configs 相同的配置。这意味着如果您已经拥有一个 Prometheus 实例,则配置将非常相似。

yaml
# Name to identify this scrape config in the Promtail UI.
job_name: <string>

# Describes how to transform logs from targets.
[pipeline_stages: <pipeline_stages>]

# Defines decompression behavior for the given scrape target.
decompression:
  # Whether decompression should be tried or not.
  [enabled: <boolean> | default = false]

  # Initial delay to wait before starting the decompression.
  # Especially useful in scenarios where compressed files are found before the compression is finished.
  [initial_delay: <duration> | default = 0s]

  # Compression format. Supported formats are: 'gz', 'bz2' and 'z.
  [format: <string> | default = ""]

# Describes how to scrape logs from the journal.
[journal: <journal_config>]

# Describes from which encoding a scraped file should be converted.
[encoding: <iana_encoding_name>]

# Describes how to receive logs from syslog.
[syslog: <syslog_config>]

# Describes how to receive logs via the Loki push API, (e.g. from other Promtails or the Docker Logging Driver)
[loki_push_api: <loki_push_api_config>]

# Describes how to scrape logs from the Windows event logs.
[windows_events: <windows_events_config>]

# Configuration describing how to pull/receive Google Cloud Platform (GCP) logs.
[gcplog: <gcplog_config>]

# Configuration describing how to get Azure Event Hubs messages.
[azure_event_hub: <azure_event_hub_config>]

# Describes how to fetch logs from Kafka via a Consumer group.
[kafka: <kafka_config>]

# Describes how to receive logs from gelf client.
[gelf: <gelf_config>]

# Configuration describing how to pull logs from Cloudflare.
[cloudflare: <cloudflare>]

# Configuration describing how to pull logs from a Heroku LogPlex drain.
[heroku_drain: <heroku_drain>]

# Describes how to relabel targets to determine if they should
# be processed.
relabel_configs:
  - [<relabel_config>]

# Static targets to scrape.
static_configs:
  - [<static_config>]

# Files containing targets to scrape.
file_sd_configs:
  - [<file_sd_configs>]

# Describes how to discover Kubernetes services running on the
# same host.
kubernetes_sd_configs:
  - [<kubernetes_sd_config>]

# Describes how to use the Consul Catalog API to discover services registered with the
# consul cluster.
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# Describes how to use the Consul Agent API to discover services registered with the consul agent
# running on the same host as Promtail.
consulagent_sd_configs:
  [ - <consulagent_sd_config> ... ]

# Describes how to use the Docker daemon API to discover containers running on
# the same host as Promtail.
docker_sd_configs:
  [ - <docker_sd_config> ... ]

pipeline_stages

Pipeline Stages 用于转换日志条目及其标签。Pipeline 在发现过程完成后执行。pipeline_stages 对象由一系列阶段组成,这些阶段对应于下面列出的项目。

在大多数情况下,您使用 regexjson Stages 从日志中提取数据。提取的数据被转换为一个临时 map 对象。然后这些数据可以被 Promtail 使用,例如作为 labels 的值或作为 output。此外,除了 dockercri 之外的任何其他 Stage 都可以访问提取的数据。

yaml
- [
    <docker> |
    <cri> |
    <regex> |
    <json> |
    <template> |
    <match> |
    <timestamp> |
    <output> |
    <labels> |
    <metrics> |
    <tenant> |
    <replace>
  ]

docker

Docker Stage 解析 Docker 容器的日志内容,并通过名称定义,带有一个空对象。

yaml
docker: {}

Docker Stage 将匹配并解析以下格式的日志行

nohighlight
`{"log":"level=info ts=2019-04-30T02:12:41.844179Z caller=filetargetmanager.go:180 msg=\"Adding target\"\n","stream":"stderr","time":"2019-04-30T02:12:41.8443515Z"}`

自动将 time 提取到日志时间戳,将 stream 提取到标签,并将 log 字段提取到输出中,这非常有用,因为 docker 以这种方式封装了您的应用程序日志,而这个 Stage 会将其解开,以便对日志内容进行进一步的 pipeline 处理。

Docker Stage 只是以下定义的便捷包装器

yaml
- json:
    expressions:
      output: log
      stream: stream
      timestamp: time
- labels:
    stream:
- timestamp:
    source: timestamp
    format: RFC3339Nano
- output:
    source: output

cri

CRI Stage 解析 CRI 容器的日志内容,并通过名称定义,带有一个空对象。

yaml
cri: {}

CRI Stage 将匹配并解析以下格式的日志行

nohighlight
2019-01-01T01:00:00.000000001Z stderr P some log message

自动将 time 提取到日志时间戳,将 stream 提取到标签,并将剩余消息提取到输出中,这非常有用,因为 CRI 以这种方式封装了您的应用程序日志,而这个 Stage 会将其解开,以便对日志内容进行进一步的 pipeline 处理。

CRI Stage 只是以下定义的便捷包装器

yaml
- regex:
    expression: "^(?s)(?P<time>\\S+?) (?P<stream>stdout|stderr) (?P<flags>\\S+?) (?P<content>.*)$"
- labels:
    stream:
- timestamp:
    source: time
    format: RFC3339Nano
- output:
    source: content

regex

Regex Stage 接受一个正则表达式,并提取捕获的命名组供后续 Stages 使用。

yaml
regex:
  # The RE2 regular expression. Each capture group must be named.
  expression: <string>

  # Name from extracted data to parse. If empty, uses the log message.
  [source: <string>]

json

JSON Stage 将日志行解析为 JSON,并使用 JMESPath 表达式从 JSON 中提取数据供后续 Stages 使用。

yaml
json:
  # Set of key/value pairs of JMESPath expressions. The key will be
  # the key in the extracted data while the expression will be the value,
  # evaluated as a JMESPath from the source data.
  expressions:
    [ <string>: <string> ... ]

  # Name from extracted data to parse. If empty, uses the log message.
  [source: <string>]

template

Template Stage 使用 Go 的 text/template 语言来操作值。

yaml
template:
  # Name from extracted data to parse. If key in extract data doesn't exist, an
  # entry for it will be created.
  source: <string>

  # Go template string to use. In additional to normal template
  # functions, ToLower, ToUpper, Replace, Trim, TrimLeft, TrimRight,
  # TrimPrefix, TrimSuffix, and TrimSpace are available as functions.
  template: <string>

示例

yaml
template:
  source: level
  template: '{{ if eq .Value "WARN" }}{{ Replace .Value "WARN" "OK" -1 }}{{ else }}{{ .Value }}{{ end }}'

match

Match Stage 在日志条目匹配可配置的 LogQL 流选择器时有条件地执行一组 Stages。

yaml
match:
  # LogQL stream selector.
  selector: <string>

  # Names the pipeline. When defined, creates an additional label in
  # the pipeline_duration_seconds histogram, where the value is
  # concatenated with job_name using an underscore.
  [pipeline_name: <string>]

  # Nested set of pipeline stages only if the selector
  # matches the labels of the log entries:
  stages:
    - [
        <docker> |
        <cri> |
        <regex>
        <json> |
        <template> |
        <match> |
        <timestamp> |
        <output> |
        <labels> |
        <metrics>
      ]

timestamp

Timestamp Stage 从提取的 map 中解析数据,并覆盖 Loki 存储的日志的最终时间值。如果不存在此 Stage,Promtail 会将日志条目的时间戳与其读取日志条目的时间相关联。

yaml
timestamp:
  # Name from extracted data to use for the timestamp.
  source: <string>

  # Determines how to parse the time string. Can use
  # pre-defined formats by name: [ANSIC UnixDate RubyDate RFC822
  # RFC822Z RFC850 RFC1123 RFC1123Z RFC3339 RFC3339Nano Unix
  # UnixMs UnixUs UnixNs].
  format: <string>

  # IANA Timezone Database string.
  [location: <string>]

output

Output Stage 从提取的 map 中获取数据,并设置将由 Loki 存储的日志条目的内容。

yaml
output:
  # Name from extracted data to use for the log entry.
  source: <string>

labels

Labels Stage 从提取的 map 中获取数据,并为发送到 Loki 的日志条目设置额外的标签。

yaml
labels:
  # Key is REQUIRED and the name for the label that will be created.
  # Value is optional and will be the name from extracted data whose value
  # will be used for the value of the label. If empty, the value will be
  # inferred to be the same as the key.
  [ <string>: [<string>] ... ]

metrics

Metrics Stage 允许从提取的数据中定义指标。

创建的指标不会推送到 Loki,而是通过 Promtail 的 /metrics 端点暴露。应该配置 Prometheus 来抓取 Promtail,以便能够检索此 Stage 配置的指标。如果 Promtail 的配置被重载,所有指标将被重置。

yaml
# A map where the key is the name of the metric and the value is a specific
# metric type.
metrics:
  [<string>: [ <counter> | <gauge> | <histogram> ] ...]
counter

定义一个值只增不减的 counter 指标。

yaml
# The metric type. Must be Counter.
type: Counter

# Describes the metric.
[description: <string>]

# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <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 "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>
gauge

定义一个值可增可减的 gauge 指标。

yaml
# The metric type. Must be Gauge.
type: Gauge

# Describes the metric.
[description: <string>]

# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <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>
histogram

定义一个值被分桶的 histogram 指标。

yaml
# The metric type. Must be Histogram.
type: Histogram

# Describes the metric.
[description: <string>]

# Key from the extracted data map to use for the metric,
# defaulting to the metric's name if not present.
[source: <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 "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>

  # Holds all the numbers in which to bucket the metric.
  buckets:
    - <int>

tenant

Tenant Stage 是一个 action Stage,它从提取的数据 map 中的字段设置日志条目的租户 ID。

yaml
tenant:
  # Either label, source or value config option is required, but not all (they
  # are mutually exclusive).

  # Name from labels to whose value should be set as tenant ID.
  [ label: <string> ]

  # Name from extracted data to whose value should be set as tenant ID.
  [ source: <string> ]

  # Value to use to set the tenant ID when this stage is executed. Useful
  # when this stage is included within a conditional pipeline with "match".
  [ value: <string> ]

replace

Replace Stage 是一个 parsing Stage,它使用正则表达式解析日志行并替换日志行。

yaml
replace:
  # The RE2 regular expression. Each named capture group will be added to extracted.
  # Each capture group and named capture group will be replaced with the value given in
  # `replace`
  expression: <string>

  # Name from extracted data to parse. If empty, uses the log message.
  # The replaced value will be assigned back to soure key
  [source: <string>]

  # Value to which the captured group will be replaced. The captured group or the named
  # captured group will be replaced with this value and the log line will be replaced with
  # new replaced values. An empty value will remove the captured group from the log line.
  [replace: <string>]

journal

journal 块配置 Promtail 从 systemd journal 读取。需要 Promtail 的构建版本启用 journal 支持。如果使用 AMD64 Docker 镜像,则默认启用。在某些系统上,Promtail 用户需要权限才能访问 journal 日志。对于 Ubuntu (24.04),您需要使用 sudo usermod -a -G systemd-journal promtail 命令将 promtail 添加到 systemd-journal 组。

yaml
# When true, log messages from the journal are passed through the
# pipeline as a JSON message with all of the journal entries' original
# fields. When false, the log message is the text content of the MESSAGE
# field from the journal entry.
[json: <boolean> | default = false]

# The oldest relative time from process start that will be read
# and sent to Loki.
[max_age: <duration> | default = 7h]

# Label map to add to every log coming out of the journal
labels:
  [ <labelname>: <labelvalue> ... ]

# Get labels from journal, when it is not empty
relabel_configs:
- source_labels: ['__journal__hostname']
  target_label: host
- source_labels: ['__journal__systemd_unit']
  target_label: systemd_unit
  regex: '(.+)'
- source_labels: ['__journal__systemd_user_unit']
  target_label: systemd_user_unit
  regex: '(.+)'
- source_labels: ['__journal__transport']
  target_label: transport
  regex: '(.+)'
- source_labels: ['__journal_priority_keyword']
  target_label: severity
  regex: '(.+)'

# Path to a directory to read entries from. Defaults to system
# paths (/var/log/journal and /run/log/journal) when empty.
[path: <string>]

可用标签

标签从 systemd journal 字段导入。标签名称是字段名称转换为小写并带有 _journal 前缀。有关更多信息,请参阅手册页 systemd.journal-fields

例如

journal 字段标签
_HOSTNAME__journal__hostname
_SYSTEMD_UNIT__journal__systemd_unit
_SYSTEMD_USER_UNIT__journal__systemd_user_unit
ERRNO__journal_errno

除了 __journal_priority(从 PRIORITY journal 字段导入,值为 0 到 7 的整数)外,promtail 添加了 __journal_priority_keyword 标签,其值使用 makeJournalPriority 映射函数生成。

journal 优先级关键字
0emerg
1alert
2crit
3error
4warning
5notice
6info
7debug

syslog

syslog 块配置 syslog 监听器,允许用户使用 syslog 协议将日志推送到 Promtail。目前支持 BSD syslog 协议IETF Syslog (RFC5424),支持或不支持八位字节计数。

建议的部署方式是在 Promtail 前面有一个专用的 syslog 转发器,如 syslog-ngrsyslog。转发器可以处理存在的各种规范和传输方式(UDP、BSD syslog 等)。

推荐使用 八位字节计数 作为消息帧方法。在采用 非透明帧 的流中,Promtail 需要等待下一条消息才能捕获多行消息,因此消息之间可能会出现延迟。

请参阅 syslog-ngrsyslog 的推荐输出配置。这两种配置都启用了带八位字节计数的 IETF Syslog。

如果连接了许多客户端,您可能需要增加 Promtail 进程的打开文件限制。(ulimit -Sn)

yaml
# TCP address to listen on. Has the format of "host:port".
listen_address: <string>

# Configure the receiver to use TLS.
tls_config:
  # Certificate and key files sent by the server (required)
  cert_file: <string>
  key_file: <string>

  # CA certificate used to validate client certificate. Enables client certificate verification when specified.
  [ ca_file: <string> ]

# The idle timeout for tcp syslog connections, default is 120 seconds.
idle_timeout: <duration>

# Whether to convert syslog structured data to labels.
# A structured data entry of [example@99999 test="yes"] would become
# the label "__syslog_message_sd_example_99999_test" with the value "yes".
label_structured_data: <bool>

# Label map to add to every log message.
labels:
  [ <labelname>: <labelvalue> ... ]

# Whether Promtail should pass on the timestamp from the incoming syslog message.
# When false, or if no timestamp is present on the syslog message, Promtail will assign the current timestamp to the log when it was processed.
# Default is false
use_incoming_timestamp: <bool>

# Sets the maximum limit to the length of syslog messages
max_message_length: <int>

# Defines used Sylog format at the target. 
syslog_format:
 [type: <string> | default = "rfc5424"]

# Defines whether the full RFC5424 formatted syslog message should be pushed to Loki
use_rfc5424_message: <bool>

可用标签

  • __syslog_connection_ip_address: 远程 IP 地址。
  • __syslog_connection_hostname: 远程主机名。
  • __syslog_message_severity: 从消息中解析的 syslog 级别。符号名称参照 syslog_message.go
  • __syslog_message_facility: 从消息中解析的 syslog facility。符号名称参照 syslog_message.gosyslog(3)
  • __syslog_message_hostname: 从消息中解析的 hostname
  • __syslog_message_app_name: 从消息中解析的 app-name 字段
  • __syslog_message_proc_id: 从消息中解析的 procid 字段
  • __syslog_message_msg_id: 从消息中解析的 msgid 字段
  • __syslog_message_sd_<sd_id>[_<iana_enterprise_id>]_<sd_name>: 从消息中解析的 structured-data 字段。数据字段 [custom@99770 example="1"] 会变成 __syslog_message_sd_custom_99770_example

loki_push_api

loki_push_api 块配置 Promtail 暴露 Loki 推送 API 服务器。

每个配置了 loki_push_api 的作业都会暴露此 API,并需要单独的端口。

注意 server 配置与 server 相同。

Promtail 还在 /promtail/api/v1/raw 上暴露了第二个端点,该端点接受换行符分隔的日志行。这可用于发送 NDJSON 或纯文本日志。

loki_push_api 服务器的就绪状态可以使用 /ready 端点检查。

yaml
# The push server configuration options
[server: <server_config>]

# Label map to add to every log line sent to the push API
labels:
  [ <labelname>: <labelvalue> ... ]

# If Promtail should pass on the timestamp from the incoming log or not.
# When false Promtail will assign the current timestamp to the log when it was processed.
# Does not apply to the plaintext endpoint on `/promtail/api/v1/raw`.
[use_incoming_timestamp: <bool> | default = false]

请参阅 Push 配置示例

windows_events

windows_events 块配置 Promtail 抓取 Windows 事件日志并将其发送到 Loki。

要订阅特定的事件流,您需要提供 eventlog_namexpath_query

事件默认每 3 秒定期抓取,但可以使用 poll_interval 更改。

bookmark path bookmark_path 是强制性的,将用作 Promtail 记录最后处理的事件的位置文件。此文件在 Promtail 重启后仍然存在。

如果您想保留传入事件的时间戳,可以设置 use_incoming_timestamp。默认情况下,Promtail 将使用从事件日志读取事件时的时间戳。

Promtail 会将 Windows 事件序列化为 JSON,并从收到的事件中添加 channelcomputer 标签。您可以使用 labels 属性添加额外的标签。

yaml
# LCID (Locale ID) for event rendering
# - 1033 to force English language
# -  0 to use default Windows locale
[locale: <int> | default = 0]

# Name of eventlog, used only if xpath_query is empty
# Example: "Application"
[eventlog_name: <string> | default = ""]

# xpath_query can be in defined short form like "Event/System[EventID=999]"
# or you can form a XML Query. Refer to the Consuming Events article:
# https://docs.microsoft.com/en-us/windows/win32/wes/consuming-events
# XML query is the recommended form, because it is most flexible
# You can create or debug XML Query by creating Custom View in Windows Event Viewer
# and then copying resulting XML here
[xpath_query: <string> | default = "*"]

# Sets the bookmark location on the filesystem.
# The bookmark contains the current position of the target in XML.
# When restarting or rolling out Promtail, the target will continue to scrape events where it left off based on the bookmark position.
# The position is updated after each entry processed.
[bookmark_path: <string> | default = ""]

# PollInterval is the interval at which we're looking if new events are available. By default the target will check every 3seconds.
[poll_interval: <duration> | default = 3s]

# Allows to exclude the xml event data.
[exclude_event_data: <bool> | default = false]

# Allows to exclude the human-friendly event message.
[exclude_event_message: <bool> | default = false]

# Allows to exclude the user data of each windows event.
[exclude_user_data: <bool> | default = false]

# Label map to add to every log line read from the windows event log
labels:
  [ <labelname>: <labelvalue> ... ]

# If Promtail should pass on the timestamp from the incoming log or not.
# When false Promtail will assign the current timestamp to the log when it was processed
[use_incoming_timestamp: <bool> | default = false]

GCP 日志

gcplog 块配置 Promtail 如何接收 GCP 日志。根据 subscription_type 的配置,有两种策略:

  • Pull:使用 GCP Pub/Sub 拉取订阅。Promtail 将直接从配置的 GCP Pub/Sub topic 消费日志消息。
  • Push:使用 GCP Pub/Sub 推送订阅。Promtail 将暴露一个 HTTP 服务器,GCP 将日志发送到该服务器。

使用 push 订阅类型时,请注意

  • server 配置与 server 相同,因为 Promtail 会为需要暴露 HTTP 服务器的目标暴露一个 HTTP 服务器。
  • 一个端点 POST /gcp/api/v1/push,它接受来自 GCP PubSub 消息传递系统的请求。
yaml
# Type of subscription used to fetch logs from GCP. Can be either `pull` (default) or `push`.
[subscription_type: <string> | default = "pull"]

# If the subscription_type is pull,  the GCP project ID
[project_id: <string>]

# If the subscription_type is pull, GCP PubSub subscription from where Promtail will pull logs from
[subscription: <string>]

# If the subscription_type is push, the server configuration options
[server: <server_config>]

# Whether Promtail should pass on the timestamp from the incoming GCP Log message.
# When false, or if no timestamp is present in the GCP Log message, Promtail will assign the current
# timestamp to the log when it was processed.
[use_incoming_timestamp: <boolean> | default = false]

# use_full_line to force Promtail to send the full line from Cloud Logging even if `textPayload` is available.
# By default, if `textPayload` is present in the line, then it's used as log line.
[use_full_line: <boolean> | default = false]

# If the subscription_type is push, configures an HTTP handler timeout. If processing the incoming GCP Logs request takes longer
# than the configured duration, that is processing and then sending the entry down the processing pipeline, the server will abort
# and respond with a 503 HTTP status code.
[push_timeout: <duration>|  default = 0 (no timeout)]

# Label map to add to every log message.
labels:
  [ <labelname>: <labelvalue> ... ]

可用标签

Promtail 接收 GCP 日志时,会提供各种内部标签用于 重新打标签。这取决于选择的订阅类型。

Pull 可用的内部标签

  • __gcp_logname
  • __gcp_severity
  • __gcp_resource_type
  • __gcp_resource_labels_<NAME>
  • __gcp_labels_<NAME>

Push 可用的内部标签

  • __gcp_message_id
  • __gcp_subscription_name
  • __gcp_attributes_<NAME>: 从传入 push 消息的 .message.attributes 中读取的所有属性。每个属性键都被方便地重命名,因为它可能包含不支持的字符。例如,logging.googleapis.com/timestamp 会被转换为 __gcp_attributes_logging_googleapis_com_timestamp
  • __gcp_logname
  • __gcp_severity
  • __gcp_resource_type
  • __gcp_resource_labels_<NAME>
  • __gcp_labels_<NAME>

Azure Event Hubs

azure_event_hubs 块配置 Promtail 如何接收 Azure Event Hubs 消息。Promtail 使用 Event Hubs 上的 Apache Kafka 端点接收消息。有关更多信息,请参阅 Azure Event Hubs 文档

要了解更多关于将 Azure 日志流式传输到 Azure Event Hubs 的信息,您可以查看此教程

请注意,Apache Kafka 端点在 Basic 定价计划中不可用。有关更多信息,请参阅 Event Hubs 定价页面

yaml
# Event Hubs namespace host names (Required). Typically, it looks like <your-namespace>.servicebus.windows.net:9093.
fully_qualified_namespace: <string> | default = ""

# Event Hubs to consume (Required).
event_hubs:
    [ - <string> ... ]

# Event Hubs ConnectionString for authentication on Azure Cloud (Required).
connection_string: <string> | default = "range"

# The consumer group id.
[group_id: <string> | default = "promtail"]

# If Promtail should pass on the timestamp from the incoming message or not.
# When false Promtail will assign the current timestamp to the log when it was processed.
[use_incoming_timestamp: <bool> | default = false]

# If Promtail should ignore messages that don't match the schema for Azure resource logs.
# Schema is described here https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/resource-logs-schema.
[disallow_custom_messages: <bool> | default = false]

# Labels optionally hold labels to associate with each log line.
[labels]:
  [ <labelname>: <labelvalue> ... ]

可用标签

Promtail 接收 Azure Event Hubs 消息时,会提供各种内部标签用于 重新打标签

  • __azure_event_hubs_category: 当消息是应用程序日志时,消息的日志类别。

以下标签列表是使用 Event Hubs 中的 Kafka 端点发现的。

  • __meta_kafka_topic: 读取消息的当前 topic。
  • __meta_kafka_partition: 读取消息的 partition ID。
  • __meta_kafka_member_id: 消费者组成员 ID。
  • __meta_kafka_group_id: 消费者组 ID。
  • __meta_kafka_message_key: 消息键。如果为空,此值将是 'none'。

kafka

kafka 块配置 Promtail 使用 group consumer 从 Kafka 抓取日志。

brokers 应列出可用于与 Kafka 集群通信的 broker。当您想要提高可用性时,请使用多个 broker。

topics 是 Promtail 将订阅的 topic 列表。如果 topic 以 ^ 开头,则使用正则表达式 (RE2) 来匹配 topic。例如,^promtail-.* 将匹配 topic promtail-devpromtail-prod。Topics 每 30 秒刷新一次,因此如果有新的 topic 匹配,它将自动添加,无需重启 Promtail。

group_id 定义了用于消费日志的唯一消费者组 ID。发布到 topic 的每条日志记录都会发送到每个订阅消费者组中的一个消费者实例。

  • 如果所有 promtail 实例具有相同的消费者组,则记录将有效地在这些 promtail 实例之间负载均衡。
  • 如果所有 promtail 实例具有不同的消费者组,则每条记录将广播到所有 promtail 实例。

如果您希望将数据有效地发送到多个 Loki 实例和/或其他 sink,group_id 会很有用。

assignor 配置允许您为消费者组选择要使用的 rebalancing 策略。Rebalancing 是一个过程,其中一组消费者实例(属于同一组)协调以拥有该组订阅的 topic 的互斥 partition 集合。

  • range 默认值,将 partition 作为范围分配给消费者组成员。
  • sticky 将 partition 分配给成员,并尝试保留先前的分配
  • roundrobin 以交替顺序将 partition 分配给成员。

version 允许选择连接集群所需的 kafka 版本(默认为 2.2.1)。

默认情况下,时间戳由 Promtail 在读取消息时分配,如果您想保留 Kafka 实际的消息时间戳,可以将 use_incoming_timestamp 设置为 true。

yaml
# The list of brokers to connect to kafka (Required).
[brokers: <strings> | default = [""]]

# The list of Kafka topics to consume (Required).
[topics: <strings> | default = [""]]

# The Kafka consumer group id.
[group_id: <string> | default = "promtail"]

# The consumer group rebalancing strategy to use. (e.g `sticky`, `roundrobin` or `range`)
[assignor: <string> | default = "range"]

# Kafka version to connect to.
[version: <string> | default = "2.2.1"]

# Optional authentication configuration with Kafka brokers
authentication:
  # Type is authentication type. Supported values [none, ssl, sasl]
  [type: <string> | default = "none"]

  # TLS configuration for authentication and encryption. It is used only when authentication type is ssl.
  tls_config:
    [ <tls_config> ]

  # SASL configuration for authentication. It is used only when authentication type is sasl.
  sasl_config:
    # SASL mechanism. Supported values [PLAIN, SCRAM-SHA-256, SCRAM-SHA-512]
    [mechanism: <string> | default = "PLAIN"]

    # The user name to use for SASL authentication
    [user: <string>]

    # The password to use for SASL authentication
    [password: <secret>]

    # If true, SASL authentication is executed over TLS
    [use_tls: <boolean> | default = false]

    # The CA file to use to verify the server
    [ca_file: <string>]

    # Validates that the server name in the server's certificate
    # is this value.
    [server_name: <string>]

    # If true, ignores the server certificate being signed by an
    # unknown CA.
    [insecure_skip_verify: <boolean> | default = false]


# Label map to add to every log line read from kafka
labels:
  [ <labelname>: <labelvalue> ... ]

# If Promtail should pass on the timestamp from the incoming log or not.
# When false Promtail will assign the current timestamp to the log when it was processed
[use_incoming_timestamp: <bool> | default = false]

可用标签

以下是消费 kafka 时发现的标签列表

  • __meta_kafka_topic: 读取消息的当前 topic。
  • __meta_kafka_partition: 读取消息的 partition ID。
  • __meta_kafka_member_id: 消费者组成员 ID。
  • __meta_kafka_group_id: 消费者组 ID。
  • __meta_kafka_message_key: 消息键。如果为空,此值将是 'none'。

要将发现的标签保留到您的日志中,请使用 relabel_configs 部分。

GELF

gelf 块配置一个 GELF UDP 监听器,允许用户使用 GELF 协议将日志推送到 Promtail。目前仅支持 UDP,如果您对 TCP 支持感兴趣,请提交功能请求。

GELF 消息可以发送未压缩的或使用 GZIP 或 ZLIB 压缩的。

收到的每条 GELF 消息都将以 JSON 格式编码为日志行。例如

json
{"version":"1.1","host":"example.org","short_message":"A short message","timestamp":1231231123,"level":5,"_some_extra":"extra"}

您可以使用 pipeline stages 处理 GELF 目标,例如,如果您想解析日志行并提取更多标签或更改日志行格式。

yaml
# UDP address to listen on. Has the format of "host:port". Default to 0.0.0.0:12201
listen_address: <string>

# Label map to add to every log message.
labels:
  [ <labelname>: <labelvalue> ... ]

# Whether Promtail should pass on the timestamp from the incoming gelf message.
# When false, or if no timestamp is present on the gelf message, Promtail will assign the current timestamp to the log when it was processed.
# Default is false
use_incoming_timestamp: <bool>

可用标签

  • __gelf_message_level: GELF 级别(字符串格式)。
  • __gelf_message_host: 发送 GELF 消息的主机。
  • __gelf_message_version: 客户端设置的 GELF 消息版本。
  • __gelf_message_facility: GELF facility。

要将发现的标签保留到您的日志中,请使用 relabel_configs 部分。

Cloudflare

cloudflare 块配置 Promtail 从 Cloudflare Logpull API 拉取日志。

这些日志包含与连接客户端、通过 Cloudflare 网络的请求路径以及源 Web 服务器的响应相关的数据。这些数据有助于丰富源服务器上的现有日志。

yaml
# The Cloudflare API token to use. (Required)
# You can create a new token by visiting your [Cloudflare profile](https://dash.cloudflare.com/profile/api-tokens).
api_token: <string>

# The Cloudflare zone id to pull logs for. (Required)
zone_id: <string>

# The time range to pull logs for.
[pull_range: <duration> | default = 1m]

# The quantity of workers that will pull logs.
[workers: <int> | default = 3]

# The type list of fields to fetch for logs.
# Supported values: default, minimal, extended, all.
[fields_type: <string> | default = default]

# The additional list of fields to supplement those provided via `fields_type`.
[additional_fields: <string> ... ]

# Label map to add to every log message.
labels:
  [ <labelname>: <labelvalue> ... ]

默认情况下,Promtail 获取包含默认字段集的日志。以下是可用的不同字段集类型及其包含的字段

  • default 包括 "ClientIP", "ClientRequestHost", "ClientRequestMethod", "ClientRequestURI", "EdgeEndTimestamp", "EdgeResponseBytes", "EdgeRequestHost", "EdgeResponseStatus", "EdgeStartTimestamp", "RayID",以及通过 additional_fields 参数提供的任何额外字段。

  • minimal 包括所有 default 字段,并添加 "ZoneID", "ClientSSLProtocol", "ClientRequestProtocol", "ClientRequestPath", "ClientRequestUserAgent", "ClientRequestReferer", "EdgeColoCode", "ClientCountry", "CacheCacheStatus", "CacheResponseStatus", "EdgeResponseContentType",以及通过 additional_fields 参数提供的任何额外字段。

  • extended 包括所有 minimal 字段,并添加 "ClientSSLCipher", "ClientASN", "ClientIPClass", "CacheResponseBytes", "EdgePathingOp", "EdgePathingSrc", "EdgePathingStatus", "ParentRayID", "WorkerCPUTime", "WorkerStatus", "WorkerSubrequest", "WorkerSubrequestCount", "OriginIP", "OriginResponseStatus", "OriginSSLProtocol", "OriginResponseHTTPExpires", "OriginResponseHTTPLastModified",以及通过 additional_fields 参数提供的任何额外字段。

  • all 包括所有 extended 字段,并添加 "BotScore", "BotScoreSrc", "BotTags", "ClientRequestBytes", "ClientSrcPort", "ClientXRequestedWith", "CacheTieredFill", "EdgeResponseCompressionRatio", "EdgeServerIP", "FirewallMatchesSources", "FirewallMatchesActions", "FirewallMatchesRuleIDs", "OriginResponseBytes", "OriginResponseTime", "ClientDeviceType", "WAFFlags", "WAFMatchedVar", "EdgeColoID", "RequestHeaders", "ResponseHeaders", "ClientRequestSource",以及通过 additional_fields 参数提供的任何额外字段(当 Cloudflare API 提供新字段但尚未包含在 all 中时,此参数仍然相关)。

  • custom 只包括在 additional_fields 中定义的字段。

要了解每个字段及其值的更多信息,请参阅 Cloudflare 文档

Promtail 将上次成功获取的时间戳保存在位置文件中。如果在文件中找到给定 zone ID 的位置,Promtail 将从该位置重新开始拉取日志。如果未找到位置,Promtail 将从当前时间开始拉取日志。

Promtail 使用多个 worker(可通过 workers 配置)获取日志,这些 worker 重复请求最后一个可用的拉取范围(可通过 pull_range 配置)。使用 cloudflare_target_last_requested_end_timestamp 指标验证 Promtail 获取的最后一个时间戳。Promtail 可能会因为每次拉取需要处理的日志行过多而落后。增加 worker 数量、减小拉取范围或减少获取的字段数量可以缓解此性能问题。

所有 Cloudflare 日志都是 JSON 格式。以下是一个示例

json
{
	"CacheCacheStatus": "miss",
	"CacheResponseBytes": 8377,
	"CacheResponseStatus": 200,
	"CacheTieredFill": false,
	"ClientASN": 786,
	"ClientCountry": "gb",
	"ClientDeviceType": "desktop",
	"ClientIP": "100.100.5.5",
	"ClientIPClass": "noRecord",
	"ClientRequestBytes": 2691,
	"ClientRequestHost": "www.foo.com",
	"ClientRequestMethod": "GET",
	"ClientRequestPath": "/comments/foo/",
	"ClientRequestProtocol": "HTTP/1.0",
	"ClientRequestReferer": "https://www.foo.com/foo/168855/?offset=8625",
	"ClientRequestURI": "/foo/15248108/",
	"ClientRequestUserAgent": "some bot",
        "ClientRequestSource": "1"
	"ClientSSLCipher": "ECDHE-ECDSA-AES128-GCM-SHA256",
	"ClientSSLProtocol": "TLSv1.2",
	"ClientSrcPort": 39816,
	"ClientXRequestedWith": "",
	"EdgeColoCode": "MAN",
	"EdgeColoID": 341,
	"EdgeEndTimestamp": 1637336610671000000,
	"EdgePathingOp": "wl",
	"EdgePathingSrc": "macro",
	"EdgePathingStatus": "nr",
	"EdgeRateLimitAction": "",
	"EdgeRateLimitID": 0,
	"EdgeRequestHost": "www.foo.com",
	"EdgeResponseBytes": 14878,
	"EdgeResponseCompressionRatio": 1,
	"EdgeResponseContentType": "text/html",
	"EdgeResponseStatus": 200,
	"EdgeServerIP": "8.8.8.8",
	"EdgeStartTimestamp": 1637336610517000000,
	"FirewallMatchesActions": [],
	"FirewallMatchesRuleIDs": [],
	"FirewallMatchesSources": [],
	"OriginIP": "8.8.8.8",
	"OriginResponseBytes": 0,
	"OriginResponseHTTPExpires": "",
	"OriginResponseHTTPLastModified": "",
	"OriginResponseStatus": 200,
	"OriginResponseTime": 123000000,
	"OriginSSLProtocol": "TLSv1.2",
	"ParentRayID": "00",
	"RayID": "6b0a...",
        "RequestHeaders": [],
        "ResponseHeaders": [
          "x-foo": "bar"
        ],
	"SecurityLevel": "med",
	"WAFAction": "unknown",
	"WAFFlags": "0",
	"WAFMatchedVar": "",
	"WAFProfile": "unknown",
	"WAFRuleID": "",
	"WAFRuleMessage": "",
	"WorkerCPUTime": 0,
	"WorkerStatus": "unknown",
	"WorkerSubrequest": false,
	"WorkerSubrequestCount": 0,
	"ZoneID": 1234
}

您可以使用 pipeline stages,例如,如果您想解析 JSON 日志行并提取更多标签或更改日志行格式。

heroku_drain

heroku_drain 块配置 Promtail 暴露 Heroku HTTPS Drain

每个配置了 Heroku Drain 的作业都会暴露一个 Drain,并需要单独的端口。

server 配置与 server 相同,因为 Promtail 会为每个新的 drain 暴露一个 HTTP 服务器。

Promtail 在 /heroku/api/v1/drain 暴露一个端点,该端点接受来自 Heroku 日志传递的请求。

yaml
# The Heroku drain server configuration options
[server: <server_config>]

# Label map to add to every log message.
labels:
  [ <labelname>: <labelvalue> ... ]

# Whether Promtail should pass on the timestamp from the incoming Heroku drain message.
# When false, or if no timestamp is present in the syslog message, Promtail will assign the current
# timestamp to the log when it was processed.
[use_incoming_timestamp: <boolean> | default = false]

可用标签

Heroku Log drain 以 Syslog 格式发送日志(带有一些 微小修改;它们不完全符合 RFC 标准)。

Heroku Drain 目标为每个日志条目暴露接收到的 syslog 字段,带有以下标签

  • __heroku_drain_host: 从消息中解析的 HOSTNAME 字段。
  • __heroku_drain_app: 从消息中解析的 APP-NAME 字段。
  • __heroku_drain_proc: 从消息中解析的 PROCID 字段。
  • __heroku_drain_log_id: 从消息解析的 MSGID 字段。

此外,Heroku drain 目标将读取配置的 drain 目标 URL 中的所有 URL 查询参数,并将它们作为 __heroku_drain_param_<name> 标签提供,相同参数的多个实例将以逗号分隔的字符串形式出现

relabel_configs

重新标记 (Relabeling) 是一个强大的工具,可以在目标被抓取之前动态地重写其标签集。每个抓取配置可以配置多个重新标记步骤。它们按照在配置文件中出现的顺序应用于每个目标的标签集。

重新标记后,如果 instance 标签在重新标记期间未设置,则默认设置为 __address__ 的值。__scheme____metrics_path__ 标签分别设置为目标的 scheme 和指标路径。__param_<name> 标签设置为第一个传递的名为 <name> 的 URL 参数的值。

在重新标记阶段,可能还会提供以 __meta_ 为前缀的附加标签。它们由提供目标的服务发现机制设置,并且因机制而异。

目标重新标记完成后,以 __ 开头的标签将从标签集中移除。

如果重新标记步骤只需要临时存储标签值(作为后续重新标记步骤的输入),请使用 __tmp 标签名前缀。该前缀保证永远不会被 Prometheus 本身使用。

yaml
# The source labels select values from existing labels. Their content is concatenated
# using the configured separator and matched against the configured regular expression
# for the replace, keep, and drop actions.
[ source_labels: '[' <labelname> [, ...] ']' ]

# Separator placed between concatenated source label values.
[ separator: <string> | default = ; ]

# Label to which the resulting value is written in a replace action.
# It is mandatory for replace actions. Regex capture groups are available.
[ target_label: <labelname> ]

# Regular expression against which the extracted value is matched.
[ regex: <regex> | default = (.*) ]

# Modulus to take of the hash of the source label values.
[ modulus: <uint64> ]

# Replacement value against which a regex replace is performed if the
# regular expression matches. Regex capture groups are available.
[ replacement: <string> | default = $1 ]

# Action to perform based on regex matching.
[ action: <relabel_action> | default = replace ]

<regex> 是任何有效的 RE2 正则表达式。它是 replacekeepdroplabelmaplabeldroplabelkeep 操作所必需的。正则表达式在两端都固定锚定。要取消锚定正则表达式,请使用 .*<regex>.*

<relabel_action> 确定要执行的重新标记操作

  • replace: 将 regex 与串联的 source_labels 匹配。然后,将 target_label 设置为 replacement,其中 replacement 中的匹配组引用 (${1}, ${2}, …) 会被其值替换。如果 regex 不匹配,则不进行替换。
  • keep: 丢弃 regex 与串联的 source_labels 不匹配的目标。
  • drop: 丢弃 regex 与串联的 source_labels 匹配的目标。
  • hashmod: 将 target_label 设置为串联的 source_labels 的哈希值的 modulus
  • labelmap: 将 regex 与所有标签名匹配。然后将匹配标签的值复制到 replacement 给定的标签名,其中 replacement 中的匹配组引用 (${1}, ${2}, …) 会被其值替换。
  • labeldrop: 将 regex 与所有标签名匹配。任何匹配的标签都将从标签集中移除。
  • labelkeep: 将 regex 与所有标签名匹配。任何不匹配的标签都将从标签集中移除。

使用 labeldroplabelkeep 时必须小心,以确保移除标签后日志仍然具有唯一标签。

static_configs

static_configs 允许指定目标列表和它们的通用标签集。它是指定抓取配置中静态目标的规范方式。

yaml
# Configures the discovery to look on the current machine.
# This is required by the prometheus service discovery code but doesn't
# really apply to Promtail which can ONLY look at files on the local machine
# As such it should only have the value of localhost, OR it can be excluded
# entirely and a default value of localhost will be applied by Promtail.
targets:
  - localhost

# Defines a file to scrape and an optional set of additional labels to apply to
# all streams defined by the files from __path__.
labels:
  # The path to load logs from. Can use glob patterns (e.g., /var/log/*.log).
  __path__: <string>

  # Used to exclude files from being loaded. Can also use glob patterns.
  __path_exclude__: <string>

  # Additional labels to assign to the logs
  [ <labelname>: <labelvalue> ... ]

file_sd_config

基于文件的服务发现提供了一种更通用的方式来配置静态目标,并作为插入自定义服务发现机制的接口。

它读取一组包含零个或多个 <static_config> 的文件。通过磁盘监视检测到所有已定义文件的更改并立即应用。文件可以提供 YAML 或 JSON 格式。仅应用导致良好格式目标组的更改。

JSON 文件必须包含静态配置列表,使用以下格式

yaml
[
  {
    "targets": [ "localhost" ],
    "labels": {
      "__path__": "<string>", ...
      "<labelname>": "<labelvalue>", ...
    }
  },
  ...
]

作为备用,文件内容也会按指定的刷新间隔定期重新读取。

重新标记阶段,每个目标都有一个元标签 __meta_filepath。其值设置为提取目标的文件的路径。

yaml
# Patterns for files from which target groups are extracted.
files:
  [ - <filename_pattern> ... ]

# Refresh interval to re-read the files.
[ refresh_interval: <duration> | default = 5m ]

其中 <filename_pattern> 可以是以 .json.yml.yaml 结尾的路径。最后一个路径段可以包含一个匹配任意字符序列的单个 *,例如 my/path/tg_*.json

kubernetes_sd_config

Kubernetes SD 配置允许从 Kubernetes 的 REST API 检索抓取目标,并始终与集群状态保持同步。

可以配置以下 role 类型之一来发现目标

node

node 角色发现每个集群节点一个目标,地址默认为 Kubelet 的 HTTP 端口。

目标地址默认为 Kubernetes 节点对象中按地址类型顺序 NodeInternalIPNodeExternalIPNodeLegacyHostIPNodeHostName 出现的第一个现有地址。

可用元标签

  • __meta_kubernetes_node_name: 节点对象的名称。
  • __meta_kubernetes_node_label_<labelname>: 节点对象中的每个标签。
  • __meta_kubernetes_node_labelpresent_<labelname>: 对于节点对象中的每个标签,值为 true
  • __meta_kubernetes_node_annotation_<annotationname>: 节点对象中的每个注解。
  • __meta_kubernetes_node_annotationpresent_<annotationname>: 对于节点对象中的每个注解,值为 true
  • __meta_kubernetes_node_address_<address_type>: 每种节点地址类型的第一个地址(如果存在)。

此外,节点的 instance 标签将设置为从 API 服务器检索到的节点名称。

service

service 角色发现每个服务的每个服务端口一个目标。这通常对服务的黑盒监控非常有用。地址将设置为服务的 Kubernetes DNS 名称和相应的服务端口。

可用元标签

  • __meta_kubernetes_namespace: 服务对象的命名空间。
  • __meta_kubernetes_service_annotation_<annotationname>: 服务对象中的每个注解。
  • __meta_kubernetes_service_annotationpresent_<annotationname>: 对于服务对象的每个注解,值为 “true”。
  • __meta_kubernetes_service_cluster_ip: 服务的集群 IP 地址。(不适用于类型为 ExternalName 的服务)
  • __meta_kubernetes_service_external_name: 服务的 DNS 名称。(适用于类型为 ExternalName 的服务)
  • __meta_kubernetes_service_label_<labelname>: 服务对象中的每个标签。
  • __meta_kubernetes_service_labelpresent_<labelname>: 对于服务对象的每个标签,值为 true
  • __meta_kubernetes_service_name: 服务对象的名称。
  • __meta_kubernetes_service_port_name: 目标的业务端口名称。
  • __meta_kubernetes_service_port_protocol: 目标的业务端口协议。

pod

pod 角色发现所有 pod 并将其容器公开为目标。对于容器的每个声明端口,都会生成一个目标。如果容器没有指定端口,则为每个容器创建一个无端口目标,以便通过重新标记手动添加端口。

可用元标签

  • __meta_kubernetes_namespace: pod 对象的命名空间。
  • __meta_kubernetes_pod_name: pod 对象的名称。
  • __meta_kubernetes_pod_ip: pod 对象的 pod IP。
  • __meta_kubernetes_pod_label_<labelname>: pod 对象中的每个标签。
  • __meta_kubernetes_pod_labelpresent_<labelname>: 对于 pod 对象中的每个标签,值为 true
  • __meta_kubernetes_pod_annotation_<annotationname>: pod 对象中的每个注解。
  • __meta_kubernetes_pod_annotationpresent_<annotationname>: 对于 pod 对象中的每个注解,值为 true
  • __meta_kubernetes_pod_container_init: 如果容器是 InitContainer,则为 true
  • __meta_kubernetes_pod_container_name: 目标地址指向的容器名称。
  • __meta_kubernetes_pod_container_port_name: 容器端口名称。
  • __meta_kubernetes_pod_container_port_number: 容器端口号。
  • __meta_kubernetes_pod_container_port_protocol: 容器端口协议。
  • __meta_kubernetes_pod_ready: 设置为 truefalse 表示 pod 的就绪状态。
  • __meta_kubernetes_pod_phase: 在 生命周期 中设置为 PendingRunningSucceededFailedUnknown
  • __meta_kubernetes_pod_node_name: pod 被调度到的节点的名称。
  • __meta_kubernetes_pod_host_ip: pod 对象的当前主机 IP。
  • __meta_kubernetes_pod_uid: pod 对象的 UID。
  • __meta_kubernetes_pod_controller_kind: pod 控制器的对象类型。
  • __meta_kubernetes_pod_controller_name: pod 控制器的名称。

endpoints

endpoints 角色从服务的列出端点发现目标。对于每个端点地址,每个端口发现一个目标。如果端点由 pod 支持,则 pod 的所有未绑定到端点端口的附加容器端口也会被发现为目标。

可用元标签

  • __meta_kubernetes_namespace: endpoints 对象的命名空间。
  • __meta_kubernetes_endpoints_name: endpoints 对象的名称。
  • 对于直接从端点列表发现的所有目标(不包含从底层 pod 额外推断出的目标),会附加以下标签:
    • __meta_kubernetes_endpoint_hostname: 端点的主机名。
    • __meta_kubernetes_endpoint_node_name: 端点所在节点的名称。
    • __meta_kubernetes_endpoint_ready: 设置为 truefalse 表示端点的就绪状态。
    • __meta_kubernetes_endpoint_port_name: 端点端口名称。
    • __meta_kubernetes_endpoint_port_protocol: 端点端口协议。
    • __meta_kubernetes_endpoint_address_target_kind: 端点地址目标的类型。
    • __meta_kubernetes_endpoint_address_target_name: 端点地址目标的名称。
  • 如果端点属于服务,则附加 role: service 发现的所有标签。
  • 对于由 pod 支持的所有目标,附加 role: pod 发现的所有标签。

ingress

ingress 角色发现每个 ingress 的每个路径一个目标。这通常对 ingress 的黑盒监控非常有用。地址将设置为 ingress spec 中指定的主机。

可用元标签

  • __meta_kubernetes_namespace: ingress 对象的命名空间。
  • __meta_kubernetes_ingress_name: ingress 对象的名称。
  • __meta_kubernetes_ingress_label_<labelname>: ingress 对象中的每个标签。
  • __meta_kubernetes_ingress_labelpresent_<labelname>: 对于 ingress 对象中的每个标签,值为 true
  • __meta_kubernetes_ingress_annotation_<annotationname>: ingress 对象中的每个注解。
  • __meta_kubernetes_ingress_annotationpresent_<annotationname>: 对于 ingress 对象中的每个注解,值为 true
  • __meta_kubernetes_ingress_scheme: ingress 的协议 scheme,如果设置了 TLS 配置则为 https。默认为 http
  • __meta_kubernetes_ingress_path: ingress spec 中的路径。默认为 /

有关 Kubernetes 发现的配置选项,请参阅下文。

yaml
# The information to access the Kubernetes API.

# The API server addresses. If left empty, Prometheus is assumed to run inside
# of the cluster and will discover API servers automatically and use the pod's
# CA certificate and bearer token file at /var/run/secrets/kubernetes.io/serviceaccount/.
[ api_server: <host> ]

# The Kubernetes role of entities that should be discovered.
role: <role>

# Optional authentication information used to authenticate to the API server.
# Note that `basic_auth`, `bearer_token` and `bearer_token_file` options are
# mutually exclusive.
# password and password_file are mutually exclusive.

# Optional HTTP basic authentication information.
basic_auth:
  [ username: <string> ]
  [ password: <secret> ]
  [ password_file: <string> ]

# Optional bearer token authentication information.
[ bearer_token: <secret> ]

# Optional bearer token file authentication information.
[ bearer_token_file: <filename> ]

# Optional proxy URL.
[ proxy_url: <string> ]

# TLS configuration.
tls_config:
  [ <tls_config> ]

# Optional namespace discovery. If omitted, all namespaces are used.
namespaces:
  names:
    [ - <string> ]

# Optional label and field selectors to limit the discovery process to a subset of available
#  resources. See
# https://kubernetes.ac.cn/docs/concepts/overview/working-with-objects/field-selectors/
# and https://kubernetes.ac.cn/docs/concepts/overview/working-with-objects/labels/ to learn
# more about the possible filters that can be used. The endpoints role supports pod,
# service, and endpoint selectors. Roles only support selectors matching the role itself;
# for example, the node role can only contain node selectors.
# Note: When making decisions about using field/label selectors, make sure that this
# is the best approach. It will prevent Promtail from reusing single list/watch
# for all scrape configurations. This might result in a bigger load on the Kubernetes API,
# because for each selector combination, there will be additional LIST/WATCH.
# On the other hand, if you want to monitor a small subset of pods of a large cluster,
# we recommend using selectors. The decision on the use of selectors or not depends
# on the particular situation.
[ selectors:
          [ - role: <string>
                  [ label: <string> ]
                  [ field: <string> ] ]]

其中 <role> 必须是 endpointsservicepodnodeingress

有关配置 Prometheus 以发现 Kubernetes 的详细示例,请参阅 此示例 Prometheus 配置文件

您可能希望查看第三方工具 Prometheus Operator,它在 Kubernetes 上自动执行 Prometheus 设置。

consul_sd_config

Consul SD 配置允许从 Consul Catalog API 检索抓取目标。使用 Catalog API 时,每个运行中的 Promtail 在发现新目标时都会获得整个 Consul 集群已知的所有服务的列表。

重新标记 期间,以下元标签可用于目标:

  • __meta_consul_address: 目标的地址
  • __meta_consul_dc: 目标的数据中心名称
  • __meta_consul_health: 服务的健康状态
  • __meta_consul_metadata_<key>: 目标的每个节点元数据键值
  • __meta_consul_node: 为目标定义的节点名称
  • __meta_consul_service_address: 目标的业务地址
  • __meta_consul_service_id: 目标的业务 ID
  • __meta_consul_service_metadata_<key>: 目标的每个业务元数据键值
  • __meta_consul_service_port: 目标的业务端口
  • __meta_consul_service: 目标所属服务的名称
  • __meta_consul_tagged_address_<key>: 目标的每个节点带标签的地址键值
  • __meta_consul_tags: 目标的标签列表,由标签分隔符连接
yaml
# The information to access the Consul Catalog API. It is to be defined
# as the Consul documentation requires.
[ server: <host> | default = "localhost:8500" ]
[ token: <secret> ]
[ datacenter: <string> ]
[ scheme: <string> | default = "http" ]
[ username: <string> ]
[ password: <secret> ]

tls_config:
  [ <tls_config> ]

# A list of services for which targets are retrieved. If omitted, all services
# are scraped.
services:
  [ - <string> ]

# See https://www.consul.io/api/catalog.html#list-nodes-for-service to know more
# about the possible filters that can be used.

# An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
tags:
  [ - <string> ]

# Node metadata key/value pairs to filter nodes for a given service.
[ node_meta:
  [ <string>: <string> ... ] ]

# The string by which Consul tags are joined into the tag label.
[ tag_separator: <string> | default = , ]

# Allow stale Consul results (see https://www.consul.io/api/features/consistency.html). Will reduce load on Consul.
[ allow_stale: <boolean> | default = true ]

# The time after which the provided names are refreshed.
# On large setup it might be a good idea to increase this value because the catalog will change all the time.
[ refresh_interval: <duration> | default = 30s ]

请注意,用于抓取目标的 IP 号和端口组合为 <__meta_consul_address>:<__meta_consul_service_port>。然而,在某些 Consul 设置中,相关的地址在 __meta_consul_service_address 中。在这种情况下,您可以使用 重新标记 功能替换特殊的 __address__ 标签。

基于任意标签过滤服务或节点的首选且更强大的方式是 重新标记阶段。对于拥有数千个服务的用户来说,直接使用 Consul API 可能更高效,该 API 支持基本的节点过滤(目前仅支持按节点元数据和单个标签过滤)。

consulagent_sd_config

Consul Agent SD 配置允许从 Consul 的 Agent API 检索抓取目标。使用 Agent API 时,每个运行中的 Promtail 在发现新目标时只会获取注册到与本地 Agent 在同一主机上运行的服务的列表。这适用于非常大的 Consul 集群,对于这些集群来说,使用 Catalog API 会太慢或资源密集。

重新标记 期间,以下元标签可用于目标:

  • __meta_consulagent_address: 目标的地址
  • __meta_consulagent_dc: 目标的数据中心名称
  • __meta_consulagent_health: 服务的健康状态
  • __meta_consulagent_metadata_<key>: 目标的每个节点元数据键值
  • __meta_consulagent_node: 为目标定义的节点名称
  • __meta_consulagent_service_address: 目标的业务地址
  • __meta_consulagent_service_id: 目标的业务 ID
  • __meta_consulagent_service_metadata_<key>: 目标的每个业务元数据键值
  • __meta_consulagent_service_port: 目标的业务端口
  • __meta_consulagent_service: 目标所属服务的名称
  • __meta_consulagent_tagged_address_<key>: 目标的每个节点带标签的地址键值
  • __meta_consulagent_tags: 目标的标签列表,由标签分隔符连接
yaml
# The information to access the Consul Agent API. It is to be defined
# as the Consul documentation requires.
[ server: <host> | default = "localhost:8500" ]
[ token: <secret> ]
[ datacenter: <string> ]
[ scheme: <string> | default = "http" ]
[ username: <string> ]
[ password: <secret> ]

tls_config:
  [ <tls_config> ]

# A list of services for which targets are retrieved. If omitted, all services
# are scraped.
services:
  [ - <string> ]

# See https://www.consul.io/api-docs/agent/service#filtering to know more
# about the possible filters that can be used.

# An optional list of tags used to filter nodes for a given service. Services must contain all tags in the list.
tags:
  [ - <string> ]

# Node metadata key/value pairs to filter nodes for a given service.
[ node_meta:
  [ <string>: <string> ... ] ]

# The string by which Consul tags are joined into the tag label.
[ tag_separator: <string> | default = , ]

请注意,用于抓取目标的 IP 地址和端口号组合为 <__meta_consul_address>:<__meta_consul_service_port>。然而,在某些 Consul 设置中,相关的地址在 __meta_consul_service_address 中。在这种情况下,您可以使用 重新标记 功能替换特殊的 __address__ 标签。

基于任意标签过滤服务或节点的首选且更强大的方式是 重新标记阶段。对于拥有数千个服务的用户来说,直接使用 Consul API 可能更高效,该 API 支持基本的节点过滤(目前仅支持按节点元数据和单个标签过滤)。

docker_sd_configs

Docker 服务发现允许从 Docker daemon 检索目标。它只会监视通过 host 参数引用的 Docker daemon 的容器。在分布式设置中,Docker 服务发现应在每个节点上运行。容器必须使用 json-filejournald 日志驱动程序运行。

请注意,发现不会拾取已完成的容器。这意味着 Promtail 在重启后不会抓取已完成容器的剩余日志。

如果 Docker 将长行拆分成不同的帧,Docker 目标会正确地连接日志段。为了避免 Promtail 中假定的无限行大小和内存不足错误,此目标应用了默认的软行大小限制 256 kiB,这与 Loki 中的默认最大行大小相对应。如果缓冲区增加超过此大小,则该行将立即发送到输出,其余部分将被丢弃。要更改此行为,将 limits_config.max_line_size 设置为非零值以应用硬限制。

配置继承自 Prometheus 的 Docker 服务发现

yaml
# Address of the Docker daemon.  Use unix:///var/run/docker.sock for a local setup.
host: <string>

# Optional proxy URL.
[ proxy_url: <string> ]

# TLS configuration.
tls_config:
  [ <tls_config> ]

# The port to scrape metrics from, when `role` is nodes, and for discovered
# tasks and services that don't have published ports.
[ port: <int> | default = 80 ]

# The host to use if the container is in host networking mode.
[ host_networking_host: <string> | default = "localhost" ]

# Sort all non-nil networks in ascending order based on network name and
# get the first network if the container has multiple networks defined, 
# thus avoiding collecting duplicate targets.
[ match_first_network: <bool> | default = true ]

# Optional filters to limit the discovery process to a subset of available
# resources.
# The available filters are listed in the Docker documentation:
# Containers: https://docs.dockerd.com.cn/engine/api/v1.41/#operation/ContainerList
[ filters:
  [ - name: <string>
      values: <string>, [...] ]
]

# The time after which the containers are refreshed.
[ refresh_interval: <duration> | default = 60s ]

# Authentication information used by Promtail to authenticate itself to the
# Docker daemon.
# Note that `basic_auth` and `authorization` options are mutually exclusive.
# `password` and `password_file` are mutually exclusive.

# Optional HTTP basic authentication information.
basic_auth:
  [ username: <string> ]
  [ password: <secret> ]
  [ password_file: <string> ]

# Optional `Authorization` header configuration.
authorization:
  # Sets the authentication type.
  [ type: <string> | default: Bearer ]
  # Sets the credentials. It is mutually exclusive with
  # `credentials_file`.
  [ credentials: <secret> ]
  # Sets the credentials to the credentials read from the configured file.
  # It is mutually exclusive with `credentials`.
  [ credentials_file: <filename> ]

# Optional OAuth 2.0 configuration.
# Cannot be used at the same time as basic_auth or authorization.
oauth2:
  [ <oauth2> ]

# Configure whether HTTP requests follow HTTP 3xx redirects.
[ follow_redirects: <bool> | default = true ]

可用元标签

  • __meta_docker_container_id: 容器的 ID
  • __meta_docker_container_name: 容器的名称
  • __meta_docker_container_network_mode: 容器的网络模式
  • __meta_docker_container_label_<labelname>: 容器的每个标签
  • __meta_docker_container_log_stream: 日志流类型 stdoutstderr
  • __meta_docker_network_id: 网络的 ID
  • __meta_docker_network_name: 网络的名称
  • __meta_docker_network_ingress: 网络是否为 ingress
  • __meta_docker_network_internal: 网络是否为 internal
  • __meta_docker_network_label_<labelname>: 网络的每个标签
  • __meta_docker_network_scope: 网络的范围
  • __meta_docker_network_ip: 容器在此网络中的 IP
  • __meta_docker_port_private: 容器上的端口
  • __meta_docker_port_public: 如果存在端口映射,则为外部端口
  • __meta_docker_port_public_ip: 如果存在端口映射,则为公共 IP

这些标签可在重新标记期间使用。例如,以下配置抓取名为 flog 的容器,并移除容器名称开头的斜杠 (/)。

yaml
scrape_configs:
  - job_name: flog_scrape
    docker_sd_configs:
      - host: unix:///var/run/docker.sock
        refresh_interval: 5s
        filters:
          - name: name
            values: [flog]
    relabel_configs:
      - source_labels: ['__meta_docker_container_name']
        regex: '/(.*)'
        target_label: 'container'

limits_config

可选的 limits_config 块配置此 Promtail 实例的全局限制。

yaml
# When true, enforces rate limiting on this instance of Promtail.
[readline_rate_enabled: <bool> | default = false]

# The rate limit in log lines per second that this instance of Promtail may push to Loki.
[readline_rate: <int> | default = 10000]

# The cap in the quantity of burst lines that this instance of Promtail may push
# to Loki.
[readline_burst: <int> | default = 10000]

# When true, exceeding the rate limit causes this instance of Promtail to discard
# log lines, rather than sending them to Loki. When false, exceeding the rate limit
# causes this instance of Promtail to temporarily hold off on sending the log lines and retry later.
[readline_rate_drop: <bool> | default = true]

# Limits the max number of active streams.
# Limiting the number of streams is useful as a mechanism to limit memory usage by Promtail, which helps
# to avoid OOM scenarios.
# 0 means it is disabled.
[max_streams: <int> | default = 0]

# Maximum log line byte size allowed without dropping. Example: 256kb, 2M. 0 to disable.
# If disabled, targets may apply default buffer size safety limits. If a target implements
# a default limit, this will be documented under the `scrape_configs` entry.
[max_line_size: <int> | default = 0]
# Whether to truncate lines that exceed max_line_size. No effect if max_line_size is disabled
[max_line_size_truncate: <bool> | default = false]

target_config

target_config 块控制从发现的目标读取文件的行为。

yaml
# Period to resync directories being watched and files being tailed to discover
# new ones or stop watching removed ones.
sync_period: "10s"

options_config

tracing_config

tracing 块配置 Jaeger 的跟踪。目前,仅限于通过 环境变量 配置。

yaml
# When true,
[enabled: <boolean> | default = false]

Docker 配置示例

在独立机器上跟踪 Docker 文件相当困难,因为它们在不同的操作系统中有不同的位置。对于本地 Docker 安装或 Docker Compose,我们推荐使用 Docker logging driver

如果在 Kubernetes 环境中运行,您应该查看 helm (helm) 和 jsonnet (jsonnet) 中定义的配置,这些配置利用 Prometheus 服务发现库(并由此得名 Promtail)来自动查找和跟踪 pod。jsonnet 配置通过注释解释了每个部分的作用。

静态配置示例

虽然 Promtail 可能因 Prometheus 服务发现代码而得名,但相同的代码对于直接在虚拟机或裸机上跟踪日志(无容器或容器环境)非常有效。

yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /var/log/positions.yaml # This location needs to be writeable by Promtail.

clients:
  - url: http://ip_or_hostname_where_Loki_run:3100/loki/api/v1/push

scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: varlogs  # A `job` label is fairly standard in prometheus and useful for linking metrics and logs.
      host: yourhost # A `host` label will help identify logs from this machine vs others
      __path__: /var/log/*.log  # The path matching uses a third party library: https://github.com/bmatcuk/doublestar

如果您正在轮转日志,使用通配符模式如 *.log 时要小心,并确保它不匹配已轮转的日志文件。例如,如果您每晚将日志从 server.log 移动到同一目录下的 server.01-01-1970.log,使用 *.log 之类的通配符搜索模式的静态配置会拾取该新文件并读取它,从而导致整个天的日志被重新摄取。

无目标静态配置示例

虽然 Promtail 可能因 Prometheus 服务发现代码而得名,但相同的代码对于直接在虚拟机或裸机上跟踪日志(无容器或容器环境)非常有效。

yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /var/log/positions.yaml # This location needs to be writeable by Promtail.

clients:
  - url: http://ip_or_hostname_where_Loki_run:3100/loki/api/v1/push

scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - labels:
      job: varlogs  # A `job` label is fairly standard in prometheus and useful for linking metrics and logs.
      host: yourhost # A `host` label will help identify logs from this machine vs others
      __path__: /var/log/*.log  # The path matching uses a third party library: https://github.com/bmatcuk/doublestar

Journal 配置示例

此示例读取 systemd journal 中的条目

yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://ip_or_hostname_where_loki_runs:3100/loki/api/v1/push

scrape_configs:
  - job_name: journal
    journal:
      max_age: 12h
      labels:
        job: systemd-journal
    relabel_configs:
      - source_labels: ['__journal__systemd_unit']
        target_label: 'unit'

Syslog 配置示例

此示例将 Promtail 作为 syslog 接收器启动,并可以通过 TCP 接受 Promtail 中的 syslog 条目

yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://loki_addr:3100/loki/api/v1/push

scrape_configs:
  - job_name: syslog
    syslog:
      listen_address: 0.0.0.0:1514
      labels:
        job: "syslog"
    relabel_configs:
      - source_labels: ['__syslog_message_hostname']
        target_label: 'host'

Push 配置示例

此示例将 Promtail 作为 Push 接收器启动,并将接受来自其他 Promtail 实例或 Docker Logging Driver 的日志

yaml
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://ip_or_hostname_where_Loki_run:3100/loki/api/v1/push

scrape_configs:
- job_name: push1
  loki_push_api:
    server:
      http_listen_port: 3500
      grpc_listen_port: 3600
    labels:
      pushserver: push1

请注意,必须提供 job_name,并且在多个 loki_push_api scrape_configs 之间必须是唯一的,它将用于注册指标。

创建了一个新的服务器实例,因此 http_listen_portgrpc_listen_port 必须与 Promtail server 配置部分中的不同(除非它被禁用)

如果不使用 httpgrpc,您可以将 grpc_listen_port 设置为 0 以分配随机端口。