

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

# 使用與 Prometheus 相容的 API 查詢
<a name="AMP-onboard-query-APIs"></a>

雖然使用 [Amazon Managed Grafana](AMP-amg.md) 等工具是檢視和查詢指標最簡單的方式，但 Amazon Managed Service for Prometheus 也支援數個可用來查詢指標的與 Prometheus 相容 API。如需有關所有可用與 Prometheus 相容 API 的詳細資訊，請參閱 [與 Prometheu 相容的 API](AMP-APIReference-Prometheus-Compatible-Apis.md)。

Prometheus 相容 APIs 使用 Prometheus 查詢語言 PromQL 來指定您要傳回的資料。如需 PromQL 及其語法的詳細資訊，請參閱 [Prometheus 文件中的查詢](https://prometheus.io/docs/prometheus/latest/querying/basics/) Prometheus。

當您使用這些 API 查詢指標時，必須使用 AWS 第 4 版簽署程序來簽署請求。您可以設定[AWS 簽章版本 4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)來簡化簽署程序。如需詳細資訊，請參閱 [aws-sigv4-proxy](https://github.com/awslabs/aws-sigv4-proxy)。

您可以使用 執行透過 AWS SigV4 代理簽署`awscurl`。下列主題[使用 awscurl 查詢與 Prometheus 相容的 APIs](AMP-compatible-APIs.md) 逐步引導您使用 `awscurl` 設定 AWS SigV4。

**Topics**
+ [使用 awscurl 查詢與 Prometheus 相容的 APIs](AMP-compatible-APIs.md)

# 使用 awscurl 查詢與 Prometheus 相容的 APIs
<a name="AMP-compatible-APIs"></a>

Amazon Managed Service for Prometheus 的 API 請求必須使用 [SigV4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) 簽署。您可以使用 [awscurl](https://github.com/okigan/awscurl) 來簡化查詢程序。

若要安裝 `awscurl`，您需要安裝 Python 3 和 pip 套件管理員。

在以 Linux 為基礎的執行個體上，下列命令會安裝 `awscurl`。

```
$ pip3 install awscurl
```

在 macOS 電腦上，下列命令會安裝 `awscurl`。

```
$ brew install awscurl
```

下列範例是範例`awscurl`查詢。將 *Region*、*Workspace-id* 和 *QUERY* 輸入取代為您的使用案例的適當值：

```
# Define the Prometheus query endpoint URL. This can be found in the Amazon Managed Service for Prometheus console page 
# under the respective workspace. 

$ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace-id/api/v1/query

# credentials are infered from the default profile
$ awscurl -X POST --region Region \
                  --service aps "${AMP_QUERY_ENDPOINT}" -d 'query=QUERY'  --header 'Content-Type: application/x-www-form-urlencoded'
```

**注意**  
您的查詢字串必須經過 url 編碼。

對於類似 的查詢`query=up`，您可以取得結果，例如：

```
{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "__name__": "up",
          "instance": "localhost:9090",
          "job": "prometheus",
          "monitor": "monitor"
        },
        "value": [
          1652452637.636,
          "1"
        ]
      },
    ]
  }
}
```

為了 `awscurl` 簽署所提供的請求，您需要以下列其中一種方式傳送有效的憑證：
+ 為 IAM 角色提供存取金鑰 ID 和密鑰。您可以在 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 中找到該角色的存取金鑰和密鑰。

  例如：

  ```
  $ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query
  
  $ awscurl -X POST --region <Region> \
                    --access_key <ACCESS_KEY> \
                    --secret_key <SECRET_KEY> \
                    --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>"
  ```
+ 參考儲存在 `.aws/credentials` 和 `/aws/config` 檔案中的組態檔案。您也可以選擇指定將使用的設定檔名稱。如果未指定，將使用 ` default ` 檔案。例如：

  ```
  $ export AMP_QUERY_ENDPOINT=https://aps-workspaces.<Region>.amazonaws.com/workspaces/<Workspace_ID>/api/v1/query
  $ awscurl -X POST --region <Region> \
                    --profile <PROFILE_NAME> 
                    --service aps "$AMP_QUERY_ENDPOINT?query=<QUERY>"
  ```
+ 使用與 EC2 執行個體相關聯的執行個體設定檔。

## 使用 awscurl 容器執行查詢請求
<a name="awscurl-container"></a>

無法安裝不同版本的 **Python** 和相關的相依項目時，一個容器可以用來打包 `awscurl` 應用程式及其相依項目。下列範例使用 **Docker** 執行期進行部署 `awscurl`，但任何符合 OCI 規範的執行期和映像都可以運作。

```
$ docker pull okigan/awscurl
$ export AMP_QUERY_ENDPOINT=https://aps-workspaces.Region.amazonaws.com/workspaces/Workspace_id/api/v1/query
$ docker run --rm -it okigan/awscurl --access_key $AWS_ACCESS_KEY_ID  --secret_key $AWS_SECRET_ACCESS_KEY \ --region Region --service aps "$AMP_QUERY_ENDPOINT?query=QUERY"
```