OpsCanary
observabilitygrafanaPractitioner

Monitoring Cypress Tests with Grafana Cloud: A Practical Guide

5 min read Grafana BlogReviewed for accuracy
Share
PractitionerHands-on experience recommended

Monitoring your Cypress tests is essential for ensuring your web applications perform as expected. As your test suite grows, understanding test results and performance metrics becomes increasingly important. By integrating Cypress with Grafana Cloud, you can visualize and analyze these metrics, enabling better decision-making and quicker responses to issues.

Cypress exposes test results through its plugin hooks, which are then transformed into Prometheus metrics. These metrics are pushed to a Prometheus Pushgateway, which temporarily holds them for scraping by Alloy. Alloy then forwards the metrics to Grafana Cloud Metrics. Key configuration parameters include the PROMETHEUS_PUSHGATEWAY_URL, which points to your Pushgateway, and GRAFANA_CLOUD_RW_URL, which directs Alloy to your Grafana Cloud instance. You’ll also need to set up user credentials with GRAFANA_CLOUD_USER and GRAFANA_CLOUD_TOKEN for authentication.

In production, ensure you have a Cypress project set up with a cypress.config.js file that you can modify. Be aware that your metrics will only be pushed if the PROMETHEUS_PUSHGATEWAY_URL is correctly set. If not, you’ll miss out on valuable insights. This integration works seamlessly with Cypress 14.x, so make sure your version aligns with this setup. Regularly check your Grafana dashboards to monitor test performance and catch issues early.

Key takeaways

  • Configure `PROMETHEUS_PUSHGATEWAY_URL` to enable metrics pushing.
  • Use `GRAFANA_CLOUD_RW_URL` for Grafana Cloud integration.
  • Implement Cypress hooks to capture test results as Prometheus metrics.
  • Set up Alloy to scrape metrics from the Pushgateway and send them to Grafana Cloud.
  • Ensure your Cypress version is compatible, specifically using Cypress 14.x.

Why it matters

In production, effective monitoring of Cypress tests can significantly reduce downtime and improve application reliability. By visualizing test metrics, teams can quickly identify and resolve issues, leading to faster deployment cycles and enhanced user satisfaction.

Code examples

JavaScript
1// cypress.config.js
2module.exports = defineConfig({
3  e2e: {
4    setupNodeEvents(on) {
5      on("before:run", () => {
6        runEpoch = String(Date.now());
7        specMetrics.clear();
8      });
9
10      on("after:spec", async (spec, results) => {
11        try {
12          await pushSpecToPrometheus(spec, results);
13        } catch (err) {
14          console.error(
15            `Prometheus push failed for ${spec.relative || spec.name}:`,
16            err.message
17          );
18        }
19      });
20    },
21  },
22});
JavaScript
// config.alloy  scrape the Pushgateway and ship to Grafana Cloud Metrics
prometheus.scrape "pushgateway" {  targets = [    { "__address__" = coalesce(sys.env("PUSHGATEWAY_ADDRESS"), "localhost:9091") },  ]  honor_labels    = true  metrics_path    = "/metrics"  scrape_interval = "60s"  forward_to = [prometheus.remote_write.grafana_cloud.receiver]}
prometheus.remote_write "grafana_cloud" {  endpoint {    url = sys.env("GRAFANA_CLOUD_RW_URL")    basic_auth {      username = sys.env("GRAFANA_CLOUD_USER")      password = sys.env("GRAFANA_CLOUD_TOKEN")    }  }  external_labels = {    source = "cypress-pushgateway",  }}
Bash
export GRAFANA_CLOUD_RW_URL=https://prometheus-prod-XX-XXX.grafana.net/api/prom/push
export GRAFANA_CLOUD_USER=<your-user-id>
export GRAFANA_CLOUD_TOKEN=<your-access-policy-token>

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
DigitalOcean Serverless InferenceSponsor

OpenAI & Anthropic-compatible inference API — no GPU provisioning needed. 55+ models, pay-per-token with no minimums. VPC + zero data retention by default.

Try Serverless Inference →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.