简体   繁体   English

如何限制普罗米修斯中的目标数量

[英]how to limit number of targets in prometheus

I am wondering is there a way to limit the number of targets in prometheus.我想知道有没有办法限制普罗米修斯中的目标数量。 Searched prometheus documentation, but couldn't find such one.搜索了普罗米修斯文档,但找不到这样的文档。 We are deploying prometheus using operators on k8s cluster and targets are added using servicemonitor crd.我们在 k8s 集群上使用算子部署 prometheus,并使用 servicemonitor crd 添加目标。 These yaml files are written by devs and I as a infra team can not control addition done by dev people.这些 yaml 文件是由开发人员编写的,我作为基础设施团队无法控制开发人员所做的添加。 I am looking for global option in prometheus which can limit the addition of targets done in prometheus and throw error on prometheus dashboard saying something like upper limit has been reached.我正在寻找普罗米修斯中的全局选项,它可以限制在普罗米修斯中完成的目标的添加,并在普罗米修斯仪表板上抛出错误,说已经达到上限。

Prometheus version-2.7普罗米修斯 2.7 版

You cannot limit the number of targets;您不能限制目标的数量; this would make a very odd feature.这将是一个非常奇怪的功能。 But you can monitor the number of targets scraped by Prometheus and trigger an error whenever this number exceeds a given threshold.但是您可以监控 Prometheus 抓取的目标数量,并在此数量超过给定阈值时触发错误。

- alert: TooManyTargetsInPrometheus
  expr: count(up) > 42
  for: 5m
  labels:
    severity: critical
  annotations:
    summary: "Prometheus instance has too much jobs"
    description: "Prometheus has {{ $value }} targets which is way too much."

If you want automation, you can trigger an action on the alert to rollback the configuration or downsize the offending processes.如果您想要自动化,您可以触发警报上的操作以回滚配置或缩小有问题的进程。

In my opinion, you would alert if your rate of missed scrape start to increase or on the number of metrics ingested by seconds exceeds recommendations, not on a arbitrary limit.在我看来,如果您的错过抓取率开始增加或以秒为单位摄取的指标数量超过建议,您会发出警报,而不是任意限制。

You are looking for a ServiceMonitor property called 'targetLimit'.您正在寻找名为“targetLimit”的 ServiceMonitor 属性。

Here is an example of how to use it:以下是如何使用它的示例:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-scraper
  labels:
    release: kube-prometheus-stack-1606315657
spec:
  selector:
    matchLabels:
      monitor: 'true'
  targetLimit: 1
  endpoints:
  - port: web
    path: /metrics

Go to Prometheus dashboard -> Status -> Targets. Go 到 Prometheus 仪表板 -> 状态 -> 目标。 You will find this error if there are more replicas than targetLimit allows.如果副本数超过 targetLimit 允许的数量,您会发现此错误。

在此处输入图像描述

Check serviceMonitor spec 检查 serviceMonitor 规格

As of Prometheus v2.21 there is a configuration option target_limit under scrape_config .从 Prometheus v2.21 开始,在scrape_config target_limit

(noted as experimental, may change in future) (注明为实验性,未来可能会改变)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM