prometheus - Get total of metrics when value all time equal 1? - Stack Overflow

admin2025-04-25  3

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?

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?

Share Improve this question edited Jan 16 at 14:42 Captain Sparrow asked Jan 16 at 13:56 Captain SparrowCaptain Sparrow 1451 silver badge9 bronze badges 3
  • This approach "I always set this metric to 1, only changing the status label" is incorrect, especially in conjunction with Pushgateway (and the fact that it never forgets pushed series). Instead you should have metric with value indicating result (for example, 0 - failed, 1 - succeeded) – markalex Commented Jan 16 at 18:41
  • Ok! I think you are right, but if I will change this way to use value as a status, how can I sum result for failed and passed statuses? I think I will get same problem, am I right ? – Captain Sparrow Commented Jan 16 at 22:53
  • I don't get what you are trying to visualize, exactly, so it's hard to say. Maybe what you are looking for is count_values. On the other hand, looking more closely into your metrics - I'd say Prometheus is a wrong tool for the job. It looks like you don't want metrics, but rather per run info (and metrics, and especially Prometheus, don't play with that idea well). Consider logging systems instead. For example, Loki is similar to prometheus (with similar query lang), but is specifically designed for logs. – markalex Commented Jan 16 at 23:08
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://anycun.com/QandA/1745528046a90792.html