<p>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. These yaml files are written by devs and I as a infra team can not control addition done by dev people. 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.</p>

<p>Prometheus version-2.7</p>

<p>You are looking for a ServiceMonitor property called 'targetLimit'.</p>
<p>Here is an example of how to use it:</p>
<pre><code>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
</code></pre>
<p>Go to Prometheus dashboard -&gt; Status -&gt; Targets. You will find this error if there are more replicas than targetLimit allows.</p>
<p><a href="https://i.stack.imgur.com/5J71r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5J71r.png" alt="enter image description here" /></a></p>
<p><a href="https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#servicemonitorspec" rel="nofollow noreferrer">Check serviceMonitor spec</a></p>
