【问题标题】:How to set a label in Service monitor so it appears in Prometheus metrics?如何在服务监视器中设置标签,使其出现在 Prometheus 指标中?
【发布时间】:2020-09-11 18:53:19
【问题描述】:

我想在服务监视器中设置 targetLabels,例如键值映射,以便它出现在 Prometheus 指标中。 我尝试在服务监视器中重新标记,但没有成功。

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-application
  labels:
    team: backend-team
spec:
  selector:
    matchLabels:
      app: example-application
  endpoints:
  - port: backend
    relabelings:
    - sourceLabels: [__name__]
      regex: (.*)
      targetLabel: teamname
      replacement: "backend-team"
      action: replace

我还发现有targetLabels。但不知道如何使用它。 TargetLabels 在 Kubernetes 服务上传输标签 到目标上。

我还读到服务监视器中可能有白名单标签。 请让我知道如何执行此操作以及哪种方法是正确的选项..

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 是的..我现在正在添加答案

标签: prometheus-operator


【解决方案1】:

在 Servicemonitor 规范中,我们需要添加目标标签以便将服务标签传播到 Prometheus。

带有“teamname”标签的示例服务:

kind: Service
apiVersion: v1
metadata:
  name: example-application
  labels:
    app: example-application
    teamname: neon
spec:
  selector:
    app: example-application
  ports:
  - name: backend
    port: 8080

从服务传播“团队名称”标签的示例 Servicemonitor:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-application
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: example-application
  endpoints:
  - port: backend
    path: /prometheus
  namespaceSelector:
    matchNames:
    - testns
  targetLabels:
    - teamname
    - app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多