You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OpenTelemetry将AWS ECS任务信息转为job/instance标签

解决AWS ECS中OT Collector资源标签转Prometheus标签的问题

核心问题在于:resourcedetection/ecs处理器获取的标签存储在metrics的resource层级,而metricstransform默认操作的是metrics的labels层级(对应Prometheus的标签),两者不在同一层级,所以直接用metricstransform访问不到resourcedetection的结果。下面给出两种可行的实现方案:

方案一:资源属性转标签+标签转换

先把resource层级的属性转成metric labels,再用metricstransform重命名/提取目标标签,步骤如下:

  1. 添加resource_to_attributes处理器:将resourcedetection拿到的ECS资源属性(如aws.ecs.service.family、aws.ecs.task.arn)转换为metric labels。
  2. 用metricstransform处理标签:把aws.ecs.service.family映射为job标签,从aws.ecs.task.arn中提取任务ID作为instance标签,最后清理冗余标签。

完整的OT Collector配置示例:

receivers:
  prometheus:
    config:
      scrape_configs:
        - job_name: 'app-metrics'
          scrape_interval: 15s
          static_configs:
            - targets: ['localhost:8080'] # 替换为你的应用Prometheus端点

processors:
  resourcedetection/ecs:
    detectors: [ecs]
    timeout: 10s
    override: false
  resource_to_attributes:
    attributes:
      - key: aws.ecs.service.family
        action: insert
      - key: aws.ecs.task.arn
        action: insert
  metricstransform:
    transforms:
      - include: .*
        match_type: regexp
        action: update
        operations:
          # 将ECS服务family设为job标签
          - action: add_label
            new_label: job
            new_value: "$${aws.ecs.service.family}"
          # 从task ARN中提取任务ID作为instance标签
          - action: add_label
            new_label: instance
            new_value: "$${aws.ecs.task.arn}"
          - action: extract_label
            label: instance
            regex: "arn:aws:ecs:.+:task/.+/(.+)"
            new_label: instance
          # 清理临时标签
          - action: delete_label
            label: aws.ecs.service.family
          - action: delete_label
            label: aws.ecs.task.arn

exporters:
  prometheusremotewrite:
    endpoint: "https://aps-workspaces.${region}.amazonaws.com/workspaces/${workspace_id}/api/v1/remote_write"
    auth:
      authenticator: sigv4auth

extensions:
  sigv4auth:
    region: "${region}"

service:
  pipelines:
    metrics:
      receivers: [prometheus]
      processors: [resourcedetection/ecs, resource_to_attributes, metricstransform]
      exporters: [prometheusremotewrite]
  extensions: [sigv4auth]

在Terraform中,可通过templatefile函数将上述配置注入到ECS任务定义的OT Collector容器中,比如挂载为配置文件或通过环境变量OTEL_COLLECTOR_CONFIG传递。

方案二:直接在Prometheus Receiver中配置Relabel规则

跳过resourcedetection的中间步骤,直接在Prometheus Receiver的抓取配置中添加relabel规则,从ECS元数据中提取所需标签:

receivers:
  prometheus:
    config:
      scrape_configs:
        - job_name: 'app-metrics'
          scrape_interval: 15s
          static_configs:
            - targets: ['localhost:8080']
          # 从ECS任务元数据中提取标签
          relabel_configs:
            - source_labels: [__meta_ecs_service_family]
              target_label: job
            - source_labels: [__meta_ecs_task_id]
              target_label: instance

这种方案更直接,无需额外处理器,但需要确保OT Collector能够访问ECS任务的元数据端点(默认ECS任务可访问http://169.254.170.2/v2/metadata),且Prometheus Receiver已启用ECS服务发现(自动发现时会自动注入__meta_ecs_*标签)。

关键注意事项

  • 方案一的处理器执行顺序必须严格遵循:resourcedetection/ecs → resource_to_attributes → metricstransform,否则无法正确传递资源属性。
  • 方案二中的__meta_ecs_*标签仅在Prometheus Receiver启用ECS服务发现时自动注入;若静态指定targets,需结合resourcedetection结果或手动从任务元数据中获取标签。

内容的提问来源于stack exchange,提问作者arian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 14:20:40