Answers: <p>If I'm understanding your question, the answer comes straight out of the <a href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/#static_config" rel="nofollow noreferrer">docs</a> for <code>static_configs:</code></p>
  <blockquote>
  <p>A static_config allows specifying a list of targets and a common label set for them. It is the canonical way to specify static targets in a scrape configuration.</p>
  </blockquote>
  <p>So,</p>
  <pre class="lang-yaml prettyprint-override"><code> - job_name: cluster1
      static_configs:
        - targets:
          - server1.example.com
          - server2.example.com
          - server3.example.com
          labels:
            database: mysql
            some_carefully_chosen_property: its_value_for_these_targets
  </code></pre>
  <p>If you want to play around with a <code>prometheus.yml</code> in your local directory, to check syntax, you can run</p>
  <pre class="lang-bash prettyprint-override"><code>docker run -it --rm --name prometheus_configcheck \
    -v ${PWD}/prometheus.yml:/prometheus.yml \
    --entrypoint /bin/promtool \
    prom/prometheus:v2.40.6 \
    check config /prometheus.yml
  </code></pre>
  <p>if you don't have prometheus installed locally, or</p>
  <pre class="lang-bash prettyprint-override"><code>promtool check config prometheus.yml
  </code></pre>
  <p>if you do, and it came with promtool, which it should have.</p>
  <p><strong>However</strong> Please do not do this in general and certainly not until you fully understand the issue of cardinality, and why high cardinality will overload your system, and is unnecessary in light of PromQL.  You can destroy your setup by choosing either a) too many labels or b) labels with a high or non-bounded set of values, since each combination of labels is stored as a separate metric stream.  This article explains it well <a href="https://grafana.com/blog/2020/08/27/the-concise-guide-to-labels-in-loki/" rel="nofollow noreferrer">https://grafana.com/blog/2020/08/27/the-concise-guide-to-labels-in-loki/</a> (it's about Loki, but labels work in the same way in Prometheus).</p>
  <p>Also, your examples for your label values indicate that you may not have identified what property (key) you're talking about.  e.g. is it <code>server_type</code>?  Saying that your label is <code>mysql</code> doesn't make sense; the only values would then be <code>true</code> or <code>false</code>.  Try to find out what properties really describe your infrastructure, then don't label things with them, and use them in PromQL queries instead, or add them sparingly as labels.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-05-01T21:35:46.187Z
Answers: 2023-05-01T21:35:46.187Z
Answers: Life5ign
Body: <p>I'm adding serves to my prometheus config like this:</p>
  <pre><code>  - job_name: cluster1
      static_configs:
        - targets:
          - server1.example.com
          - server2.example.com
          - server3.example.com
  </code></pre>
  <p>but now I would like to label the instances with additional static labels, like the server1 and server2 with &quot;database&quot; and server3 with &quot;mail&quot; and maybe server1 additionally with &quot;mysql&quot; and server2 with &quot;postgres&quot;.</p>
  <p>I would then like to use this labels in grafana to filter on like here:</p>
  <pre><code>free_space_in_bytes{job=~'$job', server_type='database', database_type='mysql' }
  </code></pre>
  <p>Is this achievable within the prometheus config? Filtering on the jobname or instance is indeed possible, but I would like to extend this to more labels.</p>
  
Tags: <prometheus><grafana>
Title: Add more static instance labels in prometheus config
UserDisplayName: Vietna
ThreadText: # Add more static instance labels in prometheus config
  
  (Asked by: username_0 on 2023-03-07)
  
  I'm adding serves to my prometheus config like this:
  
  ```
    - job_name: cluster1
      static_configs:
        - targets:
          - server1.example.com
          - server2.example.com
          - server3.example.com
  
  ```
  
  but now I would like to label the instances with additional static labels, like the server1 and server2 with "database" and server3 with "mail" and maybe server1 additionally with "mysql" and server2 with "postgres".
  
  I would then like to use this labels in grafana to filter on like here:
  
  ```
  free_space_in_bytes{job=~'$job', server_type='database', database_type='mysql' }
  
  ```
  
  Is this achievable within the prometheus config? Filtering on the jobname or instance is indeed possible, but I would like to extend this to more labels.
  
  **Tags:** prometheus, grafana
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-05-01. Score: 2 | Accepted answer
  
  If I'm understanding your question, the answer comes straight out of the [docs](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#static_config) for `static_configs:`
  
  > A static\_config allows specifying a list of targets and a common label set for them. It is the canonical way to specify static targets in a scrape configuration.
  
  So,
  
  ```yaml
   - job_name: cluster1
      static_configs:
        - targets:
          - server1.example.com
          - server2.example.com
          - server3.example.com
          labels:
            database: mysql
            some_carefully_chosen_property: its_value_for_these_targets
  
  ```
  
  If you want to play around with a `prometheus.yml` in your local directory, to check syntax, you can run
  
  ```bash
  docker run -it --rm --name prometheus_configcheck \
    -v ${PWD}/prometheus.yml:/prometheus.yml \
    --entrypoint /bin/promtool \
    prom/prometheus:v2.40.6 \
    check config /prometheus.yml
  
  ```
  
  if you don't have prometheus installed locally, or
  
  ```bash
  promtool check config prometheus.yml
  
  ```
  
  if you do, and it came with promtool, which it should have.
  
  **However** Please do not do this in general and certainly not until you fully understand the issue of cardinality, and why high cardinality will overload your system, and is unnecessary in light of PromQL. You can destroy your setup by choosing either a) too many labels or b) labels with a high or non-bounded set of values, since each combination of labels is stored as a separate metric stream. This article explains it well <https://grafana.com/blog/2020/08/27/the-concise-guide-to-labels-in-loki/> (it's about Loki, but labels work in the same way in Prometheus).
  
  Also, your examples for your label values indicate that you may not have identified what property (key) you're talking about. e.g. is it `server_type`? Saying that your label is `mysql` doesn't make sense; the only values would then be `true` or `false`. Try to find out what properties really describe your infrastructure, then don't label things with them, and use them in PromQL queries instead, or add them sparingly as labels.
  
  
  ---
  
  