

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

# 資料來源 API
<a name="v10-Grafana-API-Datasource"></a>

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

**注意**  
若要搭配 Amazon Managed Grafana 工作區使用 Grafana API，您必須擁有有效的服務帳戶字符。您可以在 API 請求的 `Authorization` 欄位中包含此項目。

## 取得所有資料來源
<a name="v10-Grafana-API-Datasource-getall"></a>

```
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 取得單一資料來源
<a name="v10-Grafana-API-Datasource-getbyId"></a>

```
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 取得單一資料來源
<a name="v10-Grafana-API-Datasource-getbyUID"></a>

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

## 依名稱取得單一資料來源
<a name="v10-Grafana-API-Datasource-getbyName"></a>

```
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
<a name="v10-Grafana-API-Datasource-getIDbyName"></a>

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

## 建立資料來源
<a name="v10-Grafana-API-Datasource-create"></a>

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

**注意**  
當您在 `basicAuthPassword`中定義 `password`和 時`secureJsonData`，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"
  }
}
```

## 更新現有的資料來源
<a name="v10-Grafana-API-Datasource-update"></a>

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

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

## 依 ID 刪除資料來源
<a name="v10-Grafana-API-Datasource-deletebyId"></a>

```
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 刪除資料來源
<a name="v10-Grafana-API-Datasource-deletebyUID"></a>

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

## 依名稱刪除資料來源
<a name="v10-Grafana-API-Datasource-deletebyname"></a>

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

## 資料來源代理呼叫
<a name="v10-Grafana-API-Datasource-proxycall"></a>

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

代理對實際資料來源的所有呼叫。

## 查詢資料來源
<a name="v10-Grafana-API-Datasource-querydatasource"></a>

```
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 內文結構描述：
+ **往返** - 指定查詢的時間範圍。時間可以是以毫秒為單位的 epoch 時間戳記，也可以是使用 Grafana 時間單位的相對時間戳記。例如 `now-5m`。
+ **查詢** — 指定一或多個查詢。必須至少包含 1 個。
+ **query.datasource.uid** — 指定要查詢的資料來源 UID。請求中的每個查詢都必須具有唯一的 `datasource`。
+ **query.refId** — 指定查詢的識別符。預設為「A」。
+ **query.format** — 指定應傳回資料的格式。有效選項為 `time_series`或 `table`，取決於資料來源。
+ **query.maxDataPoints** — 指定儀表板面板可轉譯的資料點數量上限。預設為 100。
+ **query.intervalMs** — 以毫秒為單位指定時間序列時間間隔。預設為 1000。

此外，每個資料來源的特定屬性都應新增到請求中 （例如 **query.stringInput**，如上述請求所示）。若要進一步了解如何針對特定資料來源建立查詢，請在您選擇的瀏覽器中使用開發人員工具，並檢查對 提出的 HTTP 請求`/api/ds/query`。

**範例測試資料來源時間序列查詢回應**

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

## 依 ID 查詢資料來源
<a name="v10-Grafana-API-Datasource-querybyId"></a>

```
POST /api/tsdb/query
```

**重要**  
`/api/tsdb/query` 不支援從第 9 版開始。請使用 [查詢資料來源](#v10-Grafana-API-Datasource-querydatasource)。

查詢具有後端實作的資料來源。大多數內建資料來源都有後端實作。

**範例請求**

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

**注意**  
`from`需要 `to`、 和 `queries` 屬性。

JSON 內文結構描述：
+ **往返** - 必須是以毫秒為單位的 epoch 時間戳記絕對值，或使用 Grafana 時間單位的相對值。例如 `now-1h`。
+ **query.refId** — （選用） 指定查詢的識別符。預設值為 `A`。
+ **query.datasourceId** — 指定要查詢的資料來源。請求中的每個查詢都必須具有唯一的 `datasourceId`。
+ **query.maxDataPoints** — （選用） 指定儀表板面板可轉譯的資料點數量上限。預設為 100。
+ **query.intervalIMs** — （選用） 以毫秒為單位指定時間序列的時間間隔。預設值為 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
            ]
          ]
        }
      ]
    }
  }
}
```