<p>I'm using <a href="https://github.com/helm/charts/tree/master/stable/prometheus" rel="noreferrer">this prometheus helm chart</a>.</p>
<p>I was wondering if it is possible to setup the prometheus operator to automatically monitor every service in the cluster or namespace without having to create a <code>ServiceMonitor</code> for every service.</p>
<p>With the current setup, when I want to monitor a service, I have to create a <code>ServiceMonitor</code> with the label <code>release: prometheus</code>.</p>
<p>Edit:</p>
<p>Service with <code>monitoring: &quot;true&quot;</code> label</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: issue-manager-service
  labels:
    app: issue-manager-app
    monitoring: &quot;true&quot;
spec:
  selector:
    app: issue-manager-app
  ports:
    - protocol: TCP
      name: http
      port: 80
      targetPort: 7200
</code></pre>
<p>&quot;Catch-All&quot; Servicemonitor:</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: service-monitor-scraper
  labels:
    release: prometheus
spec:
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics
  jobLabel: monitoring
  namespaceSelector:
    any: true
  selector:
    matchLabels:
      monitoring: &quot;true&quot;
</code></pre>

Jonas
<p>Only if you have a common label on all services</p>
<pre><code># for example:
org: &quot;my-company&quot;
# or
monitoring: &quot;true&quot;
# or 
app.kubernetes.io/managed-by: &quot;Helm&quot;  # &lt;- in most cases this represents all
</code></pre>
<p>Then you define a single, cross-namespace <code>ServiceMonitor</code>, that covers all labeled services:</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: common-monitor
  namespace: monitoring
spec:
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics
  jobLabel: monitoring
  namespaceSelector:
    any: true  # &lt;- important
  selector:
    matchLabels:
      monitoring: &quot;true&quot;  # &lt;- should match what you've chosen as common
</code></pre>
<p>Then to make sure this <code>ServiceMonitor</code> is discovered by the Prometheus Operator you either:</p>
<ul>
<li>Declare this <code>ServiceMonitor</code> via the built-in operator template: <a href="https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L2008" rel="noreferrer">https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L2008</a></li>
<li>Or, declare a <code>serviceMonitorSelector</code> that points to your <code>ServiceMonitor</code> <a href="https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L1760" rel="noreferrer">https://github.com/prometheus-community/helm-charts/blob/4164ad5fdb6a977f1aba7b65f4e65582d3081528/charts/kube-prometheus-stack/values.yaml#L1760</a></li>
</ul>
<p>This additional explicit linkage between Prometheus Operator and ServiceMonitor is done intentionally - in this way, if you have 2 Prometheus instances on your cluster (say Infra and Product) you can separate which Prometheus will get which Pods to its scraping config.</p>
<p>From your question, it sounds like you already have a <code>serviceMonitorSelector</code> based on <code>release: prometheus</code> label - try adding that on your catch-all <code>ServiceMonitor</code> as well.</p>
