Page MenuHomePhabricator

Define alert conditions in Prometheus (if possible)
Closed, ResolvedPublic

Description

We should be able to write PromQL queries (using the existing metrics) that define the alert conditions for:

  • Rise in 500 errors (number of 5xx response > threshold over x min)
  • Webservice not responding (maybe using number of 5xx responses from the haproxy frontend?)
    • Note that we already have health checks for webservices that will attempt restarting webservices that are not responding
    • Using blackbox probes would be an interesting addition, but it’s not in scope for this task
  • Webservice restarting often (we should have a metric for number of pod restarts)

If we can define alerts and see them fire in alertmanager, then we’re halfway there and we need a way to send notifications for these alerts to the right users.

Details

Related Changes in GitLab:
TitleReferenceAuthorSource BranchDest Branch
maintainer-alerts: re-enable alerts and add labelrepos/cloud/toolforge/alerts!70fnegriT432863-4main
maintainer-alerts: disable alerts temporarilyrepos/cloud/toolforge/alerts!68fnegriT432863-3main
maintaner-alerts: remove team=wmcsrepos/cloud/toolforge/alerts!67fnegriT432863-2main
Add new alerts for tool maintainersrepos/cloud/toolforge/alerts!66fnegriT432863main
Customize query in GitLab

Event Timeline

fnegri updated the task description. (Show Details)

Proof of concept: fire alerts with a test tool and see if it appears in Prometheus

fnegri changed the task status from Open to In Progress.Jul 24 2026, 2:47 PM
fnegri claimed this task.
fnegri moved this task from Backlog to In progress on the tools-platform-team board.

I have started looking into this.

A query like this one seems to identify the tools where the number of 5xx responses were more than 50% of all responses over the past 10 minutes:

(
  sum by (destination_service_namespace) (
    rate(istio_requests_total{response_code=~"5.."}[10m])
  )
/
  sum by (destination_service_namespace) (
    rate(istio_requests_total[10m])
  )
) > 0.5

Combining this with a for: 30m rule in the Prometheus alert definition could be a reasonable starting point.

If I understand this correctly, this query counts tools that are returning 5xx codes to the end user (tool -> istio proxy -> (5xx detected here) -> end user). The same metric contains a response_flag label that should indicate what happens between the tool and istio.

I will continue testing this next week, and spin up a test tool to generate different types of errors and verify how the metric responds.

I spinned up a test app at https://fnegri-test.toolforge.org/ which returns 200 on / and returns 500 on /error. I verified the PromQL query proposed in the previous comment correctly tracks the percentage of 5xx responses returned by the app.

Some ideas for additional alerting conditions:

  • webservice is stopped If the webservice is stopped with toolforge webservice stop, the URL returns 404 and destination_service_namespace is not set in the metric, so it can not be caught by the query above. We probably don't want to alert if the webservice is manually stopped by a tool maintainer, so that should be ok.
  • webservice is up but not responding If the webservice is unhealthy (tested with kubectl scale deployment.apps/fnegri-test --replicas=1), Istio returns 503 and adds response_flags="UH" (no healty upstream) to the metric, so we could have a different alert defined for this case. We should probably include other flags, like UF (upstream connection failure) and UC (upstream connection termination). This blog post (hat tip: @fgiunchedi) provides a list of possible response_flags reported by Istio/Envoy.
  • webservice is very slow We could also alert if the response time is consistently very high (e.g. p95 > 60 seconds). Although some tools doing slow queries/calculations might have slow responses by design. Test query: histogram_quantile(0.95, sum by (destination_service_namespace, le) (rate(istio_request_duration_milliseconds_bucket[10m]))) >60000
  • webservice is restarting often: I haven't looked into this alerting condition yet, but we should be able to leverage the metric kube_pod_container_status_restarts_total.
aputhin triaged this task as High priority.Jul 28 2026, 4:08 PM

The MR at https://gitlab.wikimedia.org/repos/cloud/toolforge/alerts/-/merge_requests/66 defines the following alerts:

  • ToolWebserviceErrors covers webservices returning 5xx
  • ToolWebserviceDown covers the webservice is up but not responding case from my previous comment
  • ToolFrequentRestarts covers the webservice is restarting often case from my previous comment

The MR includes a README file, where I listed some "known gaps", including the other possible alert conditions I mentioned in my previous comment:

  • webservice is very slow
  • webservice is stopped
fnegri renamed this task from Verify if we can define alert conditions in Prometheus to Define alert conditions in Prometheus (if possible).Jul 30 2026, 3:06 PM

Some stats on the proposed alerts. You can quickly recompute this yourself by pasting the expressions below at https://prometheus.svc.toolforge.org/tools/ or https://grafana-rw.wmcloud.org/explore, with the caveat that the alert definitions have an additional for: 20m or for: 15m, so the alert will fire only for tools where the condition remains true for longer than 15/20 minutes.

ToolWebserviceErrors

  (
      sum by (destination_service_namespace) (
        rate(istio_requests_total{response_code=~"5..",response_flags="-"}[10m])
      )
    /
      sum by (destination_service_namespace) (rate(istio_requests_total[10m]))
  )
> 0.5

ToolWebserviceErrors would be currently firing for 9 tools, and would have fired for up to 75 tools in the past 6 hours (probably fewer because of the for: clause).

ToolWebserviceDown

  (
      sum by (destination_service_namespace) (
        rate(istio_requests_total{response_flags=~"(.*,)?(UH|UF|UT|UC|UO|SI|UPE)(,.*)?"}[10m])
      )
    /
      sum by (destination_service_namespace) (rate(istio_requests_total[10m]))
  )
> 0.5

