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

如何在ServiceMonitor中为Job添加静态标签?

如何通过ServiceMonitor为Blackbox Exporter的指标添加固定标签

你可以通过以下两种方式在ServiceMonitor中为该Job的所有指标添加project: monitoring标签:

方法1:在采集阶段直接添加标签(推荐)

在ServiceMonitor的spec.endpoints下添加labels字段,这和Prometheus原生配置中static_configs里的labels作用一致,会给当前endpoint采集到的所有指标添加上指定标签。

修改后的endpoints部分配置如下:

endpoints:
- interval: 30s
  # 添加固定标签
  labels:
    project: monitoring
  metricRelabelings:
  - action: replace
    replacement: https://example.com
    sourceLabels:
    - instance
    targetLabel: instance
  - action: replace
    replacement: example.com
    sourceLabels:
    - target
    targetLabel: target
  params:
    module:
    - http_2xx
    target:
    - https://example.com
  path: /probe
  port: http
  scheme: http
  scrapeTimeout: 30s

方法2:通过metricRelabelings添加标签

利用已有的metricRelabelings规则,新增一条无sourceLabels的replace规则,直接为所有指标设置固定标签值:

修改后的metricRelabelings部分配置如下:

metricRelabelings:
- action: replace
  replacement: https://example.com
  sourceLabels:
  - instance
  targetLabel: instance
- action: replace
  replacement: example.com
  sourceLabels:
  - target
  targetLabel: target
# 新增的标签添加规则
- action: replace
  replacement: monitoring
  targetLabel: project

完整修改后的ServiceMonitor配置(以方法1为例)

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app.kubernetes.io/instance: prometheus-blackbox-exporter
    app.kubernetes.io/name: prometheus-blackbox-exporter
    app.kubernetes.io/version: 0.20.0
    instance: primary
  name: prometheus-blackbox-exporter-example.com
  namespace: monitoring
spec:
  endpoints:
  - interval: 30s
    labels:
      project: monitoring
    metricRelabelings:
    - action: replace
      replacement: https://example.com
      sourceLabels:
      - instance
      targetLabel: instance
    - action: replace
      replacement: example.com
      sourceLabels:
      - target
      targetLabel: target
    params:
      module:
      - http_2xx
      target:
      - https://example.com
    path: /probe
    port: http
    scheme: http
    scrapeTimeout: 30s
  jobLabel: prometheus-blackbox-exporter
  namespaceSelector:
    matchNames:
    - monitoring
  selector:
    matchLabels:
      app.kubernetes.io/instance: prometheus-blackbox-exporter
      app.kubernetes.io/name: prometheus-blackbox-exporter

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 11:35:59