数据源 API
如果您运行的是 Grafana 企业版,对于某些端点,您需要具有特定的权限。有关更多信息,请参阅 基于角色的访问控制权限。
获取所有数据源
GET /api/datasources
警告
此 API 当前不处理分页。默认最大返回数据源数量为 5000。您可以在 default.ini 文件中更改此值。
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:read | 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": {
"logLevelField": "",
"logMessageField": "",
"maxConcurrentShardRequests": 256,
"timeField": "@timestamp"
},
"readOnly": false
}
]
通过 ID 获取单个数据源
GET /api/datasources/:datasourceId
警告
此 API 自 Grafana v9.0.0 起已弃用,将在未来的版本中删除。请参阅 通过 UID 获取单个数据源的 API 或 通过名称获取单个数据源的 API。
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:read | datasources:* datasources:id:* datasources:id:1 (单个数据源) |
示例
示例请求:
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
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:read | datasources:* datasources:uid:* datasources:uid:kLtEtcRGk (单个数据源) |
示例
示例请求
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
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:read | datasources:* datasources:name:* datasources:name:test_datasource (单个数据源) |
示例
示例请求:
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
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources.id:read | datasources:* datasources:name:* datasources:name:test_datasource (单个数据源) |
示例
示例请求:
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
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:create | 不适用 |
示例
示例 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
下定义password
和basicAuthPassword
,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"
}
}
通过 ID 更新现有数据源
PUT /api/datasources/:datasourceId
警告
此 API 自 Grafana v9.0.0 起已弃用,将在未来版本中删除。请参阅新的数据源更新 API。
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:write | datasources:* datasources:id:* datasources:id:1 (单个数据源) |
示例
示例请求:
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,
"uid": "kLtEtcRGk",
"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"
}
注意
与创建数据源类似,为了安全地作为加密 blob 存储在数据库中,应在secureJsonData
下定义password
和basicAuthPassword
。然后,响应中的secureJsonFields
部分列出加密字段。
更新现有数据源
PUT /api/datasources/uid/:uid
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:write | datasources:* datasources:uid:* datasources:uid:kLtEtcRGk (单个数据源) |
示例
示例请求:
PUT /api/datasources/uid/kLtEtcRGk HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"id":1,
"uid": "updated UID",
"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,
"uid": "updated UID",
"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"
}
注意
与创建数据源类似,为了安全地作为加密 blob 存储在数据库中,应在secureJsonData
下定义password
和basicAuthPassword
。然后,响应中的secureJsonFields
部分列出加密字段。
通过 ID 删除现有数据源
DELETE /api/datasources/:datasourceId
警告
此 API 自 Grafana v9.0.0 起已弃用,将在未来版本中删除。请参阅按 UID 删除现有数据源的 API或按名称删除现有数据源的 API。
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:delete | datasources:* datasources:id:* datasources:id:1 (单个数据源) |
示例
示例请求:
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
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:delete | datasources:* datasources:uid:* datasources:uid:kLtEtcRGk (单个数据源) |
示例
示例请求
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",
"id": 1
}
通过名称删除现有数据源
DELETE /api/datasources/name/:datasourceName
所需权限
请参阅 简介 中的说明。
操作 | 范围 |
---|---|
datasources:delete | datasources:* datasources:name:* datasources:name:test_datasource (单个数据源) |
示例
示例请求:
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
}
按 ID 进行数据源代理调用
警告
此 API 自 Grafana v9.0.0 起已弃用,将在未来版本中删除。请参阅新的数据源代理 API。
GET /api/datasources/proxy/:datasourceId/*
代理所有对由 datasourceId
标识的实际数据源的调用。
数据源代理调用
GET /api/datasources/proxy/uid/:uid/*
代理所有对由 uid
标识的实际数据源的调用。
通过 ID 检查数据源健康状态
警告:此 API 自 Grafana v9.0.0 起已弃用,将在未来版本中删除。请参阅新的数据源健康检查 API。
GET /api/datasources/:datasourceId/health
对由给定 datasourceId
标识的数据源的健康端点进行调用。这不是强制的 - 每个插件作者都必须在他们的插件中实现对健康检查的支持。
示例
示例请求:
GET api/datasources/112/health HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
示例响应:
HTTP/1.1 200
Content-Type: application/json
{
"message": "1. Successfully queried the CloudWatch metrics API.\n2. Successfully queried the CloudWatch logs API.",
"status": "OK"
}
检查数据源健康状态
GET /api/datasources/uid/:uid/health
对由给定 uid
标识的数据源的健康端点进行调用。这不是强制的 - 每个插件作者都必须在他们的插件中实现对健康检查的支持。
示例
示例请求:
GET api/datasources/uid/P8045C56BDA891CB2/health HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
示例响应:
HTTP/1.1 200
Content-Type: application/json
{
"message": "1. Successfully queried the CloudWatch metrics API.\n2. Successfully queried the CloudWatch logs API.",
"status": "OK"
}
通过 ID 获取数据源资源
警告
此 API 自 Grafana v9.0.0 起已弃用,将在未来版本中删除。请参阅新的数据源资源 API。
GET /api/datasources/:datasourceId/resources/*
对由给定 dashboardId
标识的数据源的资源端点进行调用。
示例
示例请求:
GET api/datasources/112/resources/dimension-keys?region=us-east-2&namespace=AWS%2FEC2&dimensionFilters=%7B%7D&metricName=CPUUtilization HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
示例响应:
HTTP/1.1 200
Content-Type: application/json
[
{
"text": "AutoScalingGroupName",
"value": "AutoScalingGroupName",
"label": "AutoScalingGroupName"
},
{
"text": "ImageId",
"value": "ImageId",
"label": "ImageId"
},
{
"text": "InstanceId",
"value": "InstanceId",
"label": "InstanceId"
},
{
"text": "InstanceType",
"value": "InstanceType",
"label": "InstanceType"
}
]
获取数据源资源
GET /api/datasources/uid/:uid/resources/*
对由给定 uid
标识的数据源的资源端点进行调用。
示例
示例请求:
GET api/datasources/uid/P8045C56BDA891CB2/resources/dimension-keys?region=us-east-2&namespace=AWS%2FEC2&dimensionFilters=%7B%7D&metricName=CPUUtilization HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
示例响应:
HTTP/1.1 200
Content-Type: application/json
[
{
"text": "AutoScalingGroupName",
"value": "AutoScalingGroupName",
"label": "AutoScalingGroupName"
},
{
"text": "ImageId",
"value": "ImageId",
"label": "ImageId"
},
{
"text": "InstanceId",
"value": "InstanceId",
"label": "InstanceId"
},
{
"text": "InstanceType",
"value": "InstanceType",
"label": "InstanceType"
}
]
查询数据源
查询具有后端实现的的数据源。
POST /api/ds/query
注意
Grafana 的内置数据源通常具有后端实现。
测试数据源的示例请求:
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 – 指定查询的时间范围。时间可以是毫秒级的纪元时间戳或使用Grafana时间单位表示的相对时间。例如,
now-5m
。 - queries – 指定一个或多个查询。必须至少包含1个。
- queries.datasource.uid – 指定要查询的数据源的UID。请求中的每个查询都必须有唯一的
datasource
。 - queries.refId – 指定查询的标识符。默认为“A”。
- queries.format – 指定数据应该返回的格式。有效选项是
time_series
或table
,具体取决于数据源。 - queries.maxDataPoints - 指定仪表板面板可以渲染的最大数据点数。默认为100。
- queries.intervalMs - 指定时间序列的时间间隔(毫秒)。默认为1000。
此外,在请求中还应添加每个数据源的具体属性(例如,上述请求中显示的 queries.stringInput)。要更好地了解如何为特定数据源构建查询,请使用您选择的浏览器的开发者工具并检查对 /api/ds/query
发出的HTTP请求。
示例测试数据源时间序列查询响应
{
"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]
]
}
}
]
}
}
}
状态码
代码 | 描述 |
---|---|
200 | 所有数据源查询均返回了成功响应。 |
400 | 由于无效的JSON、缺少内容类型、缺少或无效的字段等,请求无效。或者一个或多个数据源查询失败。请参阅正文以获取更多详细信息。 |
403 | 访问拒绝。 |
404 | 无法找到满足请求所需的数据源或插件。 |
500 | 意外错误。请参阅正文和/或服务器日志以获取更多详细信息。 |