Query icon
OutputValues icon
If icon
SlackIncomingWebhook icon
Log icon
Schedule icon

Grafana Loki Instant Error Rate Threshold Gate

Run an instant LogQL query against Loki, alert Slack on an error rate threshold breach, and log the result for SRE teams.

Categories
Infrastructure

Loki computes a log-derived metric, like an error rate, the moment you ask for it; it does not keep a schedule, compare that number against a threshold you declare, or notify anyone when the rate crosses it. This blueprint evaluates the metric right now, as an instant query, and treats the real returned value as the governance signal, not a count estimated after scanning a window of raw log lines.

This blueprint was created by Mohit Jeswani.

How it works

  1. query_error_rate (io.kestra.plugin.grafana.loki.Query) calls Loki's /loki/api/v1/query instant-query endpoint with logql_query, evaluated at the current time. Read-only; nothing in Loki is changed. allowFailure: true covers an unreachable Loki instance or an invalid query without failing the flow outright.
  2. evaluate (io.kestra.plugin.core.output.OutputValues) reads the instant metric's own numeric value with a jq filter; an empty result set (no matching series right now) is treated as a rate of 0, not a probe failure.
  3. route (io.kestra.plugin.core.flow.If):
    • compliant: logs the current rate, below threshold.
    • alert: posts the query and the real rate to Slack.
  4. summary logs the rate and severity, closing the governance cycle.
  5. errors alerts Slack if the flow itself fails, for example a Loki auth error.

What you get

  • A governance check built on Loki's own metric evaluation, not a client-side guess from scanning raw log lines over a window.
  • A failed query and a genuine threshold breach both surface, with distinct severities (CRITICAL vs. ERROR_RATE_BREACH) so you know which one happened without reading logs.
  • A read-only check with zero risk of writing to or modifying Loki.

Who it's for

  • SRE and platform teams running Grafana Loki who want an error-rate spike paged the moment it crosses a threshold, not discovered on the next dashboard check.
  • Teams evaluating the Grafana plugin who want a worked example of loki.Query used for an instant metric threshold gate, distinct from this repo's existing Loki blueprints, which all use QueryRange (range queries over a historical window) or the realtime Trigger rather than an instant point-in-time metric evaluation.

Why orchestrate this with Kestra

Loki answers one instant query at a time; it does not decide what threshold counts as a breach, compare the real-time result against it, or notify a Slack channel when it crosses. Kestra keeps logql_query and max_error_rate as flow inputs, turns the evaluation into a Slack page naming the exact rate found, and keeps a full execution history of every check.

Prerequisites

  • A Grafana Loki instance, locally testable via the official grafana/loki Docker image, receiving logs that logql_query can aggregate.
  • A Loki auth token if the instance is secured; omit the secret for a local, unsecured instance.
  • A Slack incoming webhook URL.

Secrets

  • LOKI_AUTH_TOKEN: the bearer token used by query_error_rate for a secured Loki deployment. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_LOKI_AUTH_TOKEN. Leave unset for a local, unsecured Loki instance.
  • SLACK_WEBHOOK_URL: used by alert and the errors block. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.

Inputs

Input Default Purpose
logql_query sum(rate({job="api"} |= "error" [5m])) Instant LogQL metric expression to evaluate
max_error_rate 1.0 Value at or above which the alert fires

Outputs

  • {{ outputs.query_error_rate.logs }} / .resultType: the raw instant-query result (timestamp, value, labels per series) and Loki's own result type (vector, matrix, or streams).
  • {{ outputs.evaluate.values.current_rate }} / .severity: the real numeric value found, and OK, ERROR_RATE_BREACH, or CRITICAL.

Pitfalls

  • Doc/code mismatch found and avoided: the plugin's own @Example Javadoc for loki.Query shows a token property, but the actual field on AbstractLokiConnection is authToken. This blueprint uses the verified real name, authToken.
  • logql_query must be a metric expression (rate(...), count_over_time(...), etc.), not a bare log-selector; a bare selector returns raw log lines (resultType: streams) instead of a single numeric value, and this check's jq filter expects a number.
  • An empty instant-query result (no matching time series right now) is treated as rate 0 here; if the underlying stream itself disappearing is also worth alerting on, pair this gate with a separate check on log volume, not just rate.

Quick start

  1. Run Loki locally (docker run -p 3100:3100 grafana/loki:latest) and add the SLACK_WEBHOOK_URL secret (and LOKI_AUTH_TOKEN only if your instance is secured).
  2. Set logql_query to a real metric expression matching your log streams.
  3. Run the flow manually and confirm summary logs the current rate.
  4. Lower max_error_rate below the real current rate to confirm alert fires.
  5. Enable the every_5_minutes trigger once the threshold matches your real SLO.

How to extend

  • Loop over several logql_query values with io.kestra.plugin.core.flow.Loop to monitor more than one service's error rate in a single scheduled run.
  • Chain io.kestra.plugin.grafana.loki.QueryRange in the alert branch to pull the raw matching log lines for the breach window, for richer context than the single rate value.
  • Add a lower bound check alongside the upper threshold to also catch a rate dropping to zero unexpectedly, which can indicate a silent producer outage rather than a quiet period.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.