Skip to content

condition()

Sets the condition the watcher evaluates after fetching its input. If the condition is false, actions are skipped.

Signature

condition(spec: Record<string, ComparisonOperator> | { script: string }): void

Comparison-based condition

Pass an object whose keys are ctx.payload paths and whose values are comparison operators.

condition({ 'logs.hits.total': gt(0) })

Multiple keys are ANDed together:

condition({
'logs.hits.total': gt(0),
'errors.hits.total': gte(5),
})

Resulting JSON

{
"condition": {
"compare": {
"ctx.payload.logs.hits.total": { "gt": 0 }
}
}
}

Script-based condition

Pass { script: '<painless code>' } for more complex logic:

condition({
script: 'return ctx.payload.logs.hits.total > ctx.payload.threshold;'
})

To reference a Groovy file instead:

condition({ script: script('./condition.groovy') })

Resulting JSON

{
"condition": {
"script": {
"id": "my-group-my-alert-condition"
}
}
}

Examples

Fire if any log hits found

condition({ 'logs.hits.total': gt(0) })

Fire if error count exceeds threshold

condition({ 'errors.hits.total': gte(10) })

Always fire (useful for testing)

Use module.exports directly with "always": {} if you want an always-true condition, since condition() requires a comparison or script.

module.exports = {
// ...
condition: { always: {} },
// ...
};