通过awscurl Python模块在API Prometheus查询中添加过滤器失败
设置
我正在尝试通过一个 Python 脚本来查询 Amazon 管理的 Prometheus 服务,以提取一些指标数据。我使用了 make_request
方法,来自 awscurl 模块,下面是一个示例代码片段:
import awscurl.awscurl as aws
uri = 'https://<SERVICE>-workspaces.<REGION>.amazonaws.com/workspaces/<WORKSPACE_ID>/api/v1/query_range?query=http_request_total&start=1710720000.0&end=1710806400.0&step=1h'
headers = {
'Content-Type': 'application/json',
}
awscurl_auth_params = {
'method': 'GET',
'service': <SERVICE>,
'region': <REGION>,
'uri': uri,
'headers': headers,
'data': '',
'access_key': <ACCES_KEY>,
'secret_key': <SECRET_KEY>,
'security_token': None,
'data_binary': False}
r = aws.make_request(**awscurl_auth_params)
通过这种方式,我能够成功连接到这个接口,并下载与查询 query=http_request_total&start=1710720000.0&end=1710806400.0&step=1h
相关的数据。
问题
但是,当我尝试在查询中添加一个过滤条件(比如 statuscode="200"
)时,使用 PromQL 语法(例如 query=http_request_total{statuscode="200"}&start=1710720000.0&end=1710806400.0&step=1h
),同样的脚本却返回了一个麻烦的 400 状态码,并且出现了一个令人烦恼的错误 'x-amzn-errortype': 'InvalidQueryStringException'
。
问题
我哪里做错了?通过 awscurl 的 Python 库,是否可以在 Prometheus 查询中添加过滤条件?
提前谢谢你。
2 个回答
对于Prometheus来说,查询字符串必须进行URL编码,具体可以参考Prometheus的HTTP API文档。
AWS的Amazon Managed Service for Prometheus用户指南中解释了如何使用awscurl
来查询与Prometheus兼容的API。
与其使用GET
请求(这要求查询字符串的值必须已经进行URL编码),这也就是你示例不工作的原因,因为查询字符串的值应该是:
http_request_total%7Bstatuscode%3D%22200%22%7D
在亚马逊的示例中,使用POST
请求发送数据,并在请求头中包含Content-Type: application/x-www-form-urlencoded
,这样就会自动为你进行URL编码。