title: Prometheus group by substring of label
body: <p>I am trying to solve a problem of making a sum and group by query in Prometheus on a metric where the labels assigned to the metric values to unique to my sum and group by requirements.</p>
  
  <p>I have a metric sampling sizes of ElasticSearch indices, where the index names are labelled on the metric. The indices are named like this and are placed in the label "index":</p>
  
  <p><code>project.&lt;projectname&gt;.&lt;uniqueid&gt;.&lt;date&gt;</code></p>
  
  <p>with concrete value that would look like this:</p>
  
  <p><code>project.sample-x.ad19f880-2f16-11e7-8a64-jkzdfaskdfjk.2018.03.12</code></p>
  
  <p><code>project.sample-y.jkcjdjdk-1234-11e7-kdjd-005056bf2fbf.2018.03.12</code></p>
  
  <p><code>project.sample-x.ueruwuhd-dsfg-11e7-8a64-kdfjkjdjdjkk.2018.03.11</code></p>
  
  <p><code>project.sample-y.jksdjkfs-2f16-11e7-3454-005056bf2fbf.2018.03.11</code></p>
  
  <p>so if I had the short version of values in the "index" label I would just do:</p>
  
  <p><code>sum(metric) by (index)</code></p>
  
  <p>but what I am trying to do is something like this:</p>
  
  <p><code>sum(metric) by ("project.&lt;projectname&gt;")</code></p>
  
  <p>where I can group by a substring of the "index" label. How can this be done with a Prometheus query? I assume this could maybe be solved using a label_replace as part of the group, but I can't just see how to "truncate" the label value to achieve this.</p>
  
  <p>Best regards</p>
  
  <p>Lars Milland</p>