Terminal prompt logo for Ali Haidry — alihaidry.dev~/AHAli Haidryalihaidry.dev
Phase 6 of 6Published

Observability

Phase 5 made three tools produce signals: Falco events, Trivy reports and Gatekeeper violations. This phase makes those signals, and the application behaviour, visible and actionable.

PrometheusGrafanaAlertManagerSlackKubernetesUpdated Nov 2026

Goal

What this phase delivers

Platform and application metrics in one place, dashboards stored in Git, and alerts that reach Slack. Every alert rule is proven by causing the condition, not by trusting that it loaded.

  • Platform metricskube-prometheus-stack on the monitoring node pool; Argo CD, cert-manager, Gatekeeper, Falco and Trivy Operator are targets.Live
  • Application metricsRequest counts and latency by route, WebSocket connections, messages and connection attempts by result.Live
  • Dashboards in GitGrafana dashboards stored as ConfigMaps, reviewed like code.Live
  • Alert rulesApiScrapeDown, ApiHighErrorRate, ApiHighLatencyP95, WebSocketRejectionsHigh and EdgeHighErrorRate.Live
  • Slack deliveryAlertManager sends firing and resolved messages to Slack.Live

Steps and commands

Run these in dev first. Every step has a check that proves it worked, so you are never guessing.

  1. Create the Slack webhook secret

    Keep the webhook URL out of Git. AlertManager reads it from a Secret.

    $ read -rsp "Slack webhook URL: " HOOK; echo
    $ kubectl -n monitoring create secret generic alertmanager-slack --from-literal=url="$HOOK"
    $ unset HOOK
    Check: the secret exists and the AlertManager pod starts without configuration errors.
  2. Enable application scraping per environment

    Switch on metrics with a chart flag. It creates a PodMonitor and a NetworkPolicy so Prometheus can reach the api. Merge the image first, then the flag.

    # environments/dev/values.yaml
    metrics:
      enabled: true
    Check: the api target is UP in Prometheus and ApiScrapeDown does not fire.
  3. Check the alert rules loaded

    A rule that is missing from the cluster cannot fire.

    $ kubectl get prometheusrule -n monitoring
    $ kubectl -n monitoring port-forward svc/kube-prometheus-stack-alertmanager 9093
    Check: all five application rules are listed and healthy.
  4. Prove an alert by causing it

    Apply a temporary rule with kubectl (not Git) that fires when a WebSocket is open, open a chat room in a browser tab and watch it reach Slack. Close the tab for the resolved message.

    $ kubectl apply -f test-rule.yaml
    # open a chat room, wait for the Slack message, close the tab
    $ kubectl delete prometheusrule paktech-alert-test -n monitoring
    Check: the firing message and the resolved message both arrive, and the test rule is deleted afterwards.

Workflow

How a signal travels from a pod to a person.

  1. STEP 1ScrapePrometheus pulls /metrics
  2. STEP 2EvaluateAlert rules
  3. STEP 3RouteAlertManager
  4. STEP 4NotifySlack
  5. STEP 5DisplayGrafana dashboards

Rollback: Set metrics.enabled back to false and merge. Argo CD removes the PodMonitor and the NetworkPolicy; the app itself is unaffected.

Diagram

How Phase 6 fits together: scrape, evaluate, alert, display
Figure: scrape, evaluate, alert, display.
The application dashboard: connections, messages, request rate, latency and errors by route
Figure: the application dashboard.
The five application alert rules, all healthy
Figure: the five application alert rules.
Slack alerts: a real node alert, the resolved message and the WebSocket test alert
Figure: alerts arriving in Slack.

Issues met

These are real, from the run. The surprises are the useful part.

Metrics values jumped between scrapes
Cause
gunicorn runs two uvicorn workers, so each process had its own counters and scrapes alternated between them.
Fix
prometheus-client multiprocess mode: PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus on the emptyDir, plus gunicorn hooks that wipe the directory on start and mark dead workers.
/api/v1/health was recorded as /health
Cause
With this Starlette version, the route path in the request scope is relative to the router.
Fix
Rebuild the label from the original path and path parameters, and pin it with a regression test.
A false ApiScrapeDown after enabling metrics
Cause
The flag was enabled before the image that exposes /metrics was running.
Fix
Order matters: the image first, then the flag.

Trade-offs

Metric labels
I chose: Route template, never the real path

Why: One series per route instead of one per room keeps the cardinality low.

It costs: You lose per-room detail in metrics. Logs are for that.

Latency alert and AI routes
I chose: Exclude AI routes from the p95 rule

Why: Calls to the Anthropic API are legitimately slow and would make the alert noisy.

It costs: A slow AI route needs its own, separate rule.

Interview questions

How do you expose Prometheus metrics from a multi-worker Python app?
Use prometheus-client multiprocess mode with a writable directory, clean it on start and mark workers dead when they exit, otherwise each scrape sees one worker.
How do you know an alert works?
Cause the condition. A temporary rule fired from a real WebSocket in a browser tab exercised the gauge, the scrape, the rule, AlertManager and the Slack template together.
Why label requests by route template?
To keep cardinality bounded. A label per real path creates a series per room or per user and overloads Prometheus.