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

如何将OpenTelemetry中JVM指标的exported_job标签值改为Pod名称

解决OpenTelemetry JVM指标exported_job标签改为Pod名称的问题

有两种可行方案,根据你的部署偏好选择:

方案一:调整应用侧自动插桩配置

直接在应用的Deployment中通过环境变量指定service.name为Pod名称,让自动插桩的指标自带正确标签:

在Deployment的容器环境变量中添加以下配置:

spec:
  template:
    spec:
      containers:
        - name: your-app-container
          env:
            # 将service.name设置为当前Pod名称
            - name: OTEL_RESOURCE_ATTRIBUTES
              value: "service.name=$(KUBERNETES_POD_NAME)"
            # 注入Pod名称环境变量
            - name: KUBERNETES_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name

应用启动后,OpenTelemetry自动插桩会将service.name设置为Pod名称,Collector导出Prometheus格式指标时,exported_job标签就会对应Pod名称。

方案二:在OpenTelemetry Collector侧修改标签

如果不想改动应用配置,可通过Collector的处理器获取Pod元数据并重写exported_job标签:

  1. 配置k8sattributes处理器:从K8s API获取Pod名称元数据
processors:
  k8sattributes:
    auth_type: "serviceAccount"
    extract:
      metadata:
        - k8s.pod.name
  1. 配置attributes处理器:将exported_job标签替换为获取到的Pod名称
processors:
  attributes/rewrite_exported_job:
    actions:
      - action: update
        key: exported_job
        from_attribute: k8s.pod.name
  1. 更新Collector的Pipeline:将上述处理器加入指标处理流程(先获取元数据再重写标签)
service:
  pipelines:
    metrics:
      receivers: [otlp]
      processors: [k8sattributes, attributes/rewrite_exported_job, batch]
      exporters: [prometheus]

注意:使用该方案需确保Collector的ServiceAccount拥有读取Pod元数据的权限,可通过ClusterRole和ClusterRoleBinding配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 12:35:19