An alert should fire when the application logs `OutOfMemoryError` more than 3 times in 5 minutes. How do you build it?

BeginnerScenarioLogging Pipelines
Answer

Situation read → A log-based alert. With Loki you can use the ruler with LogQL metric queries.

What I'd do →

  1. Write a LogQL query and test it in Grafana Explore.
  2. Put it in a Loki ruler rule group that sends to Alertmanager:
groups:
  - name: app-logs
    rules:
      - alert: JavaOOM
        expr: sum by (service) (count_over_time({env="prod"} |= "OutOfMemoryError" [5m])) > 3
        for: 0m
        labels: {severity: page}
        annotations: {summary: "{{ $labels.service }} OOM errors in logs"}
  1. Prefer also exposing a metric (jvm OOM counter or container restarts with OOMKilled) for long-term trends.

Why → Log alerts catch events with no metric, but metrics are cheaper to evaluate.

Interview tip → If you can turn a log pattern into a metric, do it; log alerts are a bridge.

All Monitoring & Observability questions Roadmap: Logging & tracing

More on Logging Pipelines