使用 API 管理快訊和通知

您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件
info

以下章節說明如何使用 API 管理快訊。

如要進一步瞭解 Alerts API,請參閱這篇文章

使用 API 設定快訊和通知

對下列資源發出 POST 要求,即可設定快訊和通知: https://apimonitoring.enterprise.apigee.com/alerts

以下各節提供使用 API 設定快訊和通知的範例:

使用 API 為 API Proxy 設定 5xx 狀態碼快訊

以下範例說明如何設定快訊,當任何區域的 prod 環境中,飯店 API Proxy 的 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 Proxy 設定第 95 個百分位數延遲時間警報

以下範例說明如何設定警告,如果任何區域的 prod 環境中,飯店 API Proxy 的第 95 個百分位數總回應延遲時間超過 100 毫秒,且持續 5 分鐘,就會觸發警告。

如果觸發快訊,系統會將通知傳送至指定的 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 Proxy 設定「404 (找不到應用程式)」狀態碼快訊

以下範例說明如何設定快訊,當任何區域的 prod 環境中,所有 API Proxy 的 HTTP 404 狀態碼發生率在 5 分鐘內超過 10% 時,就會觸發快訊。

快訊觸發時,系統會將通知傳送至指定的 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 Proxy 數量快訊

以下範例說明如何設定快訊,當任何區域的 API 5xx 程式碼計數在 5 分鐘內超過 200 時,就會觸發快訊。

在本範例中,API 會擷取至「Critical API Proxies」集合 (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 狀態碼率在 1 小時內超過 10% 時,就會觸發快訊。

在本範例中,目標服務會擷取至「重要目標」集合 (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 設定政策錯誤代碼快訊

以下範例說明如何設定警報,在符合下列任一條件時觸發警報:

  • SpikeArrestViolation 任何區域的正式環境中,API 的錯誤代碼計數在 5 分鐘內超過 10 個。
  • 任何區域的正式版環境中,API 協議錯誤代碼的計數在 5 分鐘內超過 3%。

在本範例中,API 會擷取至「Critical API Proxies」集合 (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 呼叫取得。

例如:

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」。