Fail to add filters in API Prometheus query via awscurl python module

44 views Asked by At

Setting

I am trying to query an Amazon Managed Service for Prometheus endpoint to extract some metrics data via a python script. I am using the make_request method from the awscurl module in the following way (cleary this is an example snippet):

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)

In this way, I am able to successfully connect to the endpoint and download the data related to the query query=http_request_total&start=1710720000.0&end=1710806400.0&step=1h.

Problem

However, as soon as I try to add a filter (as statuscode="200") in the query using the PromQL syntax (e.g. query=http_request_total{statuscode="200"}&start=1710720000.0&end=1710806400.0&step=1h), the same script as above, returns a tedious 400 status code and an annoying 'x-amzn-errortype': 'InvalidQueryStringException' error.

Question

What am I doing wrong? Is it possible to add filters to prometheus queries through awscurl python library?

Thanks in advance.

2

There are 2 answers

1
DazWilkin On

For Prometheus, the query string must be URL encoded, see Prometheus HTTP API.

The User Guide for Amazon Managed Service for Prometheus explains how to use awscurl to query Prometheus-compatible APIs.

Instead of GET'ing which requires that the query string values already be URL-encoded; this explains why your example doesn't work, because the query string value should be:

http_request_total%7Bstatuscode%3D%22200%22%7D

The Amazon example, POSTs the data using the header Content-Type: application/x-www-form-urlencoded which performs the URL-encoding for you.

2
true_vices On

This is a documented issue in the awscurl repo issues section. Replacing the above query string with the URL encoded one retruns an InvalidSignatureException error.

The solution that worked for me: removing function aws_url_encode() from this line as explained in this issue comment.