Search code examples
prometheusgrafanaprometheus-pushgateway

Get total of metrics when value all time equal 1?


I need to build a Grafana chart that shows the number of completed test runs by status for a specific branch. For example, over the last 12 hours for the develop branch, 12 runs passed and 3 runs failed.

I have a Gauge metric defined in TypeScript:

this.runStatusGauge = new Gauge({
  name: 'test_run_status',
  help: 'Test run status',
  labelNames: ['runName', 'status'],
  registers: [this.register],
});

I always set this metric to 1, only changing the status label:

this.runStatusGauge.set(
  {
    runName: this.runName,
    status: '<passed or failed>',
  },
  1,
);

Then I group the metric like this and push to Pushgateway:

await this.pushgateway.pushAdd({
  jobName: this.jobName,
  groupings: {
    env: this.options.env,
    projectName: this.projectName || 'undefined project',
    branchName: this.branchName,
  },
});

As a result, Prometheus receives metrics such as:

test_run_status{
  branchName="local-new",
  env="staging",
  exported_env="staging",
  exported_job="pw_metrics",
  instance="",
  job="external-pushgateway",
  project="",
  projectName="widget",
  runName="Local run name 5f99692c-32df-4d33-89f9-d2ddee90c1d9",
  service="",
  source="",
  status="failed"
} 1

I’m trying to build a Pie Chart in Grafana, but it always shows separate values for each status, and they are always equal to 1. How can I make it display the total number of runs per status correctly? enter image description here


Solution

  • Resolved this issue =)

    I just added additional query and added filter by status for each query and now its work right for all time ranges.

    enter image description here