使用 API 管理提醒和通知

您正在查看的是 Apigee Edge 文档。
转到 Apigee X 文档
信息

以下部分介绍了如何使用 API 管理提醒。

如需了解 Alerts API 的详细信息,请参阅 Alerts API

使用 API 设置提醒和通知

通过向以下资源发出 POST 请求来设置提醒和通知:https://apimonitoring.enterprise.apigee.com/alerts

以下部分提供了使用 API 设置提醒和通知的示例:

使用 API 为 API 代理设置 5xx 状态代码提醒

以下示例显示了如何为任何地区的 prod 环境中的酒店 API 代理设置当 5xx 状态代码在 10 分钟内以大于每秒 100 项事务 (TPS) 的速率出现时会触发的提醒。触发提醒时,系统会向指定的电子邮件地址发送通知。

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"5xx Alert",
     "description":"My 5xx alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{
            "org":"myorg", 
            "env":"prod", 
            "proxy":"hotels",
            "region":"ANY",
            "statusCode":"5xx"
        },
        "metric":"tps",
        "threshold":100,
        "durationSeconds":600,
        "comparator":">"
     }
     ],
     "notifications":[{ 
         "channel":"email", 
         "destination":"ops@acme.com"
     }],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
    }'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 为 API 代理设置第 95 百分位延迟提醒

以下示例显示如何为任何地区的 prod 环境中的酒店 API 代理设置如果第 95 百分位的总响应延迟时间在持续 5 分钟时间内超过 100 毫秒时会触发的提醒。

如果触发提醒,系统会向指定的 Webhook 发送一条通知。

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"My Alert",
     "description":"My first alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{
            "org":"myorg", 
            "env":"prod",
            "proxy" : "hotels",
            "region":"ANY",
            "percentile":"95"
        },
        "metric":"totalLatency",
        "threshold":100,
        "durationSeconds":300,
        "comparator":">"
     }
     ],
     "notifications":[{ "channel":"webhook", "destination":"https://apigee.com/test-webhook"}],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
}'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 为所有 API 代理设置 404(找不到应用)状态代码提醒

以下示例显示如何为任何地区的 prod 环境中的 API 代理中设置当 HTTP 404 状态代码以大于 10% 的速率出现并持续 5 分钟时会触发的提醒。

触发提醒时,系统会向指定的 Slack 频道发送通知。

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"404 Application Not Found Alert",
     "description":"My 404 alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{"org":"myorg", 
            "env":"prod", 
            "proxy":"ALL",
            "region":"ANY",
            "statusCode":"404"},
        "metric":"rate",
        "threshold":0.05,
        "durationSeconds":300,
        "comparator":">"
     }],
     "notifications":[{ "channel":"slack", "destination":"https://hooks.slack.com/services/T00000000/B00000000/XXXXX"}],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
}'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 为 API 设置 API 代理计数提醒

下面提供了一个示例,说明如何设置当任何地区的 5xx 代码计数超过 200 且持续 5 分钟时触发的提醒。

在此示例中,这些 API 被捕获到关键 API 代理集合中(使用 UUID aeff4394-86b7-11e8-83d7-42010a840040)。要获取集合的 UUID,请参阅使用 API 查看所有集合

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"Proxy Count Alert",
     "description":"My proxy count alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{
            "collection":"aeff4394-86b7-11e8-83d7-42010a840040",
            "org":"myorg", 
            "env":"prod",
            "proxy" : "ANY",
            "region":"ANY",
            "statusCode":"5xx"
        },
        "metric":"count",
        "threshold":200,
        "durationSeconds":300,
        "comparator":">"
     }
     ],
     "notifications":[{ 
         "channel":"email", 
         "destination":"ops@acme.com"
     }],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
    }'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 为目标服务设置错误率提醒

下面提供了一个示例,说明如何设置任何地区的目标服务的 500 代码速率超过 10%且持续 1 小时时触发的提醒。

