---
updatedAt: 2025-05-14T20:38:36.000Z
agentTools:
  projectIndex: https://docs.chaossearch.io/llms.txt
---

# Define Triggers for a Monitor

Add one or more triggers to a monitor to detect when a condition occurs

A monitor requires at least one trigger to run. A monitor could have up to 10 triggers to check for different conditions and take different actions. For example, you could define one trigger to send a message to a destination like a Slack channel when a specific error message is detected once in the last 15 minutes of indexed data for a view. You might have another trigger that will page an administrator if that message is detected more than 5 times over 30 minutes of indexed data.

Trigger conditions are scripts that return true or false to indicate if a trigger should be activated to send an alert. These scripts are given the extraction query response as JSON handed down in the `ctx` variable. Note that ChaosSearch supports a variety of [common painless expression patterns](https://docs.chaossearch.io/docs/define-triggers-for-monitor#trigger-condition-patterns) for the typical trigger conditions and checks, but not all of the  `painless` syntax is supported.

To define a trigger:

1. When you are creating or editing a monitor, scroll to the bottom of the window to the **Triggers** section.

![](https://files.readme.io/d48b1d3-triggers_section.jpg "triggers_section.jpg")

2. Click **Add trigger** to open a panel with the fields for a new trigger.

![](https://files.readme.io/46e1c1b-trigger_def_pt1.jpg "trigger_def_pt1.jpg")

3. In the **Trigger name** field, type a name for the trigger.
4. In the **Severity level** field, specify an alert severity level from **1 (Highest)** to **5 (Lowest)**.
5. In the **Trigger condition** field, specify the conditions that must be met for the trigger to fire.  The default condition is that the monitor query must return at least one hit/result. You can define a custom condition using `painless` style scripting. Click **Info** for more information about the scripting variables, and also see [Trigger Condition Patterns](#trigger-condition-patterns) below for some supported patterns.
6. In the **Actions** panel, define one or more actions to take when a trigger condition is met.

> 👍 An action is typical, but not required.
>
> For example, you might skip an action to stage an alert, or to monitor conditions passively from the **Alerting** page, rather than use active notifications to destinations. It can be helpful for testing monitors, especially when a destination is not ready for use at the time.

<br />

<Image align="center" src="https://files.readme.io/7d676aa-trigger_def_part2.jpg" />

7. In the **Action name** field, type a name for the action that you are adding.
8. In the **Destinations** list, select an [alert destination](https://docs.chaossearch.io/docs/alert-destinations) to which the alert will be sent.
9. In the **Message subject** field, type a clear message that will be sent in the alert.
10. In the **Message** field, update the template content as needed to provide helpful information for the alert consumer about the monitoring condition and the trigger detected.
11. Click **Preview message** to display a sample event message based on the information that you just specified. Note that the preview displayed in the web browser could diverge from the message as rendered by the service. It is helpful to use the **Send test message** feature to see a sample message processed by the service to confirm that the message renders as intended.
12. Under **Throttling**, you can select **Enable action throttling** if you want to limit the number of notifications you receive within a given time frame. Another field appears where you can specify the throttle timeframe of 1 to 1440 minutes.\
    If a monitor checks a trigger condition every minute, you could receive one notification per minute. If you set action throttling to 60 minutes, you receive no more than one notification per hour, even if the trigger condition is met dozens of times during that hour.
13. You can choose to add another action if desired, for up to 10 actions on the monitor. For example, you might have one trigger to send a low priority alert if data crosses an early-warning threshold, and another trigger for a higher priority alert when data crosses a more severe threshold level.
14. When you finish specifying the trigger(s) for the monitor, save the monitor by clicking **Create** (or **Update**) at the bottom of the window.

# Trigger Condition Patterns

The following patterns are some examples of typical evaluations that are supported in trigger conditions. They can be combined to form condition expressions in the `painless` script.

> 📘 Use care with complex condition expressions.
>
> If you combine multiple patterns into evaluation expressions, note that there is no *enforced* limit on the number of terms that can be used in an expression.  However, performance degradation has been observed when 6 or more patterns are contained in an expression. Note that ChaosSearch support is limited to single-line scripts, so an expression must be composed on one line.

<Table align={["left","left","left"]}>
  <thead>
    <tr>
      <th>
        Pattern
      </th>

      <th>
        Summary
      </th>

      <th>
        Symbol
      </th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        `"string"`
      </td>

      <td>
        A raw string value
      </td>

      <td>
        string
      </td>
    </tr>

    <tr>
      <td>
        9999.0
      </td>

      <td>
        A raw number
      </td>

      <td>
        number
      </td>
    </tr>

    <tr>
      <td>
        `\+,-,/,\*`
      </td>

      <td>
        An arithmetic operation
      </td>

      <td>
        op
      </td>
    </tr>

    <tr>
      <td>
        `> ,>=,==,\<,\<=`
      </td>

      <td>
        Arithmetic signs to compare two numbers
      </td>

      <td>
        sign
      </td>
    </tr>

    <tr>
      <td>
        `&&,\||`
      </td>

      <td>
        AND OR boolean values
      </td>

      <td>
        and/or
      </td>
    </tr>

    <tr>
      <td>
        `()`
      </td>

      <td>
        Precedence of expression evaluations
      </td>

      <td>

      </td>
    </tr>

    <tr>
      <td>
        `ctx.results[0].hits.total.value`
      </td>

      <td>
        How to access the total number of results
      </td>

      <td>
        `mt_group_doc_count`
      </td>
    </tr>

    <tr>
      <td>
        `ctx.results[0].hits.hits[0]._source.NUMKEY`
      </td>

      <td>
        `NUMKEY` is the name of a number field spelled out even if that field contains dots or \[] square brackets.
      </td>

      <td>
        `rowaccess`
      </td>
    </tr>

    <tr>
      <td>
        `ctx.results[0].hits.hits[0]._source.STRKEY`
      </td>

      <td>
        `STRKEY` is the key to a string field. The `hits` array is accessed at the "zero" index. In painless, this matches to the first result in the array. 
      </td>

      <td>
        `mt_group_agg`
      </td>
    </tr>

    <tr>
      <td>
        `ctx.results[0].aggregations.MTGROUPAGG.value`
      </td>

      <td>
        How to access an aggregation with an “empty grouping” that is an ElasticSearch aggregation and does not separate values into multiple buckets. Specifically, sum's, average's, min's, and max's.
      </td>

      <td>
        `mt_group_agg`
      </td>
    </tr>

    <tr>
      <td>
        ```
        ctx.results[0].aggregations.GROUPING1.buckets[0]
        ...GROUPINGK.buckets[0].doc_count
        ```
      </td>

      <td>
        How to access the `doc_count` of an ElasticSearch aggregation named `GROUPING` that creates buckets. Specifically (date) histogram and terms aggregations. 
      </td>

      <td>
        `group_doc_count`
      </td>
    </tr>

    <tr>
      <td>
        ```
        ctx.results[0].aggregations.GROUPING1.buckets[0]
        ...GROUPINGK.buckets[0].key
        ```
      </td>

      <td>
        How to access the key of a grouping aggregation (with any level of nesting starting at GROUPING1 to GROUPINGK).
      </td>

      <td>
        `group_key`
      </td>
    </tr>

    <tr>
      <td>
        ```
        ctx.results[0].aggregations.GROUPING1.buckets[0]
        ...GROUPINGK.buckets[0].
        ```
      </td>

      <td>
        How to access bucketed subaggregations where GROUPING1, GROUPING2 … GROUPINGK are the names of grouping aggregations and MTGROUPAGG is the name of the metric at the end.
      </td>

      <td>
        `group_agg`
      </td>
    </tr>

    <tr>
      <td>
        `return true`
      </td>

      <td>
        A trigger condition that’s always true so long as there are some results.
      </td>

      <td>
        true
      </td>
    </tr>

    <tr>
      <td>
        `return false`
      </td>

      <td>
        Always false
      </td>

      <td>
        false
      </td>
    </tr>

    <tr>
      <td>
        `mt_group_doc_count op number sign number`\
        `number op mt_group_doc_count sign number`
      </td>

      <td>
        Arithmetic on the total number of results compared to a number.
      </td>

      <td>
        `mt_group_doc_count_cmp`
      </td>
    </tr>

    <tr>
      <td>
        `rowaccess op number sign number`\
        `number op rowaccess sign number`\
        `rowaccess sign number`
      </td>

      <td>
        Row result access to number fields compared to another number potentially with arithmetic.
      </td>

      <td>
        `rowaccess_cmp`
      </td>
    </tr>

    <tr>
      <td>
        `mt_group_agg`\
        `group_doc_count op number`\
        `group_key op number`\
        `group_agg op number`
      </td>

      <td>
        Arithmetic on any aggregated value.
      </td>

      <td>
        `agg_arithmetic`
      </td>
    </tr>

    <tr>
      <td>
        `strrowaccess == string`
      </td>

      <td>
        String comparison to row results.
      </td>

      <td>
        `strrowaccess_cmp`
      </td>
    </tr>

    <tr>
      <td>
        `rowaccess op rowaccess sign number`
      </td>

      <td>
        Arithmetic with two different fields in row access.
      </td>

      <td>
        `rowaccess_arithmetic`
      </td>
    </tr>

    <tr>
      <td>
        `rowaccess_cmp,strrowaccess_cmp and/or rowaccess_cmp,strrowaccess`
      </td>

      <td>
        Combining two row access comparisons with an and/or.
      </td>

      <td>

      </td>
    </tr>

    <tr>
      <td>
        `group_doc_count sign number`\
        `group_key op number`\
        `mt_group_agg sign number`\
        `group_agg sign number`\
        `agg_arithmetic sign number`
      </td>

      <td>
        A comparison of an aggregated value to some number. 
      </td>

      <td>
        `agg_cmp`
      </td>
    </tr>

    <tr>
      <td>
        `group_agg,group_doc_count op group_agg,group_doc_count sign number`
      </td>

      <td>
        Arithmetic with two different aggregated values. IMPORTANT These aggregations have to be at the SAME level of nesting.
      </td>

      <td>
        `same_agg_plan_arithmetic`
      </td>
    </tr>

    <tr>
      <td>
        `simple_agg_cmp and/or simple_agg_cmp`
      </td>

      <td>
        Combining two simple aggregation comparisons. IMPORTANT These aggregations have to be at the SAME level of nesting.
      </td>

      <td>
        `same_agg_plan_andor`
      </td>
    </tr>
  </tbody>
</Table>

## Iterating over Result Collections

ChaosSearch includes a custom `painless` language extension called `forall` that supports the ability to  iterate (or loop) over collections. The extension uses the following syntax style:

`forall (i in ctx.results[0].aggregations.datehistogramagg.buckets) { i.doc_count > 0 }`

In this example, if there is a collection of buckets from an aggregation such as `ctx.results[0].aggregations.datehistogramagg.buckets`, each bucket has a `doc_count` value, and you evaluate them using an expression—in this example, whether they all exceed a value of `0`. The body of the expression can include the common `painless` comparison types that are supported.

> ❗️ Other `painless` looping methods are not supported.
>
> The ChaosSearch implementation is a custom extension for iterating over collections. Painless includes  some other looping mechanisms which are not expressions, and thus are not supported for use in ChaosSearch monitoring scripts.