Custom Metrics

Detects abnormal values of a metric that you define with a SQL query.

The Custom Metrics monitor runs a SQL query of your own that returns a metric, for example per day, and alerts you when the metric is unusual.

When to Use

  • Metrics that are not stored in a field: A metric combines several aggregates, such as a sum of positive amounts minus a sum of negative amounts, and no field stores it.
  • Metrics from a semantic layer: A key performance indicator is computed in the semantic layer of a business intelligence (BI) tool. Reproduce its SQL to monitor it.
  • Complex logic: The metric needs subqueries, window functions, or several tables, such as the share of rows processed within one hour of their creation.

When the metric is a simple aggregate of one field, use Metrics instead: it supports where clauses, group by, and incremental scans without writing SQL.

How It Works

Sifflet runs your query at each run and reads the following columns from the result:

  • Metric value (required): A numeric column.
  • Metric timing (optional): A date or timestamp column. Each row of the result is then a data point. Without a time column, Sifflet takes a snapshot of the metric at each run: one data point per run.
  • Monitoring dimension (optional): A column with the alias sifflet_monitoring_dimension. Sifflet then monitors one metric per value of this column.

Always refer to datasets with their full name (schema.dataset_name). The query must return at least one row.

The monitor fails when a data point is outside the threshold:

  • Dynamic (default): Outside the confidence band that a machine learning (ML) model predicts from the history of the metric.
  • Static: Outside the minimum and maximum you set.
  • Relative: The change compared to the previous data point, as a value or a percentage, is outside the range you set.

Example

You want to know when the daily net revenue is unusual. Create a Custom Metrics monitor on payments with the following query (here on Snowflake):

SELECT
  paid_at::date AS day,
  SUM(CASE WHEN status = 'succeeded' THEN amount WHEN status = 'refunded' THEN -amount ELSE 0 END) AS net_revenue
FROM sales.payments
GROUP BY 1;

Keep the default dynamic threshold. The net revenue is usually about 600,000 per day. On February 22, the payment provider stops sending successful payments for half of the day, and the net revenue of the day drops to 250,000. The data point of February 22 is far below the expected range, so the monitor fails.

To monitor the net revenue of each country separately, join orders and add a dimension column:

SELECT
  p.paid_at::date AS day,
  o.country AS sifflet_monitoring_dimension,
  SUM(CASE WHEN p.status = 'succeeded' THEN p.amount WHEN p.status = 'refunded' THEN -p.amount ELSE 0 END) AS net_revenue
FROM sales.payments p
JOIN sales.orders o ON o.order_id = p.order_id
GROUP BY 1, 2;

Configure the Monitor

Custom Metrics Settings

SettingDescriptionDefault
SQL queryThe query that returns the metric (see How It Works). You can generate it from a description with the Sifflet AI Assistant, and fix it with AI if the test run fails.None
Replace missing/empty values with zeroReplaces missing or empty metric values with 0.Off

Common Settings

  • Threshold: Dynamic (default), static, or relative. See Thresholds.
  • There is no scan mode, where clause, group by, or join: write them in the query.

Monitor as Code

The monitor kind is CustomMetrics. The following YAML creates the monitor of the example:

kind: CustomMetrics
sql: |
  SELECT
    paid_at::date AS day,
    SUM(CASE WHEN status = 'succeeded' THEN amount WHEN status = 'refunded' THEN -amount ELSE 0 END) AS net_revenue
  FROM sales.payments
  GROUP BY 1
ParameterRequiredDescription
sqlYesThe query that returns the metric.
emptyMeansZeroNotrue to replace missing or empty metric values with 0. Default: false.
timeWindow.offset or timeWindow.offsetPeriodsNoShifts the data points into the past, to account for data delays.

For the shared parameters, see the Parameters reference.

Related Templates

  • Metrics: Monitors an aggregate of one field, without writing SQL.
  • SQL Query: Fails when a query returns rows, instead of monitoring a metric.

Did this page help you?