Actions

When a watch’s condition is met, its actions are executed unless it is being throttled. A watch can perform multiple actions. The actions are executed one at a time and each action executes independently. Any failures encountered while executing an action are recorded in the action result and in the watch history.

If no actions are defined for a watch, no actions are executed. However, a watch_record is still written to the watch history.

Actions have access to the payload in the execution context. They can use it to support their execution in any way they need. For example, the payload might serve as a model for a templated email body.

Watcher supports the following types of actions: email, webhook, index, logging, slack, and pagerduty.

Acknowledgement and throttling

During the watch execution, once the condition is met, a decision is made per configured action as to whether it should be throttled. The main purpose of action throttling is to prevent too many executions of the same action for the same watch.

For example, suppose you have a watch that detects errors in an application’s log entries. The watch is triggered every five minutes and searches for errors during the last hour. In this case, if there are errors, there is a period of time where the watch is checked and its actions are executed multiple times based on the same errors. As a result, the system administrator receives multiple notifications about the same issue, which can be annoying.

To address this issue, Watcher supports time-based throttling. You can define a throttling period as part of the action configuration to limit how often the action is executed. When you set a throttling period, Watcher prevents repeated execution of the action if it has already executed within the throttling period time frame (now - throttling period).

The following snippet shows a watch for the scenario described above - associating a throttle period with the email_administrator action:

  1. PUT _watcher/watch/error_logs_alert
  2. {
  3. "metadata" : {
  4. "color" : "red"
  5. },
  6. "trigger" : {
  7. "schedule" : {
  8. "interval" : "5m"
  9. }
  10. },
  11. "input" : {
  12. "search" : {
  13. "request" : {
  14. "indices" : "log-events",
  15. "body" : {
  16. "size" : 0,
  17. "query" : { "match" : { "status" : "error" } }
  18. }
  19. }
  20. }
  21. },
  22. "condition" : {
  23. "compare" : { "ctx.payload.hits.total" : { "gt" : 5 }}
  24. },
  25. "actions" : {
  26. "email_administrator" : {
  27. "throttle_period": "15m",
  28. "email" : {
  29. "to" : "sys.admino@host.domain",
  30. "subject" : "Encountered {{ctx.payload.hits.total}} errors",
  31. "body" : "Too many error in the system, see attached data",
  32. "attachments" : {
  33. "attached_data" : {
  34. "data" : {
  35. "format" : "json"
  36. }
  37. }
  38. },
  39. "priority" : "high"
  40. }
  41. }
  42. }
  43. }

There will be at least 15 minutes between subsequent email_administrator action executions.

See Email action for more information.

You can also define a throttle period at the watch level. The watch-level throttle period serves as the default throttle period for all of the actions defined in the watch:

  1. PUT _watcher/watch/log_event_watch
  2. {
  3. "trigger" : {
  4. "schedule" : { "interval" : "5m" }
  5. },
  6. "input" : {
  7. "search" : {
  8. "request" : {
  9. "indices" : "log-events",
  10. "body" : {
  11. "size" : 0,
  12. "query" : { "match" : { "status" : "error" } }
  13. }
  14. }
  15. }
  16. },
  17. "condition" : {
  18. "compare" : { "ctx.payload.hits.total" : { "gt" : 5 }}
  19. },
  20. "throttle_period" : "15m",
  21. "actions" : {
  22. "email_administrator" : {
  23. "email" : {
  24. "to" : "sys.admino@host.domain",
  25. "subject" : "Encountered {{ctx.payload.hits.total}} errors",
  26. "body" : "Too many error in the system, see attached data",
  27. "attachments" : {
  28. "attached_data" : {
  29. "data" : {
  30. "format" : "json"
  31. }
  32. }
  33. },
  34. "priority" : "high"
  35. }
  36. },
  37. "notify_pager" : {
  38. "webhook" : {
  39. "method" : "POST",
  40. "host" : "pager.service.domain",
  41. "port" : 1234,
  42. "path" : "/{{watch_id}}",
  43. "body" : "Encountered {{ctx.payload.hits.total}} errors"
  44. }
  45. }
  46. }
  47. }

There will be at least 15 minutes between subsequent action executions (applies to both email_administrator and notify_pager actions)

If you do not define a throttle period at the action or watch level, the global default throttle period is applied. Initially, this is set to 5 seconds. To change the global default, configure the xpack.watcher.execution.default_throttle_period setting in elasticsearch.yml:

  1. xpack.watcher.execution.default_throttle_period: 15m

Watcher also supports acknowledgement-based throttling. You can acknowledge a watch using the ack watch API to prevent the watch actions from being executed again while the watch condition remains true. This essentially tells Watcher “I received the notification and I’m handling it, please do not notify me about this error again”. An acknowledged watch action remains in the acked state until the watch’s condition evaluates to false. When that happens, the action’s state changes to awaits_successful_execution.

To acknowledge an action, you use the ack watch API:

  1. POST _watcher/watch/<id>/_ack/<action_ids>

Where <id> is the id of the watch and <action_ids> is a comma-separated list of the action ids you want to acknowledge. To acknowledge all actions, omit the actions parameter.

The following diagram illustrates the throttling decisions made for each action of a watch during its execution:

action throttling