在此示例中,目标服务被捕获到关键目标集合中(使用 UUID aeff4394-86b7-11e8-83d7-42010a840040)。要获取集合的 UUID,请参阅使用 API 查看所有集合

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"Error rate Alert",
     "description":"My error rate alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{
            "collection":"aeff4394-86b7-11e8-83d7-42010a840040",
            "org":"myorg", 
            "env":"prod",
            "proxy" : "ANY",
            "region":"ANY",
            "statusCode":"500"
        },
        "metric":"rate",
        "threshold":0.1,
        "durationSeconds":3600,
        "comparator":">"
     }
     ],
     "notifications":[{ 
         "channel":"email", 
         "destination":"ops@acme.com"
     }],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
    }'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 为服务标注政策设置错误率提醒

以下示例说明如何为任何地区设置当 ServiceCallout 政策指定的服务的 500 代码速率超过 10% 并持续 1 小时时会触发的提醒。

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"Error rate Alert",
     "description":"My error rate alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions":{
            "target": "sc://docstore-api",
            "org":"myorg", 
            "env":"prod",
            "proxy" : "ANY",
            "region":"ANY",
            "statusCode":"500"
        },
        "metric":"rate",
        "threshold":0.1,
        "durationSeconds":3600,
        "comparator":">"
     }
     ],
     "notifications":[{ 
         "channel":"email", 
         "destination":"ops@acme.com"
     }],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
    }'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

使用 API 设置政策故障代码提醒

以下示例显示如何设置在满足以下条件之一时触发的提醒:

  • 对于任何地区的 API 生产环境,SpikeArrestViolation 故障代码数量超过 10 个并持续 5 分钟。
  • 对于任何地区的 prod 环境中的 API,所有 API 协议故障代码计数大于 3% 并持续 5 分钟。

在此示例中,这些 API 被捕获到关键 API 代理集合中(使用 UUID aeff4394-86b7-11e8-83d7-42010a840040)。要获取集合的 UUID,请参阅使用 API 查看所有集合

触发提醒时,系统会向指定的 PagerDuty 代码发送通知。

curl 'https://apimonitoring.enterprise.apigee.com/alerts' \
  -X POST \
  -H 'Accept: application/json, text/plain, */*' -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{
     "organization":"myorg",
     "name":"My Fault Code Alert",
     "description":"My fault code alert",
     "environment":"prod",
     "enabled":true,
     "conditions":[
     {
        "description":"",
        "dimensions": { 
            "collection":"aeff4394-86b7-11e8-83d7-42010a840040",
            "org":"myorg",
            "env":"prod",
            "proxy":"ANY",
            "region":"ANY",
            "faultCodeCategory":"Traffic Mgmt Policy",
            "faultCodeSubCategory":"Spike Arrest",
            "faultCodeName":"SpikeArrest Violation"
        },
        "metric":"count,
        "threshold":10,
        "durationSeconds":300,
        "comparator":">"
     },
     {
       "description":"",
       "dimensions": { 
           "collection":"aeff4394-86b7-11e8-83d7-42010a840040",
           "org":"myorg",
           "env":"prod",
           "proxy":"ANY",
           "region":"ANY",
           "faultCodeCategory":"API Protocol",
           "faultCodeSubCategory":"ALL"
       },
       "metric":"rate",
       "threshold":0.03,
       "durationSeconds":300,
       "comparator":">"
     }   
     ],
     "notifications":[{ "channel":"pagerduty", "destination":"abcd1234efgh56789"}],
     "playbook":"http://acme.com/myplaybook.html",
     "throttleIntervalSeconds":3600,
     "reportEnabled":true
}'

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

查看提醒和通知

以下各部分提供了有关如何使用 API 查看提醒定义和有关触发提醒的信息的示例:

查看某一组织的所有提醒定义