ToolWebserviceDown would be currently firing for 4 tools, and would have fired for up to 43 tools in the past 6 hours (probably fewer because of the for: clause).

ToolFrequentRestarts

  sum by (namespace) (increase(kube_pod_container_status_restarts_total{namespace=~"tool-.*"}[1h]))
> 5

ToolFrequentRestarts would be currently firing for 67 tools, and would have fired for up to 73 tools in the past 6 hours (probably fewer because of the for: clause).

The alerts are working as expected:

Screenshot 2026-08-12 at 12.08.05.png (2,588×2,524 px, 880 KB)

However, notifications are being sent to IRC (#wikimedia-cloud-feed) and to email (cloud-admin-feed@lists.wikimedia.org), which is not desirable. I will disable the alerts until I find a solution.

From a discussion in IRC with @taavi:

12:51 <taavi> but yeah, you either need completely separate prometheus+AM instances, or you need to extend the metricsinfra tooling for more specific alert routing rules support

I will look into these two options tomorrow.

I will look into these two options tomorrow.

I am hopeful that we can avoid the separate prometheus/alertmanager instances, I created T434801: [metricsinfra] allow custom routing for Toolforge alerts to discuss how we could tweak metricsinfra to support this use case.

With the metricsinfra patches in T434801: [metricsinfra] allow custom routing for Toolforge alerts, alerts can now be routed differently based on labels. In https://gitlab.wikimedia.org/repos/cloud/toolforge/alerts/-/merge_requests/70 I added a custom label {audience: tool-maintainers} and re-enabled the alerts.

The alerts are no longer being sent to the default admin channels (IRC and cloud-admin-feed mailing list), but are visible at https://alerts.wikimedia.org/?q=audience%3Dtool-maintainers:

Screenshot 2026-08-18 at 17.49.40.png (3,202×1,442 px, 525 KB)

One concern that was raised by @dcaro is that we are mixing admin alerts and maintainer alerts in the same alerting infrastructure. Moreover, we're relying on metricsinfra, making it a dependency for the Toolforge Alerting System described in T432860: ST5.4.2 Toolforge Alerting System. This could be an argument for having a dedicated alertmanager instance, however I believe the current implementation is a good first iteration, and does not prevent in any way migrating to a dedicated alertmanager instance in the future.

The next step will be building the "Toolforge alerting service", and adding an alertmanager webhook to send these alerts to the new service. This is tracked in T432865: Email tool maintainers when an alert condition is detected.

Yep, there's a couple things we should keep in mind:

  • redeployability/reproducibility: it's a great advantage to be able to deploy toolforge by itself and retest/redeploy (ex. lima-kilo, toolsbeta)
  • self-containment: we have put a lot of effort in separating toolforge components from shared ones, partially to be able to follow the previous principle, partly to benefit of not having to know/take into account a way wider cross-project/infra impact when doing so
  • different instances for different audiences: we also have pushed to keep user-facing services and infra level services separated, so we can deliver both, a simplified user experience to users, while still being able to benefit from keeping infra layer services simpler (no need for backwards compatibility, no need for multitenancy, no need for multi-user support, etc.), simplifying both the user services and the infra/toolforge-root only ones

So even if in a first iteration it might be beneficial to "skip" some of these, I strongly suggest that we keep that acquired technical debt in mind for a next round and make sure we take the time to pay it back before it starts slowing us down.

I agree on the three principles, and I think having a dedicated alertmanager instance that can be deployed in lima-kilo is the desirable end state. That will ensure redeployability and self-containment, and also achieve "different instances for different audiences" as the alerts for maintainers will go through the dedicated alertmanager instance.

On how to get there:

T432865: Email tool maintainers when an alert condition is detected will still rely on metricsinfra, so that we can deploy it sooner and start testing it with real users. We can test the service in lima-kilo by recording real alertmanager webhook payloads and sending them to the service. This is not as good as a full end-to-end test, but it's good enough to deploy the new service.

I'll create a separate task for setting up a dedicated Toolforge alertmanager, which will allow us to reproduce the full alerting flow (metric -> alert triggering -> email generation). This task should include deploying alertmanager to lima-kilo, alongside a prometheus instance (that is currently missing from lima-kilo). I will file it as a sub-task of T432860: ST5.4.2 Toolforge Alerting System but it's not a strict requirement for delivering the hypothesis and it can be pushed to next quarter if we're tight with time.

Until then, metricsinfra remains a dependency, but it's only routing information. The alert rules are in the toolforge/alerts repo, the alerts are already visible in the Toolforge prometheus (https://prometheus.svc.toolforge.org/tools/), and the emailing logic will be in the Toolforge Alerting Service.

The metricsinfra AM deployment is already highly available and designed to be used by multiple projects with different alerting needs and targets. Replacing it with (a pair of) VMs in the tools project would in my opinion bring very little benefits compared to the current setup.

If you'd like to move more of the alerting stack into Kubernetes-deployed Toolforge components that could be replicated in lima-kilo, I guess you could run Alertmanager in Kubernetes, although that still leaves the Prometheus server which is at the moment in production best deployed as a VM.

I guess you could run Alertmanager in Kubernetes, although that still leaves the Prometheus server which is at the moment in production best deployed as a VM.

I think having a Toolforge Alertmanager in Kubernetes could be a nice solution that would satisfy @dcaro's principles above without adding other VMs. We would still need a Prometheus instance inside lima-kilo for full end-to-end testing, maybe that could run in k8s for lima-kilo, but remaining as a VM in tools/toolsbeta.

fnegri moved this task from In progress to Done on the tools-platform-team board.

I'll create a separate task for setting up a dedicated Toolforge alertmanager

Task created: T435636: Add a dedicated Toolforge alertmanager instance

Everything else in this task is done, marking it as Resolved.