Package: promql-http-api
PromQL HTTP APIThis python package provides aPrometheusHTTP API client library.
It encapsulates and simplifies the collection of data from a Prometheus server.
One major feature of this library is that responses to queries are returned asPandasDataFrames.Prometheus is an open-source system monitoring and alerting toolkit. It collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true. The Prometheus server exposes an HTTP API for querying the collected data, and a query language called PromQL.This library is intended to help data scientists who would like to harvest data from a Prometheus server for analysis and visualization. The library is design to be simple to use, and to provide a convenient interface to the Prometheus HTTP API. It is also designed to be performant and scalable, by using therequestslibrary and caching HTTP connections to the Prometheus server between API accesses.For unstable connections, the library supports retrying failed requests. The user may specify the number of retries, the time-out between retries, and the back-off factor for the retry interval.Version 0.2.0 improves timezone awareness. Starting in this version, the HTTP API query is issued with a timestamp value in the 'time', 'start', and 'end' fields. In addition, the query schema now supports a 'timezone' element (provided as a timezone object), which controls the timestamp column formatting in the returned dataframe.InstallationTo install as a root user:python3 -m pip install promql-http-apiTo install as a non-root user:python3 -m pip install --user promql-http-apiTo uninstall:python3 -m pip uninstall promql-http-apiUsage ExamplesHere is a basic usage example:frompromql_http_apiimportPromqlHttpApiapi=PromqlHttpApi('http://localhost:9090')tz=pytz.timezone('UTC')q_time=datetime.now()q=api.query('up',tz.localize(q_time))df=q.to_dataframe()print(df)On the first line we create a PromqlHttpApi object namedapi. This example assumes that a Prometheus server is running on the local host, and it is listening to port 9090.
Replace this URL as needed with the appropriate URL for your server.Next, we use theapiobject to create a Query object namedq. Thequery()function takes two parameters: a query string and a date-time string.To execute the query explicitly, without converting the result to a DataFrame, you can use:# Execute the query explicitlypromql_response_data=q()# Convert the cached result to a DataFramedf=q.to_dataframe()Alternately, by calling the to_dataframe() method alone, we will implicitly execute the query.# Execute the query implicitlydf=q.to_dataframe()Adding retries and time-out to the query work only with explicit execution:# Execute the query explicitly# with 5 retries and retry intervals of 5, 10, 20, and 40 secondspromql_response_data=q(retries=5,timeout=5,backoff=2)# Convert the cached result to a DataFramedf=q.to_dataframe()Working with schemasTheto_dataframe()method takes an optionalschemaparameter. The schema is a dictionary that controls several elements of the query. A schema may include the following element keys:columns,dtype, andtimezone.Thecolumnselement controls the PromQL response elements to be included as columns in the returned DataFrame. The returned DataFrame will always include a timestamp column (in seconds since the epoch), and avaluecolumn. Ifcolumnsis not provided, all the fields returned in a PromQL response will be included in the returned DataFrame. Ifcolumnsis provided, only the fields listed incolumnswill be included in the returned DataFrame.Thedtypeelement controls the data type of thevaluecolumn in the returned DataFrame. The default isstr. Thedtypeelement may be set to any valid Pandas data type.Thetimezoneelement allows the user to request an additionaldatetimecolumn which is formatted in the specified timezone. Thetimezoneelement must be a timezone object from thepytzlibrary. If thetimezoneelement is not provided, the returned DataFrame will not include adatetimecolumn.Here is an example of how to use a schema:schema={'columns':['node','sensor'],'dtype':float,'timezone':pytz.timezone('US/Eastern')}df=q.to_dataframe(schema)DebuggingIf something goes wrong, you can look at the HTTP response and the PromQL response information. Here are some examples:frompromql_http_apiimportPromqlHttpApiapi=PromqlHttpApi('http://localhost:9090')tz=pytz.timezone('UTC')q_time=datetime.now()q=api.query('up',tz.localize(q_time))q()promql_response=q.responsehttp_response=promql_response.responseprint(f'HTTP response status code  ={http_response.status_code}')print(f'HTTP response encoding     ={http_response.encoding}')print(f'PromQL response status     ={promql_response.status()}')print(f'PromQL response data       ={promql_response.data()}')print(f'PromQL response error type ={promql_response.error_type()}')print(f'PromQL response error      ={promql_response.error()}')List of Supported APIsAPIMethod/api/v1/queryquery(query, time)/api/v1/query_rangequery_range(query, start, end, step)/api/v1/format_queryformat_query(query)/api/v1/seriesseries(match)/api/v1/labelslabels()/api/v1/label/<label_name>/valueslabel_values(label)/api/v1/targetstargets(state)/api/v1/rulesrules(type)/api/v1/alertsalerts()/api/v1/alertmanagersalertmanagers()/api/v1/status/configconfig()/api/v1/status/flagsflags()/api/v1/status/runtimeinforuntimeinfo()/api/v1/status/buildinfobuildinfo()TestingThe package contains limited unit testing.
Run the tests from the package top folder using:pytestFuture workImplement a CI/CD pipeline with a Prometheus instance in a Docker container to test API accesses.If you use this library and would like to help - please contact the author.ReferencesPrometheus / HTTP API