使用 OpenTelemetry Collector 转换遥测数据

OpenTelemetry Collector 是在将数据发送给供应商或其他系统之前进行转换的便利位置。这样做通常是出于数据质量、治理、成本和安全方面的考虑。

Collector Contrib 仓库提供的处理器支持对指标、跨度(span)和日志数据进行数十种不同的转换。下面通过几个常用处理器的基础示例介绍如何入门。

基础过滤

处理器:filter processor。

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

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

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 处理器文档提供了更多示例,包括如何过滤日志和指标。

添加或删除属性

处理器:attributes processor 或 resource processor。

attributes 处理器可用于更新、插入、删除或替换指标或 trace 上的现有属性。例如,下面的配置会为所有 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 处理器与 attributes 处理器有部分相同功能,同时还支持重命名及其他专用于指标的功能。

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

metrics transform 处理器还支持正则表达式,可以同时对多个指标名称或指标标签应用转换规则。下面的示例将所有指标中的 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 processor 和 k8sattributes 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 名称中提取属性。示例见 transform 处理器的配置文件。

高级转换

transform 处理器也支持更高级的属性转换。终端用户可以使用 OpenTelemetry Transformation Language 为指标、日志和 trace 指定转换。

YAML 缩进修正版

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"

这些示例是处理器配置片段,仍需在实际 Collector 发行版中提供相应处理器并接入服务流水线。

原文:OpenTelemetry 文档贡献者,Transforming telemetry。页面最后修改日期:2025年5月22日。本版本为中文翻译,保留原文配置示例,另加 YAML 缩进校正示例;© 2019 至今 OpenTelemetry Authors,保留所有权利。源文档采用 CC BY 4.0,按原样提供,不作保证。许可文件。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容