Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for printing associated queries, and sample documents in notification message. #6866

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 68 additions & 7 deletions _observing-your-data/alerting/triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,76 @@ Variable | Data type | Description
#### Other variables

Variable | Data type | Description
:--- | :--- : :---
`ctx.results` | Array | An array with one element (`ctx.results[0]`). Contains the query results. This variable is empty if the trigger was unable to retrieve results. See `ctx.error`.
:--- | :--- | : ---
`ctx.results` | Array | An array with one element (`ctx.results.0`). Contains the query results. This variable is empty if the trigger was unable to retrieve results. See `ctx.error`.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`ctx.last_update_time` | Milliseconds | Unix epoch time of when the monitor was last updated.
`ctx.periodStart` | String | Unix timestamp for the beginning of the period during which the alert was triggered. For example, if a monitor runs every 10 minutes, a period might begin at 10:40 and end at 10:50.
`ctx.periodEnd` | String | The end of the period during which the alert triggered.
`ctx.error` | String | The error message displayed if the trigger was unable to retrieve results or could not be evaluated, typically due to a compile error or null pointer exception. Null otherwise.
`ctx.alert` | Object | The current, active alert (if it exists). Includes `ctx.alert.id`, `ctx.alert.version`, and `ctx.alert.isAcknowledged`. Null if no alert is active. Only available with query-level monitors.
`ctx.dedupedAlerts` | Object | Alerts that have been triggered. OpenSearch keeps the existing alert to prevent the plugin from creating endless numbers of the same alert. Only available with bucket-level monitors.
`ctx.newAlerts` | Object | Newly created alerts. Only available with bucket-level monitors.
`ctx.completedAlerts` | Object | Alerts that are no longer ongoing. Only available with bucket-level monitors.
`bucket_keys` | String | Comma-separated list of the monitor's bucket key values. Available only for `ctx.dedupedAlerts`, `ctx.newAlerts`, and `ctx.completedAlerts`. Accessed through `ctx.dedupedAlerts[0].bucket_keys`.
`parent_bucket_path` | String | The parent bucket path of the bucket that triggered the alert. Accessed through `ctx.dedupedAlerts[0].parent_bucket_path`.
`ctx.alerts` | Array | Newly created alerts. Includes `ctx.alerts.0.finding_ids` that triggered the alert, and `ctx.alerts.0.related_doc_ids` associated with the findings. Only available with document-level monitors.
AWSHurneyt marked this conversation as resolved.
Show resolved Hide resolved
`ctx.dedupedAlerts` | Array | Alerts that have been triggered. OpenSearch keeps the existing alert to prevent the plugin from creating endless numbers of the same alert. Only available with bucket-level monitors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Alerts that have been triggered" => "Triggered alerts"? "creating endless numbers of the same alert" => "perpetually creating the same alert"?

Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`ctx.newAlerts` | Array | Newly created alerts. Only available with bucket-level monitors.
`ctx.completedAlerts` | Array | Alerts that are no longer ongoing. Only available with bucket-level monitors.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`bucket_keys` | String | Comma-separated list of the monitor's bucket key values. Available only for `ctx.dedupedAlerts`, `ctx.newAlerts`, and `ctx.completedAlerts`. Accessed through `ctx.dedupedAlerts.0.bucket_keys`.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`parent_bucket_path` | String | The parent bucket path of the bucket that triggered the alert. Accessed through `ctx.dedupedAlerts.0.parent_bucket_path`.
`associated_queries` | Array | An array of document-level monitor queries that triggered the creation of the finding associated with the alert. Only available with document-level monitors. Accessed through `ctx.alerts.0.associated_queries`.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`sample_documents` | Array | An array of sample documents that matched the monitor query. Only available with bucket-level, and document-level monitors. Accessed through `ctx.newAlerts.0.sample_documents`, and `ctx.alerts.0.sample_documents`, respectively.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved

#### `associated_queries`, and `sample_documents` variables
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved

Per bucket, and per document monitors support printing sample documents in notification messages; and per document monitors support printing the list of queries that triggered the creation of the finding associated with the alert. Each new alert that's generated during a monitor execution will be added to a list (i.e., `newAlerts` for per bucket monitors, and `alerts` for per document monitors) within the `ctx` variable. Each alert has its own list of `sample_documents`, and each per document monitor alert has its own list of `associated_queries`. The message template can be formatted to iterate through the list of alerts, and the list of `associated_queries`, and `sample_documents` for each alert.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved

To learn more about security in the Alerting plugin, see [Alerting security](https://opensearch.org/docs/latest/observing-your-data/alerting/security/).

#### Sample documents variables
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved

Variable | Data type | Description
:--- | :--- | : ---
`_index` | String | The index containing the
AWSHurneyt marked this conversation as resolved.
Show resolved Hide resolved
`_id` | String | The document ID for the document in its index.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm technical meaning is retained.

`_score` | Float | A positive 32-bit floating point number illustrating the relevance of the returned document.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`_source` | Object | The JSON payload of the sample document.

##### Mustache template example

```groovy
Alerts:
{{#ctx.alerts}}
RULES
{{#associated_queries}}
Name: {{name}}
Id: {{id}}
Tags: {{tags}}
------------------------
{{/associated_queries}}
{{/ctx.alerts}}
```

#### Associated queries variables
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved

Variable | Data type | Description
:--- | :--- | : ---
`id` | String | The ID for the document-level query.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`name` | String | The name configured for the document-level query.
Naarcha-AWS marked this conversation as resolved.
Show resolved Hide resolved
`tags` | Array | An array of tags (each of type String) configured for the document-level query.

##### Mustache template example

**Note**: The `_source` object in this example is based on the `opensearch_dashboards_sample_data_ecommerce` index that's available on OpenSearch Dashboards.
AWSHurneyt marked this conversation as resolved.
Show resolved Hide resolved

```groovy
Alerts
{{#ctx.alerts}}
Copy link
Contributor Author

@AWSHurneyt AWSHurneyt Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for documentation reviewers:
The following sentence on line 146, calls out that the variable names for the "alerts" list is newAlerts for per bucket monitors, and alerts for per document monitors.

Each new alert that's generated during a monitor execution will be added to a list (i.e., newAlerts for per bucket monitors, and alerts for per document monitors) within the ctx variable.

The example in this code block is for a per document monitor as, on line 188, the "alerts" list is accessed via {{#ctx.alerts}} instead of {{#ctx.newAlerts}}. Does that seem clear enough, or would it be helpful to update the note on line 184 to call out that the example is for a per document monitor?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets update line 184 and call it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revised.

Sample documents:
{{#sample_documents}}
Index: {{_index}}
Document ID: {{_id}}

Order date: {{_source.order_date}}
Order ID: {{_source.order_id}}
Clothing category: {{_source.category}}
-----------------
{{/sample_documents}}
{{/ctx.alerts}}
```
Loading