使用 API 查看指標資料

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

使用下列 API 取得 API 監控指標。指標是 API Monitoring 從 API Monitoring 記錄中包含的原始資料匯總而來的資料值。

以下各節說明如何使用 API 管理指標。

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

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

指標 API

您用來向指標 API 發出 GET 要求的網址如下:

https://apimonitoring.enterprise.apigee.com/metrics/resource

其中 resource 對應至特定指標。下表列出指標資源:

資源 說明
/traffic 取得流量指標。指定篩選條件,例如 Proxy 名稱、間隔、時間範圍、目標、狀態碼等。
/latency 取得要求的延遲時間指標

傳送至 Edge 和後端目標。指定 Proxy 名稱、間隔、目標、狀態碼等篩選條件。

/targets 取得特定機構和環境的所有目標網域。
/alerthistory 取得特定機構和時間範圍的快訊記錄指標。
/alertinstance/instanceid 取得特定快訊例項 ID 的快訊記錄指標。
/alertsummary 取得機構和時間範圍的快訊總數。
/faultcodenames 取得所有 faultcode 名稱。
/faultcodes 取得錯誤碼。
/faultcodecategories 取得故障代碼類別。
/faultcodesubcategories 取得故障代碼子類別。
/faultcodedetails 取得所有故障代碼和詳細資料。

取得故障資訊

/fault* 資源都會傳回 Edge 可能發生的故障相關中繼資料。 舉例來說,如要查看所有可能的故障類別清單,請執行下列操作:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/faultcodecategories" \
-H "accept: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN"

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

回應如下所示:

{
  "faultCodeCategories":[
    "","API Protocol","Developer/App","Extension Policy","Gateway",
    "Mediation Policy","Mint","Security Policy","Sense","Traffic Mgmt Policy"
  ]
}

接著,您就可以判斷 API Protocol 類別的故障代碼清單:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/faultcodes?faultCodeCategory=API Protocol" \
-H "accept: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN"

如需其他選項,請參閱 Metrics API

擷取流量和延遲時間的指標

指標 API 具有篩選條件,可套用至 API,指定自訂時間範圍、Proxy、區域、環境和其他篩選條件,以計算指標。舉例來說,如要查看所有環境在過去一小時內每 10 分鐘的每秒交易次數 (tps) 指標,請執行下列操作:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/traffic?from=-1h&to=now&select=tps&interval=10m&groupBy=env&org=myorg" \
-H "accept: application/json"  \
-H "Authorization: Bearer $ACCESS_TOKEN" 

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

這項呼叫會傳回以下格式的結果:

{
  "results":[
    {
      "series":[
        {
          "name":"proxy",
        "tags":
             {
                "env":"prod",
                "intervalSeconds":"60",
                "org":"myorg",
                "region":"myregion"
              },
            "columns":["time","tps"],
            "values":[
              ["2018-08-15T13:10:00Z",5.03],
              ["2018-08-15T13:20:00Z",5.01],
              ["2018-08-15T13:30:00Z",5.81],
              ["2018-08-15T13:40:00Z",5.95],
              
            ]
          },
       
       }
    }]
}

請注意,columns 屬性會指定 values 的格式。values 屬性包含每 10 分鐘計算一次的 tps,適用於前一個 10 分鐘間隔。

interval 查詢參數會定義指標儲存至結果的頻率,以及結果中值的取樣時間範圍。在上述範例中,系統會根據前 10 分鐘的資料計算指標,並每 10 分鐘將結果寫入。

使用 fromto 查詢參數,指定 ISO 格式的時間範圍。fromto 指定的時間長度上限為 24 小時。

日期格式可以是:

  • yyyy-mm-ddThh:mm:ssZ
  • yyyy-mm-ddThh:mm:ss+00:00

例如:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/traffic?from=2018-08-13T14%3A04%3A00Z&to=2018-08-13T14%3A10%3A00Z&select=tps&interval=1m&groupBy=env&org=myorg&proxy=PublicAPI" \
-H "accept: application/json"  \
-H "Authorization: Bearer $ACCESS_TOKEN"

或者,您也可以使用 fromto 查詢參數指定相對時間範圍,例如過去 1 小時:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/traffic?from=-1h&to=now&select=tps&interval=1m&groupBy=env&org=myorg&proxy=PublicAPI" \
-H "accept: application/json"  \
-H "Authorization: Bearer $ACCESS_TOKEN"

您也可以使用 proxy 查詢參數,顯示單一 Proxy 的每秒交易數 (tps):

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/traffic?from=-1h&to=now&select=tps&interval=1m&groupBy=env&org=myorg&proxy=PublicAPI" \
-H "accept: application/json"  \
-H "Authorization: Bearer $ACCESS_TOKEN"

延遲指標的指定條件與流量指標大致相同。不過,對於 /latency 資源:

  • 您必須將 percentile 查詢參數指定為 50909599。舉例來說,如果您指定 90,API 會傳回第 90 個百分位數的總回應延遲值。
  • windowsize 固定為一分鐘。

舉例來說,如要查看 1 分鐘時間區間內第 90 百分位數的總延遲時間指標,請執行下列操作:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/latency?percentile=90&select=totalLatency&from=-1h&to=now&interval=5m&windowsize=1m&groupBy=org,env,region&org=myorg" \
-H "accept: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN"

如需其他選項,請參閱 Metrics API

擷取快訊指標

Metrics API 會傳回所有快訊、特定快訊或快訊摘要的指標。舉例來說,如要取得機構過去一小時的快訊記錄,請執行下列指令:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/alerthistory?org=myorg&from=-1h&to=now" \
-H "accept: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN"

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

此 API 呼叫會傳回以下形式的回應:

[
  {
"id":"983c4c7a-c301-4697-95cc-9a7c53e05fac",
"organization":"myorg",
"environment":"prod",
"name":"Public Api 5xx error rate",
"type":"Alert",
"source":"https://www.apigee.net/sonar",
"raw_payload":"
{
    \"reportUUID\":\"\",
    \"reportEnabled\":false,
    \"organization\":\"myorg\",
    \"name\":\"Public Api 5xx error rate\",
    \"self\":\"/alerts/95cc9ef4-345f-11e8-9fd3-12774584e062\",
    \"description\":\"\",
    \"conditions\":[
    {
        \"comparator\":\"\u003e\",
        \"metric\":\"rate\",
        \"durationSeconds\":3600,
        \"name\":\"\",
        \"description\":\"\",
        \"threshold\":0.01,
        \"dimensions\":
        {
            \"proxy\":\"myAPI\",
            \"org\":\"myorg\",
            \"env\":\"prod\",
            \"region\":\"myRegion\",
            \"statusCode\":\"5xx\"
            }
        }],
        \"uuid\":\"95cc9ef4-345f-11e8-9fd3-12774584e062\",
    \"playbook\":\"This is a test alert.\"
    }",
"time":"2018-08-14T12:45:28Z"
 },
 
]

然後,您可以使用傳回陣列中的 id,取得特定快訊的相關資訊:

curl -X GET \
"https://apimonitoring.enterprise.apigee.com/metrics/alertinstance/983c4c7a-c301-4697-95cc-9a7c53e05fac" \
-H "accept: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN"

如需其他選項,請參閱 Metrics API