

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

# 使用兼容普罗米修斯进行查询 APIs
<a name="AMP-onboard-query-APIs"></a>

尽管使用诸如 [Amazon Managed Grafana 之类的工具是查看和查询指标的最简单方法，但适用于 Prometheus 的亚马逊托管](AMP-amg.md)服务也支持多个 APIs 兼容 Prometheus 的工具，您可以使用这些工具来查询您的指标。有关所有可用的 Prometheus 兼容版本的更多信息，请参阅。 APIs [兼容普罗米修斯 APIs](AMP-APIReference-Prometheus-Compatible-Apis.md)

与 Prometheus 兼容的 Prometheus 查询语言使用 APIs Prometheus 查询语言 PromQL 来指定要返回的数据。有关 PromQL 及其语法的详细信息，请参阅 Prometheus 文档中的[查询 Prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/)。

当您使用它们 APIs 来查询指标时，必须使用签 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) Sigv4 的过程。`awscurl` AWS 

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