View a markdown version of this page

为 Amazon OpenSearch 无服务器配置警报 - 亚马逊 OpenSearch 服务
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为 Amazon OpenSearch 无服务器配置警报

Alerting 会按照定义的时间表自动监控您的 OpenSearch 数据,并在满足触发条件时通知您。监控器是一项针对您的数据运行查询、评估触发条件并生成警报的作业。触发器触发时,监视器会运行通过配置的渠道发送通知的操作。有关监视器、触发器和操作的概念性概述,请参阅 OpenSearch 网站上的警报。

无服务器警报使用 Amazon OpenSearch 无服务器集合作为监控器的数据源。您可以通过 OpenSearch UI 创建和管理显示器。每个监视器都是您在其中创建它的 OpenSearch UI 工作区的资源。本页介绍无服务器集合特有的先决条件、权限和注意事项。它还记录了您用来以编程方式管理监视器的工作空间范围的 API 操作。

重要

监视器是 OpenSearch UI 工作空间资源。因此,无服务器集合的警报 API 操作与下的开源警报 API 操作不同。_plugins/_alerting使用中的工作空间范围的操作。为无服务器集合的 API 操作发出警报

先决条件

选择用于警报的收集类型

我们建议您将警报与时间序列集合一起使用。无服务器集合上的监视器必须引用具体的索引名称——不支持索引模式、别名和通配符表达式。时间序列集合会自动管理受监控索引的数据保留期,因此您不必构建自定义保留流程。

请注意以下事项:

  • 仅限具体索引名称-监视器必须引用具体的索引名称。不支持索引模式(例如logs-*)、别名和以逗号分隔的多个索引列表。

  • 自动数据保留-时间序列集合为您管理受监控索引中数据的生命周期。这与警报搭配得很好,因为不受限制地增长的受监控索引会要求您自己管理保留率。

  • 搜索集合-搜索集合也被接受为监控数据源,但它们不为您管理数据保留。

有关集合类型的更多信息,请参阅选择集合类型。

配置警报权限

监视器作为后台作业对你的收藏集运行。以下收集数据访问策略授予警报所需的权限。placeholder values用您的具体信息替换。有关更多信息,请参阅 受支持的策略权限。

{ "Rules": [ { "Resource": [ "collection/collection_name" ], "Permission": [ "aoss:CreateCollectionItems", "aoss:DeleteCollectionItems", "aoss:UpdateCollectionItems", "aoss:DescribeCollectionItems" ], "ResourceType": "collection" }, { "Resource": [ "index/collection_name/*" ], "Permission": [ "aoss:CreateIndex", "aoss:DeleteIndex", "aoss:UpdateIndex", "aoss:DescribeIndex", "aoss:ReadDocument", "aoss:WriteDocument", "aoss:DelegateAccess" ], "ResourceType": "index" } ], "Principal": [ "arn:aws:iam::account_id:role/role_name" ], "Description": "Alerting access policy for collection_name" }
  • 收集权限 (aoss:CreateCollectionItems、aoss:DeleteCollectionItems、aoss:UpdateCollectionItems、aoss:DescribeCollectionItems)-允许警报任务解析集合并对其进行操作。

  • 索引权限(aoss:CreateIndex、aoss:DeleteIndex、aoss:UpdateIndex、aoss:DescribeIndex、aoss:ReadDocument、aoss:WriteDocument)-允许监视器读取受监控的索引。它们还允许监控器创建和写入警报插件管理的内部警报历史索引。

  • aoss:DelegateAccess— 允许提醒后台作业代表您访问馆藏。

重要

