菜单
开源

tenant

注意

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

租户(tenant)阶段是一个操作阶段,它从提取的数据映射中的字段中选取日志条目的租户 ID 并进行设置。如果该字段缺失,将使用默认的 promtail 客户端 tenant_id

Schema

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> ]

示例: 从结构化日志中提取租户 ID

对于给定的流水线

yaml
pipeline_stages:
  - json:
      expressions:
        customer_id: customer_id
  - tenant:
      source: customer_id

假设有以下日志行

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

第一个阶段会将 customer_id 提取到提取映射中,其值为 1。租户阶段会将 X-Scope-OrgID 请求头(Loki 用于识别租户)设置为提取的 customer_id 数据的值,即 1

示例: 使用配置的值覆盖租户 ID

对于给定的流水线

yaml
pipeline_stages:
  - json:
      expressions:
        app:
        message:
  - labels:
      app:
  - match:
      selector: '{app="api"}'
      stages:
        - tenant:
            value: "team-api"
  - output:
      source: message

假设有以下日志行

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

该流水线将

  1. 解码 JSON 日志
  2. 设置标签 app="api"
  3. 处理 match 阶段,检查选择器 {app="api"} 是否匹配,如果匹配则运行子阶段。tenant 子阶段会将租户 ID 覆盖为值 "team-api"

示例: 从 Kubernetes 服务发现中提取租户 ID

yaml
scrape_configs:
  - job_name: kubernetes-pods-name

    kubernetes_sd_configs:
      - role: pod

    relabel_configs:
      - action: replace
        source_labels:
          - __meta_kubernetes_namespace
        target_label: namespace

    pipeline_stages:
    - match:
        selector: '{namespace=~".+"}'
        stages:
          - tenant:
              label: "namespace"
    - output:
         source: message

该流水线将

  1. 匹配任何 namespace 标签与正则表达式 .+ 匹配的日志。
  2. 处理 match 阶段,检查选择器 {namespace=~".+"} 是否匹配,如果匹配则运行子阶段。tenant 子阶段会将租户 ID 覆盖为 namespace 标签的值(如果已设置)。