使用 API 管理快訊和通知

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

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

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

使用 API 設定快訊和通知

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

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

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

以下範例說明如何設定快訊,在任一區域的飯店 API Proxy 中,飯店 API Proxy 每秒有 5xx 狀態碼達到每秒 100 次交易 (TPS) 超過 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":"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 個百分位數的延遲時間快訊

以下範例說明如何設定快訊,在任一區域的第 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 (找不到應用程式) 狀態碼快訊

以下範例說明如何設定快訊,即針對任一地區在正式環境中的所有 API Proxy,發出 HTTP 404 狀態碼超過 10%,且 API Proxy 持續 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 Proxy 數量快訊

以下範例說明如何設定快訊,以便在任一區域的 API 5xx 代碼超過 200 次時觸發快訊。

在這個範例中,API 擷取至 Critical API Proxy 集合中 (使用 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% 時觸發快訊。

在這個範例中,目標服務擷取在「關鍵目標」集合中 (使用 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 為服務呼叫政策設定錯誤率快訊

以下範例說明如何設定快訊,並設定在 Service callout 政策指定服務的 500% 程式碼比率超過任一區域達 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 的 API 錯誤碼超過 10 分鐘。
  • 在所有區域,正式版環境中 API 的所有 API 通訊協定錯誤碼數量均大於 3%。

在這個範例中,API 擷取至 Critical API Proxy 集合中 (使用 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,傳遞您的機構名稱。您可以視需要指定用於搜尋快訊的時間範圍。預設設定是傳回過去一小時內觸發的所有快訊。

如上一節所示、建立快訊定義時,或使用「查看所有快訊定義」中顯示的 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」一文。