aoss:DelegateAccess允许警报后台作业针对集合运行,并aoss:ReadDocument允许监控执行查询受监视的索引。您可以将索引资源的范围限定为特定的索引,而不是index/collection_name/*。在这种情况下,请涵盖监视器引用的每个索引以及警报插件写入的内部警报历史索引。

在 OpenSearch 用户界面工作区中,对警报功能的访问权限遵循工作区协作者权限(只读或管理员)。 read/write Read-only合作者可以查看监视器、警报和通知渠道。 Read/write 管理员协作者可以创建、更新、启用、禁用和删除监视器,还可以确认和静音警报。

支持的显示器类型

无服务器集合支持以下监视器类型:

  • Per-query 监视器-运行单个查询并根据聚合结果评估触发条件。如果要针对单个聚合指标(例如平均响应延迟时间超过阈值)发出警报,请使用按查询监视器。

  • Per-bucket 监视器-使用复合聚合运行查询,并针对每个存储桶评估触发条件。如果您想对数据中的各个组(例如每台超过错误率的主机)发出警报,请使用每桶监控器。

  • PPL 监视器 — 运行管道处理语言 (PPL) 查询并根据结果评估触发条件。如果要用管道查询语言表达复杂的筛选、转换或统计逻辑,请使用 PPL 监视器。有关 PPL 语法,请参阅网站上的 PPL。 OpenSearch

其他监视器类型(包括每文档监视器、复合监视器和每集群指标监视器)不适用于无服务器集合。

将异常检测与警报结合使用

要在异常检测器发现异常时收到通知,请创建一个用于查询检测器的自定义结果索引的监视器。无服务器集合上的异常检测器将结果写入同一个集合中的自定义结果索引,这意味着你可以使用标准的每查询或每存储桶监视器,同时开启和触发器。anomaly_grade confidence

完成以下步骤:

  1. 设置异常检测并使用自定义结果索引创建检测器。有关更多信息,请参阅 在 Amazon OpenSearch 无服务器上配置异常检测。

  2. 在 OpenSearch 用户界面中根据探测器的自定义结果索引创建监视器(例如,opensearch-ad-plugin-result-my-detector)。有关用于异常结果的触发条件的指导,请参阅网站上的异常检测。 OpenSearch

  3. 向触发器添加通知渠道,以便警报到达您选择的目的地。

注意

开源警报插件中的异常检测器监视器类型在无服务器集合中不可用,因为它从默认的异常结果索引中读取。改为使用按查询或按存储桶监视器查询自定义结果索引。

注意事项和限制

在 OpenSearch 无服务器上发出警报有以下注意事项和限制:

  • 不支持别名。监视器必须引用具体的索引名称。

  • 不支持通配符索引模式。监视器必须引用具体的索引名称(例如,logs-*不允许)。

  • Per-bucket 显示器支持单个触发器。

  • Per-document 监视器、复合监视器和每集群指标监视器不适用于无服务器集合。

  • 警报设置(例如警报历史记录保留)由服务管理,无法修改。

  • 在更新或删除显示器之前,必须将其禁用。

为无服务器集合的 API 操作发出警报

无服务器集合支持网站上开源警报 API 中的所有监控和警报操作。 OpenSearch 但是,你可以用工作空间风格的路径来称呼它们。监视器是 OpenSearch UI 工作空间资源,因此您可以向 OpenSearch UI 应用程序端点而不是集合端点发送请求。每条路径都包含一个工作空间,用于标识代表您的集合的数据源。

使用 SigV4-signing HTTP 客户端(例如awscurl在 GitHub 网站上)来调用操作。以下各节描述了请求格式,列出了操作路径并显示了代表性示例。

请求格式

请求使用以下格式:

https://application_endpoint/w/workspace_id/api/alerting/operation_path

下表描述了请求的每个部分。

组件 说明
application_endpoint 您的 OpenSearch UI 应用程序的终端节点,格式为application-application_name-application_id.region.opensearch.amazonaws.com。
workspace_id 包含显示器的 OpenSearch 用户界面工作区的 ID。你可以在 OpenSearch 界面控制台的工作区 URL /w/ 部分中找到它。
data_source_id 工作空间中代表您的集合的数据源的 ID。根据操作的不同,您可以将此值作为路径的最后一段或作为dataSourceId查询参数传递。
monitor_id 监视器的 ID,在创建或搜索监视器时在id字段中返回。

使用签名版本 4 (Sigv4) 对每个请求进行签名,使用opensearch作为服务名称。在每个请求中包含以下标头:

  • Content-Type: application/json— 发送正文的请求必填项。

  • osd-xsrf: osd-fetch— 必需的跨站请求伪造标头。

  • osd-version: 3.6.0— 您的 OpenSearch 用户界面应用程序的 OpenSearch 版本。

每个响应都使用信封。返回成功的响应{"ok": true, "response": {...}},返回失败的响应{"ok": false, "error": "message"}。

操作路径

下表列出了警报操作及其工作空间路径。所有路径都是相对于的https://application_endpoint/w/workspace_id/api/alerting。

操作 方法 路径
创建监视器 POST monitors?dataSourceId=data_source_id
买一台显示器 GET monitors/monitor_id?dataSourceId=data_source_id
更新显示器 PUT monitors/monitor_id?dataSourceId=data_source_id
删除监测仪 DELETE monitors/monitor_id?dataSourceId=data_source_id
搜索显示器 POST monitors/_search?dataSourceId=data_source_id
执行监视器 POST monitors/monitor_id/_execute?dataSourceId=data_source_id
获取警报 GET monitors/alerts?dataSourceId=data_source_id
确认警报 POST monitors/monitor_id/_acknowledge/alerts?dataSourceId=data_source_id
注意

在更新或删除显示器之前,必须将其禁用。启用监视器后,警报可能需要几分钟才会出现。

示例:创建监视器

以下示例创建了一个按查询显示器,该监视器每 5 分钟检查一次服务器日志中的错误峰值。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{ "type": "monitor", "name": "error-spike-monitor", "monitor_type": "query_level_monitor", "enabled": false, "schedule": { "period": { "interval": 5, "unit": "MINUTES" } }, "inputs": [{ "search": { "indices": ["server-logs"], "query": { "size": 0, "query": { "bool": { "filter": [ {"range": {"@timestamp": {"gte": "{{period_end}}||-5m", "lte": "{{period_end}}", "format": "epoch_millis"}}}, {"term": {"level": "ERROR"}} ] } }, "aggregations": { "error_count": {"value_count": {"field": "_id"}} } } } }], "triggers": [{ "query_level_trigger": { "name": "High error count", "severity": "1", "condition": { "script": { "source": "ctx.results[0].aggregations.error_count.value > 100", "lang": "painless" } }, "actions": [] } }] }'

响应返回id字段中的显示器 ID。

{"ok": true, "response": {"id": "a3f8c214-7b62-4e91-9d05-1c3b6f4e8a92", "name": "error-spike-monitor"}}

请注意以下特定于无服务器的要求:

  • indices必须包含具体的索引名称。不支持通配符和别名。有关更多信息,请参阅 选择用于警报的收集类型。

  • 该操作使显示器处于禁用状态。通过设置在更新操作中将其启用"enabled": true。

示例:执行监视器

执行操作会立即运行监控器的查询并返回结果,无需等待下一个计划间隔。在启用监控器之前,使用它来测试其查询和触发器。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors/monitor_id/_execute?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{ "ok": true, "response": { "monitor_name": "error-spike-monitor", "period_start": 1781041644000, "period_end": 1781041944000, "error": null, "input_results": { "results": [{"aggregations": {"error_count": {"value": 42}}}] }, "trigger_results": { "trigger-id": { "name": "High error count", "triggered": false, "action_results": {} } } } }

搜索操作对工作区中的监视器配置运行查询。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors/_search?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{"query": {"match_all": {}}, "size": 1}'
{ "ok": true, "response": { "totalMonitors": 8, "monitors": [{ "id": "a3f8c214-7b62-4e91-9d05-1c3b6f4e8a92", "name": "error-spike-monitor", "monitor_type": "query_level_monitor", "enabled": true, "schedule": {"period": {"interval": 5, "unit": "MINUTES"}}, "inputs": [{"search": {"indices": ["server-logs"], "query": {}}}], "triggers": [{"query_level_trigger": {"name": "High error count", "severity": "1"}}], "last_update_time": 1781030925933, "dataSourceId": "ef581a74-e4e6-3fab-a5b4-9bffd8d4af64" }] } }

获取警报操作会返回工作区中显示器上的所有活动警报。

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors/alerts?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{ "ok": true, "response": { "alerts": [{ "id": "d7e5b324-1f93-4a2d-bc89-6e4f1d2c3a56", "monitor_id": "a3f8c214-7b62-4e91-9d05-1c3b6f4e8a92", "monitor_name": "error-spike-monitor", "trigger_name": "High error count", "state": "ACTIVE", "severity": "1", "start_time": 1781041944000, "last_notification_time": 1781041944000 }], "totalAlerts": 1 } }

示例:确认警报

确认警报会将其从状态变为ACTIVEACKNOWLEDGED状态,从而防止在触发条件重置之前对该警报触发进一步的通知操作。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors/monitor_id/_acknowledge/alerts?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{"alerts": ["d7e5b324-1f93-4a2d-bc89-6e4f1d2c3a56"]}'
{"ok": true, "response": {"success": ["d7e5b324-1f93-4a2d-bc89-6e4f1d2c3a56"], "failed": []}}

示例:删除显示器

在删除显示器之前,请将其禁用。删除监视器并不会删除其先前生成的警报。

awscurl --service opensearch --region us-west-2 \ -X DELETE \ 'https://application_endpoint/w/workspace_id/api/alerting/monitors/monitor_id?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{"ok": true, "response": {}}