向以下 API 发出 GET 请求,以查看所有提醒和通知定义:https://apimonitoring.enterprise.apigee.com/alerts

您必须使用 org 查询参数传递您的组织名称。

例如:

curl 'https://apimonitoring.enterprise.apigee.com/alerts?org=myorg'
    -X GET
    -H 'Accept: application/json, text/plain, */*'
    -H "Authorization: Bearer $ACCESS_TOKEN"    

提醒的 UUID 显示在响应的 uuid 字段中。您需要使用此 UUID 进行调用以确定特定于提醒定义的信息。以下提供了一个响应示例:

[ 
 {
    "uuid": "4fa49a87-3463023ea7c4",
    "name": "PublicAPI latency alert",
    "enabled": true,
    "description": "Public API Latency alerts, 90th %ile > 6secs for 5 minute window trigger this alert",
    "conditions": [
      {
        "uuid": "4fa49a87-3463023ea7c4",
        "description": "",
        "dimensions": {
          "env": "prod",
          "org": "myorg",
          "percentile": "90",
          "proxy": "PublicAPI",
          "region": "ANY"
        },
        "metric": "totalLatency",
        "threshold": 6000,
        "durationSeconds": 300,
        "comparator": ">",
        "updatedBy": "me@foo.com"
      }
    ],
    "playbook": "PublicAPI Latency alert, setup to go off when 90th %ile is > 4 secs for 5 minute window",
    "throttleIntervalSeconds": 3600,
    "self": "/alerts/4fa49a87-3463023ea7c4",
    "feed": "/o/myorg/events/4fa49a87-3463023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "notifications": [
      {
        "channel": "email",
        "destination": "me@foo.com"
      }
    ],
    "updatedAt": "2018-07-19T18:19:31.654738Z",
    "updatedBy": "me@foo.com"
  },
  {
    "uuid": "ef1a5249-345ed3023ea7c4",
    "name": "Minty API Latency alert",
    "enabled": true,
    "description": "Minty API Latency alerts, 90th %ile > 6secs for 5 minute window trigger this alert",
    "conditions": [
      {
        "uuid": "ef1a5249-345ed3023ea7c4",
        "description": "",
        "dimensions": {
          "env": "prod",
          "org": "myorg",
          "percentile": "90",
          "proxy": "minty",
          "region": "ANY"
        },
        "metric": "totalLatency",
        "threshold": 6000,
        "durationSeconds": 300,
        "comparator": ">",
        "updatedBy": "me@foo.com"
      }
    ],
    "playbook": "Minty API",
    "throttleIntervalSeconds": 3600,
    "self": "/alerts/ef1a5249-345ed3023ea7c4",
    "feed": "/o/myorg/events/ef1a5249-345ed3023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "notifications": [
      {
        "channel": "email",
        "destination": "me@foo.com"
      }
    ],
    "updatedAt": "2018-07-19T18:19:33.22479Z",
    "updatedBy": "me@foo.com"
  },
 ...
]

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

查看指定提醒的定义

通过向以下资源发出 GET 请求来查看特定提醒定义:https://apimonitoring.enterprise.apigee.com/alerts/alert_uuid 其中alert_uuid指定提醒定义的 UUID。在创建提醒时获取 UUID,或使用上一部分中显示的 API 调用列出所有提醒及其关联的 UUID。

例如:

curl 'https://apimonitoring.enterprise.apigee.com/alerts/4fa49a87-3463023ea7c4'
    -X GET
    -H 'Accept: application/json, text/plain, */*'
    -H "Authorization: Bearer $ACCESS_TOKEN"    

