View a markdown version of this page

数据来源 API - Amazon Managed Grafana

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

数据来源 API

使用数据来源 API 创建、更新、删除和处理 Amazon Managed Grafana 工作区中的数据来源。

注意

要在您的亚马逊托管 Grafana 工作空间中使用 Grafana API,您必须拥有有效的服务账户令牌。您可以将其包含在 API 请求的 Authorization 字段中。

获取所有数据来源

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 } ]

按 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

Graphite 请求示例

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 }

Graphite 响应示例

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" }
注意

当您在 secureJsonData 中定义 passwordbasicAuthPassword 时,Amazon Managed Grafana 会将它们安全地加密为数据库中的加密 blob。然后,响应会列出 secureJsonFields 中的加密字段。

启用了基本身份验证的 Graphite 请求示例

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" } }

通过 UID 更新数据源

PUT /api/datasources/uid/:uid

更新与给定 UID 匹配的数据源。

示例请求

PUT /api/datasources/uid/kLtEtcRGk 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, "isDefault": false }

响应示例

HTTP/1.1 200 Content-Type: application/json { "datasource": { "id": 1, "uid": "kLtEtcRGk", "orgId": 1, "name": "test_datasource", "type": "graphite", "access": "proxy", "url": "http://mydatasource.com", "basicAuth": false, "isDefault": false }, "name": "test_datasource" }

按 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/*

代理对实际数据来源的所有调用。

查询数据来源

POST /api/ds/query

查询具有后端实现的数据来源。大多数内置数据来源都有后端实现。

示例请求

POST /api/ds/query HTTP/1.1 Accept: application/json Content-Type: application/json { "queries":[ { "refId":"A", "scenarioId":"csv_metric_values", "datasource":{ "uid":"PD8C576611E62080A" }, "format": "table", "maxDataPoints":1848, "intervalMs":200, "stringInput":"1,20,90,30,5,0" } ], "from":"now-5m", "to":"now" }

JSON 正文架构:

  • from/to— 指定查询的时间范围。时间可以是以毫秒为单位的 epoch 时间戳,也可以是使用 Grafana 时间单位的相对值。例如 now-5m

  • queries:指定一个或多个查询。必须至少包含 1 个。

  • queries.datasource.uid:指定要查询的数据来源的 UID。请求中的每个查询都必须具有唯一的 datasource

  • queries.refId:指定查询的标识符。默认值为“A”。

  • queries.format:指定返回数据的格式。有效选项是 time_seriestable,取决于数据来源。

  • queries.max DataPoints — 确定仪表板面板可以呈现的最大数据点数量。默认值为 100。

  • queries.intervalMs:指定以毫秒为单位的时间序列时间间隔。默认值为 1000。

此外,应在请求中添加每个数据来源的特定属性(例如 queries.stringInput,如上面的请求所示)。要更好地了解如何为特定数据来源生成查询,请在您选择的浏览器中使用开发者工具,并检查向 /api/ds/query 发出的 HTTP 请求。

测试数据来源时间序列查询响应示例

HTTP/1.1 200 Content-Type: application/json { "results": { "A": { "frames": [ { "schema": { "refId": "A", "fields": [ { "name": "time", "type": "time", "typeInfo": { "frame": "time.Time" } }, { "name": "A-series", "type": "number", "typeInfo": { "frame": "int64", "nullable": true } } ] }, "data": { "values": [ [1644488152084, 1644488212084, 1644488272084, 1644488332084, 1644488392084, 1644488452084], [1, 20, 90, 30, 5, 0] ] } } ] } } }

更新现有数据源(已弃用)

重要

此端点已被弃用。请改用 通过 UID 更新数据源

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" }
注意

我们建议您在 secureJsonData 中定义 passwordbasicAuthPassword,以便它们在数据库中安全地存储为加密的 blob。然后,响应会列出 secureJsonFields 中的加密字段。

按 Id 获取单个数据源(已弃用)

重要

此端点已被弃用。请改用 按 UID 获取单个数据来源

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 }

按 Id 删除数据源(已弃用)

重要

此端点已被弃用。请改用 按 UID 删除数据来源

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"}