title: How to calculate the percentage by label for gauge metrics?
body: <p>I'm exporting some metrics about running tasks, the available data includes the current number of tasks by their status and queue:</p>
  
  <pre class="lang-sh prettyprint-override"><code># TYPE gauge
  tasks{queue="high", status="queued"} 2.0
  tasks{queue="high", status="started"} 1.0
  tasks{queue="high", status="successful"} 5.0
  tasks{queue="high", status="failed"} 1.0
  
  tasks{queue="low", status="queued"} 1.0
  tasks{queue="low", status="started"} 2.0
  tasks{queue="low", status="successful"} 3.0
  tasks{queue="low", status="failed"} 2.0
  </code></pre>
  
  <p>These numbers change regularly when the tasks are added or expired from the database, so for example the <code>failed</code> tasks number will go up and down depending on the tasks in the database at the time of collecting the data.</p>
  
  <p>I don't have a way to get the total tasks count, so that's all the data that I have, I want to calculate the percentage of the tasks by their <code>status</code> label and create a graph of this value using <strong>Grafana</strong>.</p>
  
  <p>How the percentage should be calculated?</p>
  
  <p>What I've tried so far:</p>
  
  <p>Get the percentage of all successful tasks:</p>
  
  <pre><code>( sum(tasks{status="successful"}) / sum(tasks) ) * 100
  </code></pre>
  
  <p>Get the percentage of successful tasks by queue:</p>
  
  <pre><code>( sum(tasks{status="finished"}) by (queue) / sum(tasks) by (queue) ) * 100
  </code></pre>
  
  <p>How can I get this percentage by time? For example when setting the time range in <strong>Grafana</strong>? I can use the variable <code>$__range</code> but how should I do the calculation?</p>
  
  <p>I have other data where I have <strong>count</strong> metrics and I'm doing the following:</p>
  
  <pre><code>sum(increase(tasks_total{status="success"}[$__range])) /
  sum(increase(tasks_total{status="started"}[$__range]))
  </code></pre>
  
  <p>But these are counts and these calculation don't apply to gauge metrics.</p>