資料來源 API - Amazon Managed Grafana

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

資料來源 API

使用資料來源 API 在 Amazon 受管的 Grafana 工作區中建立、更新、刪除和使用資料來源。

注意

若要將 Grafana API 與您的 Amazon 受管理工作區搭配使用,您必須擁有有效的 Grafana API 權杖。您可以在 API 要求的Authorization欄位中包含此項目。如需如何建立權杖以驗證 API 呼叫的相關資訊,請參閱使用令牌進行身

取得所有資料來源

GET /api/datasources

範例請求

GET /api/datasources HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json [ { "id": 1, "orgId": 1, "uid": "H8joYFVGz" "name": "datasource_elastic", "type": "elasticsearch", "typeLogoUrl": "public/app/plugins/datasource/elasticsearch/img/elasticsearch.svg", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "grafana-dash", "basicAuth": false, "isDefault": false, "jsonData": { "esVersion": 5, "logLevelField": "", "logMessageField": "", "maxConcurrentShardRequests": 256, "timeField": "@timestamp" }, "readOnly": false } ]

透過 Id 取得單一資料來源

GET /api/datasources/:datasourceId

範例請求

GET /api/datasources/1 HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json { "id": 1, "uid": "kLtEtcRGk", "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": false, "basicAuthUser": "", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": { "graphiteType": "default", "graphiteVersion": "1.1" }, "secureJsonFields": {}, "version": 1, "readOnly": false }

透過 UID 取得單一資料來源

GET /api/datasources/uid/:uid

範例請求

GET /api/datasources/uid/kLtEtcRGk HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json { "id": 1, "uid": "kLtEtcRGk", "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": false, "basicAuthUser": "", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": { "graphiteType": "default", "graphiteVersion": "1.1" }, "secureJsonFields": {}, "version": 1, "readOnly": false }

依名稱取得單一資料來源

GET /api/datasources/name/:name

範例請求

GET /api/datasources/name/test_datasource HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json { "id": 1, "uid": "kLtEtcRGk", "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": false, "basicAuthUser": "", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": { "graphiteType": "default", "graphiteVersion": "1.1" }, "secureJsonFields": {}, "version": 1, "readOnly": false }

依名稱取得資料來源 ID

GET /api/datasources/id/:name

範例請求

GET /api/datasources/id/test_datasource HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json { "id":1 }

建立資料來源

POST /api/datasources

示例石墨請求

POST /api/datasources HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk { "name":"test_datasource", "type":"graphite", "url":"http://mydatasource.com", "access":"proxy", "basicAuth":false }

例如石墨回應

HTTP/1.1 200 Content-Type: application/json { "datasource": { "id": 1, "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": false, "basicAuthUser": "", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": {}, "secureJsonFields": {}, "version": 1, "readOnly": false }, "id": 1, "message": "Datasource added", "name": "test_datasource" }
注意

當您定義password和在其basicAuthPassword中進行定義時secureJsonData,Amazon 受管的 Grafana 會將它們安全地加密為資料庫中的加密 Blob。接著回應會列出中的加密欄位secureJsonFields

啟用基本身份驗證的示例石墨請求

POST /api/datasources HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk { "name": "test_datasource", "type": "graphite", "url": "http://mydatasource.com", "access": "proxy", "basicAuth": true, "basicAuthUser": "basicuser", "secureJsonData": { "basicAuthPassword": "basicpassword" } }

啟用基本身份驗證的示例響應

HTTP/1.1 200 Content-Type: application/json { "datasource": { "id": 1, "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": true, "basicAuthUser": "basicuser", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": {}, "secureJsonFields": { "basicAuthPassword": true }, "version": 1, "readOnly": false }, "id": 102, "message": "Datasource added", "name": "test_datasource" }

CloudWatch 請求示例

POST /api/datasources HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk { "name": "test_datasource", "type": "cloudwatch", "url": "http://monitoring.us-west-1.amazonaws.com", "access": "proxy", "jsonData": { "authType": "keys", "defaultRegion": "us-west-1" }, "secureJsonData": { "accessKey": "Ol4pIDpeKSA6XikgOl4p", "secretKey": "dGVzdCBrZXkgYmxlYXNlIGRvbid0IHN0ZWFs" } }

更新現有資料來源

PUT /api/datasources/:datasourceId

範例請求

PUT /api/datasources/1 HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk { "id":1, "orgId":1, "name":"test_datasource", "type":"graphite", "access":"proxy", "url":"http://mydatasource.com", "password":"", "user":"", "database":"", "basicAuth":true, "basicAuthUser":"basicuser", "secureJsonData": { "basicAuthPassword": "basicpassword" }, "isDefault":false, "jsonData":null }

回應範例

HTTP/1.1 200 Content-Type: application/json { "datasource": { "id": 1, "orgId": 1, "name": "test_datasource", "type": "graphite", "typeLogoUrl": "", "access": "proxy", "url": "http://mydatasource.com", "password": "", "user": "", "database": "", "basicAuth": true, "basicAuthUser": "basicuser", "basicAuthPassword": "", "withCredentials": false, "isDefault": false, "jsonData": {}, "secureJsonFields": { "basicAuthPassword": true }, "version": 1, "readOnly": false }, "id": 102, "message": "Datasource updated", "name": "test_datasource" }
注意

我們建議您定義password並在其basicAuthPassword中,secureJsonData以便將它們安全地儲存為資料庫中的加密 Blob。接著回應會列出中的加密欄位secureJsonFields

依 Id 刪除資料來源

DELETE /api/datasources/:datasourceId

範例請求

DELETE /api/datasources/1 HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json {"message":"Data source deleted"}

透過 UID 刪除資料來源

DELETE /api/datasources/uid/:uid

範例請求

DELETE /api/datasources/uid/kLtEtcRGk HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json {"message":"Data source deleted"}

依名稱刪除資料來源

DELETE /api/datasources/name/:datasourceName

範例請求

DELETE /api/datasources/name/test_datasource HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

回應範例

HTTP/1.1 200 Content-Type: application/json { "message":"Data source deleted", "id": 1 }

資料來源代理呼叫

GET /api/datasources/proxy/:datasourceId/*

代理對實際數據源的所有調用。

依 Id 查詢資料來源

POST /api/tsdb/query

查詢具有後端實現的數據源。大多數內置數據源都具有後端實現。

範例請求

POST /api/tsdb/query HTTP/1.1 Accept: application/json Content-Type: application/json { "from": "1420066800000", "to": "1575845999999", "queries": [ { "refId": "A", "intervalMs": 86400000, "maxDataPoints": 1092, "datasourceId": 86, "rawSql": "SELECT 1 as valueOne, 2 as valueTwo", "format": "table" } ] }
注意

fromto、和queries屬性是必需的。

主體結構定義:

  • 從/到 — 必須是以毫秒為單位的紀元時間戳記的絕對值,或是使用 Grafana 時間單位的相對值。例如 now-1h

  • 詢. ReFID — (選擇性) 指定查詢的識別碼。預設值為 A

  • 詢。資料供應 ID — 指定要查詢的資料來源。請求中的每個查詢都必須具有唯一的datasourceId

  • 查詢。 maxDataPoints— (可選) 指定管控面板可以彩現的最大資料點數量。預設為 100。

  • 詢。相互驗證 — (選擇性) 指定時間間隔,以毫秒為單位的時間序列。預設值為 1000

對於 MySQL 數據源請求示例:

POST /api/tsdb/query HTTP/1.1 Accept: application/json Content-Type: application/json { "from": "1420066800000", "to": "1575845999999", "queries": [ { "refId": "A", "intervalMs": 86400000, "maxDataPoints": 1092, "datasourceId": 86, "rawSql": "SELECT\n time,\n sum(opened) AS \"Opened\",\n sum(closed) AS \"Closed\"\nFROM\n issues_activity\nWHERE\n $__unixEpochFilter(time) AND\n period = 'm' AND\n repo IN('grafana/grafana') AND\n opened_by IN('Contributor','Grafana Labs')\nGROUP BY 1\nORDER BY 1\n", "format": "time_series" } ] }

對於 MySQL 數據源請求的響應示例:

HTTP/1.1 200 Content-Type: application/json { "results": { "A": { "refId": "A", "meta": { "rowCount": 0, "sql": "SELECT\n time,\n sum(opened) AS \"Opened\",\n sum(closed) AS \"Closed\"\nFROM\n issues_activity\nWHERE\n time <= 1420066800 AND time >= 1575845999 AND\n period = 'm' AND\n repo IN('grafana/grafana') AND\n opened_by IN('Contributor','Grafana Labs')\nGROUP BY 1\nORDER BY 1\n" }, "series": [ { "name": "Opened", "points": [ [ 109, 1420070400000 ], [ 122, 1422748800000 ] ] }, { "name": "Closed", "points": [ [ 89, 1420070400000 ] ] } ] } } }