菜单
开源

structured_metadata

注意

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

structured_metadata 阶段是一个动作阶段,它从提取的 map 中获取数据,并修改随日志条目发送到 Loki 的结构化元数据

警告

除非您在 limits_config 中启用 allow_structured_metadata 租户配置,否则 Loki 将拒绝结构化元数据。

结构化元数据已添加到 chunk 格式 V4 中,当 schema 版本大于或等于 13 时使用此格式。(有关 schema 版本的更多详细信息,请参见 Schema Config。)

Schema

yaml
structured_metadata:
  # Key is REQUIRED and the name for the label of structured metadata 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>] ... ]

示例

从日志条目解析

对于给定的流水线

yaml
- json:
    expressions:
      stream: stream
      traceID: traceID
- labels:
    stream:
- structured_metadata:
    traceID:

给定以下日志行

json
{"log":"log message\n","stream":"stderr","traceID":"0242ac120002","time":"2019-04-30T02:12:41.8443515Z"}

第一个阶段会将值为 stderrstream 和值为 0242ac120002traceID 提取到提取的数据集中。labels 阶段会将 streamstderr 键值对转换为流标签。structured_metadata 阶段会将 traceID0242ac120002 键值对作为结构化元数据附加到日志行。

从服务发现标签解析

对于以下配置,您可以使用 relabel_configs 中的标签作为 structured_metadata

yaml
pipeline_stages:
  - structured_metadata:
      pod_uid:
      pod_host_ip:
relabel_configs:
  - action: replace
    source_labels:
      - __meta_kubernetes_pod_uid
    target_label: pod_uid
  - action: replace
    source_labels:
      - __meta_kubernetes_pod_host_ip
    target_label: pod_host_ip

给定以下发现的标签以及日志行 sample log

- 发现的标签- 值- 目标标签
__meta_kubernetes_pod_host_ip127.0.0.1pod_host_ip
__meta_kubernetes_pod_uidb3937321-fe90-4e15-ac94-495c8fdb9202pod_uid

structured_metadata 阶段会将发现的标签 pod_uidpod_host_ip 转换为键值对,作为日志行 sample log 的结构化元数据,并将其排除在创建高基数流之外。