首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在服务监视器中向作业添加静态标签

如何在服务监视器中向作业添加静态标签
EN

Stack Overflow用户
提问于 2022-11-22 17:07:16
回答 3查看 80关注 0票数 3

在普罗米修斯( prometheus )中,我们可以选择在作业的每一个度量中添加一个标签,

代码语言:javascript
复制
- job_name: 'your_job'                 
  honor_labels: true                         
  static_configs:                     
  - targets:                          
    - '127.0.0.1'          
    labels:                           
      cluster: 'stage'

我想在度量标准中添加标签,但使用服务程序。我正在使用黑匣子普罗米修斯操作员扫描一些网站。这就是我的服务监视器的样子。

代码语言:javascript
复制
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
    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

我想为这项工作中的指标添加一个标签。标签是

代码语言:javascript
复制
project: monitoring

我如何使用服务程序来实现它呢?

EN

回答 3

Stack Overflow用户

发布于 2022-11-26 16:01:26

spec.endpoints[].metricRelabelingsspec.endpoints[].relabelings下添加以下内容

代码语言:javascript
复制
- targetLabel: project  # name
  replacement: monitoring  # value

例如:

代码语言:javascript
复制
spec:
  endpoints:
  - interval: 30s
    relabelings:
    - targetLabel: project
      replacement: monitoring

在您的情况下,使用spec.endpoints[].relabelings的选项更好,因为标签不仅会出现在收集的指标上,而且也会出现在所有自动的(比如up)上。

阅读更多关于文档中重新标记的信息:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config

票数 0
EN

Stack Overflow用户

发布于 2022-11-30 22:22:01

要在Prometheus中使用ServiceMonitors向度量标准添加静态标签,可以在ServiceMonitor资源中使用static_configs字段。此字段允许您指定要刮除的目标列表及其相关标签。

下面是一个示例,说明如何使用static_configs字段将带有值monitoringproject标签添加到来自ServiceMonitor的度量中:

代码语言:javascript
复制
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
    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
  static_configs:
  - targets:
    - https://example.com
    labels:
      project: monitoring
  jobLabel: prometheus-blackbox-exporter
  namespaceSelector:
    matchNames:
    - monitoring
  selector:
    matchLabels:
      app.kubernetes.io/instance: prometheus-blackbox-exporter
      app.kubernetes.io/name: prometheus-blackbox-exporter

在上面的示例中,static_configs字段用于指定https://example.com的目标和project: monitoring的标签。这将导致Prometheus从https://example.com端点中刮取度量,并将project: monitoring标签应用于从该端点收集的所有度量。

票数 0
EN

Stack Overflow用户

发布于 2022-12-01 21:16:37

要将静态标签添加到由Prometheus中的ServiceMonitor监视的作业生成的度量中,可以在ServiceMonitor配置中使用job_label字段。

job_label字段指定一个标签,该标签将添加到由ServiceMonitor监视的作业生成的所有指标中。对于作业生成的所有指标,标签值都是相同的,因此您可以使用此字段添加应用于整个作业的静态标签。

下面是一个示例,说明如何在job_label配置中使用ServiceMonitor字段将静态标签添加到作业生成的指标中:

代码语言:javascript
复制
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
    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: project
  jobName: monitoring
  namespaceSelector:
    matchNames:
    - monitoring
  selector:
    matchLabels:
      app.kubernetes.io/instance: prometheus-blackbox-exporter
      app.kubernetes.io/name: prometheus-black
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74536514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档