以下提供了一个响应示例:

  {
    "uuid": "4fa49a87-3463023ea7c4",
    "name": "PublicAPI latency alert",
    "enabled": true,
    "description": "Public API Latency alerts, 90th %ile > 6secs for 5 minute window trigger this alert",
    "conditions": [
      {
        "uuid": "4fa49a87-3463023ea7c4",
        "description": "",
        "dimensions": {
          "env": "prod",
          "org": "myorg",
          "percentile": "90",
          "proxy": "PublicAPI",
          "region": "ANY"
        },
        "metric": "totalLatency",
        "threshold": 6000,
        "durationSeconds": 300,
        "comparator": ">",
        "updatedBy": "me@foo.com"
      }
    ],
    "playbook": "PublicAPI Latency alert, setup to go off when 90th %ile is > 4 secs for 5 minute window",
    "throttleIntervalSeconds": 3600,
    "self": "/alerts/4fa49a87-3463023ea7c4",
    "feed": "/o/myorg/events/4fa49a87-3463023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "notifications": [
      {
        "channel": "email",
        "destination": "me@foo.com"
      }
    ],
    "updatedAt": "2018-07-19T18:19:31.654738Z",
    "updatedBy": "me@foo.com"
  }

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

查看某一组织所有触发提醒的历史记录

通过向以下资源发出 GET 请求来查看某个组织所有触发提醒的历史记录:https://apimonitoring.enterprise.apigee.com/metrics/alerthistory

您必须使用 org 查询参数传递您的组织名称。您可以选择指定一个时间段,用于搜索已触发的提醒。默认为返回过去一小时内触发的所有提醒。

例如:

curl 'https://apimonitoring.enterprise.apigee.com/metrics/alerthistory?org=myorg'
    -X GET
    -H 'Accept: application/json, text/plain, */*'
    -H "Authorization: Bearer $ACCESS_TOKEN"    

响应包含在请求的时间段内所有触发提醒的数组。在响应正文中,id 字段指定所触发提醒的 UUID,shared_id 字段指定与触发提醒相关联的提醒定义的 UUID。

以下提供了一个响应示例。

