菜单
开源

match

注意

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

Match 阶段是一个过滤阶段,当日志条目匹配可配置的 LogQL 流选择器过滤表达式时,它会条件性地应用一组阶段或丢弃条目。

注意

过滤条件不包括标签过滤表达式,例如 | label == "foobar"

Schema

yaml
match:
  # LogQL stream selector and line filter expressions.
  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>]

  # Determines what action is taken when the selector matches the log
  # line. Defaults to keep. When set to drop, entries will be dropped
  # and no later metrics will be recorded.
  # Stages must be not defined when dropping entries.
  [action: <string> | default = "keep"]
  
  # If you specify `action: drop` the metric `logentry_dropped_lines_total` 
  # will be incremented for every line dropped.  By default the reason
  # label will be `match_stage` however you can optionally specify a custom value 
  # to be used in the `reason` label of that metric here.
  [drop_counter_reason: <string> | default = "match_stage"]

  # Nested set of pipeline stages only if the selector
  # matches the labels of the log entries:
  stages:
    [<stages>...]

有关此处支持的各种阶段的 schema,请参阅Promtail 阶段配置参考

示例

对于给定的 pipeline

yaml
pipeline_stages:
- json:
    expressions:
      app:
- labels:
    app:
- match:
    selector: '{app="loki"}'
    stages:
    - json:
        expressions:
          msg: message
- match:
    pipeline_name: "app2"
    selector: '{app="pokey"}'
    action: keep
    stages:
    - json:
        expressions:
          msg: msg
- match:
    selector: '{app="promtail"} |~ ".*noisy error.*"'
    action: drop
    drop_counter_reason: promtail_noisy_error
- output:
    source: msg

以及给定的日志行

json
{ "time":"2012-11-01T22:08:41+00:00", "app":"loki", "component": ["parser","type"], "level" : "WARN", "message" : "app1 log line" }
{ "time":"2012-11-01T22:08:41+00:00", "app":"promtail", "component": ["parser","type"], "level" : "ERROR", "message" : "foo noisy error" }

第一个阶段将为第一行日志将 app 的值 loki 添加到提取映射中,而第二个阶段将 app 作为标签添加(值同样为 loki)。第二行日志将遵循相同的流程,并被添加标签 app,值为 promtail

第三个阶段使用 LogQL,仅当存在标签 app 且其值为 loki 时执行嵌套阶段。这在我们的例子中匹配了第一行日志;嵌套的 json 阶段随后将 msg 添加到提取映射中,值为 app1 log line

第四个阶段使用 LogQL,仅当存在标签 app 且其值为 pokey 时执行嵌套阶段。这在我们的例子中**不**匹配,因此嵌套的 json 阶段不运行。

第五个阶段将丢弃来自应用 promtail 且匹配正则表达式 .*noisy error 的任何条目,并会增加带有标签 reason="promtail_noisy_error" 的指标 logentry_dropped_lines_total

最后的 output 阶段将日志行的内容更改为提取映射中 msg 的值。在本例中,日志行更改为 app1 log line