Esql icon
OutputValues icon
If icon
SlackIncomingWebhook icon
Log icon
Schedule icon

Elasticsearch ES|QL Error Count Threshold Gate

Run an ES|QL error count query against Elasticsearch, alert Slack on a threshold breach, and log the result for platform teams.

Categories
Infrastructure

An application logging errors to Elasticsearch can spike in volume with nobody watching until a customer complains; Elasticsearch itself shows the count if someone opens Discover, it does not keep a schedule, compare the count against a threshold you declare, or notify anyone. This blueprint runs the aggregation server-side with ES|QL's own STATS COUNT(*) and reports the real number, not a client-side guess.

This blueprint was created by Mohit Jeswani.

How it works

  1. query_error_count (io.kestra.plugin.elasticsearch.Esql) runs an ES|QL pipe query that filters to ERROR-level documents since lookback_minutes ago and aggregates them with STATS error_count = COUNT(*), using a dynamic ? parameter rather than string concatenation. Read-only; nothing in Elasticsearch is changed. allowFailure: true covers a connection or syntax error without failing the flow outright.
  2. evaluate (io.kestra.plugin.core.output.OutputValues) reads the real aggregated error_count directly from the ES|QL response's own row, keyed by the STATS alias.
  3. route (io.kestra.plugin.core.flow.If):
    • compliant: logs the count, below threshold.
    • alert: posts the real count to Slack.
  4. summary logs the count and severity, closing the governance cycle.
  5. errors alerts Slack if the flow itself fails, for example an unreachable cluster.

What you get

  • A count that is Elasticsearch's own ES|QL aggregation, not a client-side guess computed after paging through matching documents.
  • A failed query and a genuine threshold breach both surface, with distinct severities (CRITICAL vs. ERROR_COUNT_BREACH) so you know which one happened without reading logs.
  • A read-only check with zero risk of writing to or modifying an index.

Who it's for

  • Platform and SRE teams running Elasticsearch who want an error-volume spike paged the moment it crosses a threshold, not discovered on the next dashboard check.
  • Teams evaluating the Elasticsearch plugin who want a worked example of Esql used as a standing threshold gate with dynamic query parameters, the first blueprint in this repo to exercise ES|QL rather than the classic Search/Request DSL-based tasks.

Why orchestrate this with Kestra

Elasticsearch answers one ES|QL query at a time; it does not decide what threshold counts as a breach, compare the real-time count against it, or notify a Slack channel when it crosses. Kestra keeps lookback_minutes and max_error_count as flow inputs, turns the aggregation into a Slack page naming the real count, and keeps a full execution history of every check.

Prerequisites

  • An Elasticsearch cluster, locally testable via the official elasticsearch Docker image, with an index named logs containing a level keyword field and a @timestamp date field (or adjust the query to match your real mapping).
  • A Slack incoming webhook URL.

Secrets

  • 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
lookback_minutes 15 How far back from now to count errors
max_error_count 10 Error count at or above which the alert fires

Outputs

  • {{ outputs.query_error_count.row }} / .size / .total: the single aggregated row and row counts, as Esql reports them with fetchType: FETCH_ONE.
  • {{ outputs.evaluate.values.error_count }} / .severity: the real error count, and OK, ERROR_COUNT_BREACH, or CRITICAL.

Pitfalls

  • Query parameter values are interpolated client-side as literals, not sent as a server-side parameterized query (verified directly from the plugin's own interpolateParams implementation); this is safe for a date filter here, but is not the same security guarantee as a true server-side bind variable for untrusted input.
  • The ES|QL query and its index/field names (logs, level, @timestamp) are hardcoded on the task; adjust them to match your real index mapping before relying on this gate.
  • fetchType: FETCH_ONE assumes the query returns exactly one aggregated row; a query changed to group by an extra dimension (for example BY service) would need FETCH instead and a different evaluate step to sum across rows.

Quick start

  1. Run Elasticsearch locally (docker run -p 9200:9200 -e "discovery.type=single-node" elasticsearch:8.15.0) and add the SLACK_WEBHOOK_URL secret.
  2. Adjust query_error_count's query to match your real index, level field, and timestamp field.
  3. Run the flow manually and confirm summary logs the real count.
  4. Lower max_error_count below the real current count to confirm alert fires.
  5. Enable the every_5_minutes trigger once the threshold matches your real SLO.

How to extend

  • Add a BY service grouping to the STATS clause and switch fetchType to FETCH to alert per-service instead of on one cluster-wide total.
  • Chain io.kestra.plugin.elasticsearch.Search in the alert branch to pull a sample of the actual error documents for richer Slack context beyond the bare count.
  • Set async: true on query_error_count for a query against a much larger lookback window that would otherwise risk timing out on the synchronous endpoint.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.