[
  {
    "id": "80cbe560-f6e0-475c6f7ed2d",
    "shared_id": "4fa49a87-3463023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "name": "PublicAPI latency alert",
    "type": "Alert",
    "source": "null/current",
    "raw_payload": "{\"reportUUID\":\"\",\"reportEnabled\":false,\"organization\":\"myorg\",\"name\":\"emgmt-api 404\",\"self\":\"/alerts/4fa49a87-3463023ea7c4\",\"description\":\"go/apigee-extensions-playbook\",\"conditions\":[
  {\"comparator\":\">\",\"metric\":\"rate\",\"durationSeconds\":300,\"name\":\"PublicAPI latency alert\",\"description\":\"\",\"threshold\":0.05,\"dimensions\":{\"proxy\":\"emgmt-api\",\"org\":\"myorg\",\"env\":\"prod\",\"region\":\"any\",\"statusCode\":\"404\"}}],\"uuid\":\"4fa49a87-3463023ea7c4\",\"playbook\":\"go/apigee-extensions-playbook\"}",
    "time": "2019-03-25T15:30:18Z"
  },
  {
    "id": "8131d740-6680-45b9c72c3",
    "shared_id": "1a64885b-f9-42010a850039",
    "organization": "apigee-pinpoint",
    "environment": "prod",
    "name": "Demo 5xx alert",
    "type": "Alert",
    "source": "null/current",
    "raw_payload": "{\"reportUUID\":\"\",\"reportEnabled\":false,\"organization\":\"myorg\",\"name\":\"Demo 5xx alert\",\"self\":\"/alerts/1a64885b-f9-42010a850039\",\"description\":\"Demo 5xx alert\",\"conditions\":[
  {\"comparator\":\">\",\"metric\":\"rate\",\"durationSeconds\":300,\"name\":\"Demo 5xx alert\",\"description\":\"\",\"threshold\":0.4,\"dimensions\":{\"proxy\":\"ALL\",\"org\":\"myorg\",\"env\":\"prod\",\"region\":\"any\",\"statusCode\":\"5xx\"}}],\"uuid\":\"1a64885b-f9-42010a850039\",\"playbook\":\"Recommended Playbook\"}",
    "time": "2019-03-25T15:57:30Z"
  },
 ...
]

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL

查看特定提醒的历史记录

通过向以下资源发出 GET 请求,查看特定提醒定义的触发提醒的历史记录:https://apimonitoring.enterprise.apigee.com/metrics/alerthistory

您必须使用 org 查询参数和提醒定义的 UUID 传递您的组织的名称。您可以选择指定一个时间段,用于搜索提醒。默认为返回过去一小时内触发的所有提醒。

您可以从上一部分中创建提醒定义时显示的提醒历史记录中获取提醒定义 UUID,或使用查看所有提醒定义中显示的 API 调用获取提醒定义 UUID。

例如:

curl 'https://apimonitoring.enterprise.apigee.com/metrics/alerthistory?org=myorg&alertId=4fa49a87-3463023ea7c4'
    -X GET
    -H 'Accept: application/json, text/plain, */*'
    -H "Authorization: Bearer $ACCESS_TOKEN"    

响应包含一个数组,其中包含在请求时间段内针对指定提醒定义 UUID 触发的所有提醒。在响应正文中,id 字段指定已触发提醒的 UUID,shared_id 字段指定与触发的提醒相关联的提醒定义的 UUID。

以下提供了一个响应示例。

[
  {
    "id": "80cbe560-f6e0-475c6f7ed2d",
    "shared_id": "4fa49a87-3463023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "name": "PublicAPI latency alert",
    "type": "Alert",
    "source": "null/current",
    "raw_payload": "{\"reportUUID\":\"\",\"reportEnabled\":false,\"organization\":\"myorg\",\"name\":\"emgmt-api 404\",\"self\":\"/alerts/4fa49a87-3463023ea7c4\",\"description\":\"go/apigee-extensions-playbook\",\"conditions\":[
  {\"comparator\":\">\",\"metric\":\"rate\",\"durationSeconds\":300,\"name\":\"PublicAPI latency alert\",\"description\":\"\",\"threshold\":0.05,\"dimensions\":{\"proxy\":\"emgmt-api\",\"org\":\"myorg\",\"env\":\"prod\",\"region\":\"any\",\"statusCode\":\"404\"}}],\"uuid\":\"4fa49a87-3463023ea7c4\",\"playbook\":\"go/apigee-extensions-playbook\"}",
    "time": "2019-03-25T15:30:18Z"
  },
  {
    "id": "9fc442d5-d607-40ef118c4e7",
    "shared_id": "4fa49a87-3463023ea7c4",
    "organization": "myorg",
    "environment": "prod",
    "name": "PublicAPI latency alert",
    "type": "Alert",
    "source": "null/current",
    "raw_payload": "{\"reportUUID\":\"\",\"reportEnabled\":false,\"organization\":\"myorg\",\"name\":\"emgmt-api 404\",\"self\":\"/alerts/4fa49a87-3463023ea7c4\",\"description\":\"go/apigee-extensions-playbook\",\"conditions\":[{\"comparator\":\">\",\"metric\":\"rate\",\"durationSeconds\":300,\"name\":\"PublicAPI latency alert\",\"description\":\"\",\"threshold\":0.05,\"dimensions\":{\"proxy\":\"emgmt-api\",\"org\":\"myorg\",\"env\":\"prod\",\"region\":\"any\",\"statusCode\":\"404\"}}],\"uuid\":\"4fa49a87-3463023ea7c4\",\"playbook\":\"go/apigee-extensions-playbook\"}",
    "time": "2019-03-25T15:17:55Z"
  },
 ...
]

按照获取 OAuth 2.0 访问令牌中的说明,将 $ACCESS_TOKEN 设置为您的 OAuth 2.0 访问令牌。

如需了解此示例中使用的 cURL 选项,请参阅使用 cURL