转换遥测数据

OpenTelemetry Collector 是一个方便的位置,可以在将数据发送给供应商或其他系统之前对其进行转换。这通常出于数据质量、治理、成本和安全原因而进行。

来自 Collector Contrib 仓库 的处理器支持对指标、span 和日志数据进行数十种不同的转换。以下部分提供了使用几个常用处理器入门的一些基本示例。

处理器的配置,特别是高级转换,可能会对收集器性能产生重大影响。

基本过滤

处理器filter processor

filter 处理器允许用户使用 OTTL 过滤遥测数据。匹配任何条件的遥测数据将被丢弃。

例如,要允许来自服务 app1、app2 和 app3 的 span 数据,并丢弃来自所有其他服务的 data

processors:
  filter/ottl:
    error_mode: ignore
    traces:
      span:
        - |
        resource.attributes["service.name"] != "app1" and
        resource.attributes["service.name"] != "app2" and
        resource.attributes["service.name"] != "app3"

仅丢弃来自名为 service1 的服务的 span,同时保留所有其他 span

processors:
  filter/ottl:
    error_mode: ignore
    traces:
      span:
        - resource.attributes["service.name"] == "service1"

有关 filter processor 文档 提供了更多示例,包括过滤日志和指标。

添加或删除属性

处理器attributes processorresource processor

attributes 处理器可用于更新、插入、删除或替换指标或跟踪中的现有属性。例如,以下配置向所有 span 添加了一个名为 account_id 的属性

processors:
  attributes/accountid:
    actions:
      - key: account_id
        value: 2245
        action: insert

resource 处理器具有相同的配置,但仅适用于 资源属性。使用 resource 处理器修改与遥测相关的基础设施元数据。例如,此配置插入了 Kubernetes 集群名称

processors:
  resource/k8s:
    attributes:
      - key: k8s.cluster.name
        from_attribute: k8s-cluster
        action: insert

重命名指标或指标标签

处理器: metrics transform processor

metrics transform processorattributes processor 共享一些功能,但也支持重命名和其他特定于指标的功能。

processors:
  metricstransform/rename:
    transforms:
      - include: system.cpu.usage
        action: update
        new_name: system.cpu.usage_time

metrics transform processor 还支持正则表达式,可以同时将转换规则应用于多个指标名称或指标标签。此示例将所有指标的 cluster_name 重命名为 cluster-name

processors:
  metricstransform/clustername:
    transforms:
      - include: ^.*$
        match_type: regexp
        action: update
        operations:
          - action: update_label
            label: cluster_name
            new_label: cluster-name

使用资源属性丰富遥测数据

处理器resource detection processork8sattributes processor

这些处理器可用于用相关的基础设施元数据丰富遥测数据,以帮助团队快速识别底层基础设施何时影响服务健康或性能。

resource detection 处理器将相关的云或主机级别信息添加到遥测数据中

processors:
  resourcedetection/system:
    # Modify the list of detectors to match the cloud environment
    detectors: [env, system, gcp, ec2, azure]
    timeout: 2s
    override: false

同样,K8s 处理器会用相关的 Kubernetes 元数据(如 pod 名称、节点名称或工作负载名称)丰富遥测数据。必须将 collector pod 配置为具有 对某些 Kubernetes RBAC API 的读取访问权限。要使用默认选项,可以使用空块进行配置

processors:
  k8sattributes/default:

设置 span 状态

处理器transform processor

使用 transform 处理器设置 span 的状态。当 http.request.status_code 属性为 400 时,以下示例将 span 状态设置为 Ok

transform:
  error_mode: ignore
  trace_statements:
    - set(span.status.code, STATUS_CODE_OK) where span.attributes["http.request.status_code"] == 400

您还可以使用 transform 处理器根据 span 的属性修改 span 名称,或从 span 名称中提取 span 属性。有关示例,请参阅 transform 处理器 配置文件

高级转换

transform processor 中也可以进行更高级的属性转换。transform 处理器允许最终用户使用 OpenTelemetry Transformation Language 指定对指标、日志和跟踪的转换。


最后修改于 2025 年 5 月 22 日:[chore] Accessible links 1 (#6049) (801233d0)