天翼云Prometheus監控服務提供了HTTP API地址,您可以在自建的Grafana中接入Prometheus監控數據,或在二次開發過程中通過Prometheus API獲取監控數據。
前提條件
已創建Prometheus 實例。
客戶端網絡已經與暴露接口打通。
操作指南
步驟一:獲取用戶的AccessKey和AccessKey Secret
如果您已創建Prometheus實例,且您需要使用AccessKey和AccessKey Secret進行遠程讀寫,則需要先為獲取用戶的AccessKey ID和AccessKey Secret。
通過實名認證的賬號登錄天翼云。
進入天翼云賬號中心。
點擊【安全設置】進入安全設置中心
在用戶AccessKey模塊,可創建AKSK,或直接查看已生成的AKSK。
步驟二:獲取HTTP API地址
登錄應用性能監控控制臺,左側菜單選擇Prometheus監控,進入實例列表頁面。
單擊目標實例名稱。
在設置頁簽上,復制HTTP API地址。
可以單擊生成token,獲取Prometheus實例的鑒權Token。
注意
生成Token后,可在Grafana中添加數據源時配置Token,否則必須配置用戶的AccessKey和AccessKey Secret。
步驟三:接入自建Grafana(token接入)
1. 配置Grafana數據源。
以管理員賬號登錄本地Grafana系統。
點擊頁面左上角的
圖標。在左側導航欄中選擇Configuration > Data Sources。說明:僅管理員可以看到此菜單。
在Data Sources頁簽上單擊+ Add new data source。
在Add data source頁面上單擊Prometheus。
在Settings頁簽的Name字段中輸入自定義的名稱,在Prometheus server URL字段中粘貼上文【步驟一:獲取HTTP API地址】中獲得的HTTP API地址。
單擊+ Add header,在Custom HTTP Headers區域單擊+Add header,設置Header為Authorization,設置Value為【步驟二:獲取HTTP API地址】中獲取的鑒權Token。

單擊頁簽底部的Save & Test。
2. 驗證結果。
登錄本地Grafana系統。
點擊頁面左上角的
圖標,然后在左側導航欄中單擊Dashboards,點擊Dashboards頁面右側的New,然后在下拉菜單中單擊New dashboard。
點擊+ Add visualization,
在Select data source頁面選擇您創建的數據源。
在Edit Panel頁面的Query頁簽下的A區域的Metrics字段和Label filters字段中輸入指標名稱和值,單擊Run queries進行查詢。
如果能顯示出相應指標的圖表,則說明操作成功。否則請檢查填寫的接口地址或Token是否正確,以及數據源是否有Prometheus的監控數據。

步驟四:接入自建Grafana(AccessKey接入)
1. 配置Grafana數據源。
以管理員賬號登錄本地Grafana系統。
點擊頁面左上角的
圖標。在左側導航欄中選擇Configuration > Data Sources。說明:僅管理員可以看到此菜單。
在Data Sources頁簽上單擊+ Add new data source。
在Add data source頁面上單擊Prometheus。
在Settings頁簽的Name字段中輸入自定義的名稱,在Prometheus server URL字段中粘貼上文【步驟一:獲取HTTP API地址】中獲得的HTTP API地址。
選中Auth中的Basic auth,User和Password輸入在【步驟一】中獲取的AccessKey和AccessKey Secret。
單擊頁簽底部的Save & Test。
2. 驗證結果。
參考【步驟三】中的驗證。
步驟五:獲取Prometheus監控數據(token接入示例)
調用Prometheus監控數據的請求示例如下。更多使用HTTP API獲取Prometheus監控數據的操作,請參見開源版Prometheus HTTP API。
GET {HTTP API}/api/v1/query
Accept: application/json
Content-Type: application/json
Authorization: {Token}
{
"query":"arms_prometheus_target_interval_length_seconds_sum",
"time":"1635302655",
"timeout":"1000"
}說明
{HTTP API}和{Token}請替換為上文【步驟一:獲取HTTP API地址】中獲取的HTTP API和鑒權Token。
返回示例:
{
"status": "success",
"data": {
"resultType": "vector",
"result": [
{
"metric": {
"__name__": "arms_prometheus_target_interval_length_seconds_sum",
"instance": "localhost:9335",
"interval": "15s",
"job": "_arms-prom/kubelet/1"
},
"value": [
1635302655,
"146655.24420603***"
]
},
{
"metric": {
"__name__": "arms_prometheus_target_interval_length_seconds_sum",
"instance": "localhost:9335",
"interval": "30s",
"job": "_arms-prom/kubelet/1"
},
"value": [
1635302655,
"879810.747346***"
]
},
{
"metric": {
"__name__": "arms_prometheus_target_interval_length_seconds_sum",
"instance": "localhost:9335",
"interval": "20s",
"job": "_arms-prom/kubelet/1"
},
"value": [
1635302655,
"73320.13578499***"
]
}
]
}
}
