使用 API 管理快訊和通知

您正在查看 Apigee Edge 說明文件。
參閱 Apigee X說明文件
資訊

以下各節將說明如何使用 API 管理警示。

如要進一步瞭解 Alerts API,請參閱 Alerts API

使用 API 設定快訊和通知

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

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

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

以下範例說明如何設定快訊,在任何區域的產品環境中,當飯店 API 代理程式發生 5xx 狀態碼的速率超過每秒 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 百分位數延遲警示

以下範例說明如何設定快訊觸發條件,如果在任何區域的產品環境中,飯店 API 代理的 95 個百分位數總回應延遲時間超過 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
}'

$ACCESS_TOKEN 設為 OAuth 2.0 存取權杖,如「取得 OAuth 2.0 存取權杖」一節所述。

如要瞭解本例中使用的 cURL 選項,請參閱「使用 cURL」。

為使用該 API 的所有 API Proxy 設定 404 (找不到應用程式) 狀態碼快訊

以下範例說明如何設定快訊,在任一區域的實際工作環境中,所有 API Proxy 發生 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
}'

$ACCESS_TOKEN 設為 OAuth 2.0 存取權杖,如「取得 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 錯誤率在 1 小時內超過 10% 時觸發快訊。

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 錯誤程式碼數量大於 10 分 5 分鐘。
  • 在任何區域的實際工作環境中,所有 API 通訊協定錯誤程式碼的計數都大於 3%,且 API 執行 5 分鐘。

在這個範例中,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"
  },
 ...
]

$ACCESS_TOKEN 設為 OAuth 2.0 存取權憑證,如「取得 OAuth 2.0 存取權憑證」一節所述。

如要瞭解本例中使用的 cURL 選項,請參閱「使用 cURL」。

查看特定快訊定義

如要查看特定的快訊定義,請對下列資源發出 GET 要求:https://apimonitoring.enterprise.apigee.com/alerts/alert_uuid,其中 alert_uuid 會指定快訊定義的 UUID。建立快訊或使用上一節顯示的 API 呼叫,即可列出所有快訊和相關 UUID,因此請取得 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"
  }

$ACCESS_TOKEN 設為 OAuth 2.0 存取權杖,如「取得 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"
  },
 ...
]

$ACCESS_TOKEN 設為 OAuth 2.0 存取權杖,如「取得 OAuth 2.0 存取權杖」一文所述。

如要瞭解本例中使用的 cURL 選項,請參閱「使用 cURL」。

查看特定快訊的記錄

如要查看特定警報定義觸發警報的歷程記錄,請向下列資源發出 GET 要求:https://apimonitoring.enterprise.apigee.com/metrics/alerthistory

您必須使用 org 查詢參數和警報定義的 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"
  },
 ...
]

$ACCESS_TOKEN 設為 OAuth 2.0 存取權杖,如「取得 OAuth 2.0 存取權杖」一文所述。

如要瞭解本例中使用的 cURL 選項,請參閱「使用 cURL」。