prometheus_api_client package

Submodules

prometheus_api_client.metric module

A Class for metric object.

class prometheus_api_client.metric.Metric(metric, oldest_data_datetime=None)[source]

Bases: object

A Class for Metric object.

Parameters:
  • metric – (dict) A metric item from the list of metrics received from prometheus
  • oldest_data_datetime

    (datetime|timedelta) Any metric values in the dataframe that are older than this value will be deleted when new data is added to the dataframe using the __add__(“+”) operator.

    • oldest_data_datetime=datetime.timedelta(days=2), will delete the

    metric data that is 2 days older than the latest metric. The dataframe is pruned only when new data is added to it. n * oldest_data_datetime=datetime.datetime(2019,5,23,12,0), will delete any data that is older than “23 May 2019 12:00:00” n * oldest_data_datetime=datetime.datetime.fromtimestamp(1561475156) can also be set using the unix timestamp

Example Usage:

prom = PrometheusConnect()

my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'}

metric_data = prom.get_metric_range_data(metric_name='up', label_config=my_label_config) Here metric_data is a list of metrics received from prometheus

# only for the first item in the list ``my_metric_object = Metric(metric_data[0], datetime.timedelta(days=10)) ``

__add__(other)[source]

Overloading operator +.

Add two metric objects for the same time-series

Example Usage:
metric_1 = Metric(metric_data_1)
metric_2 = Metric(metric_data_2)
metric_12 = metric_1 + metric_2 # will add the data in ``metric_2`` to ``metric_1``
                                # so if any other parameters are set in ``metric_1``
                                # will also be set in ``metric_12``
                                # (like ``oldest_data_datetime``)
Returns:(Metric) Returns a Metric object with the combined metric data

of the two added metrics

Raises:(TypeError) Raises an exception when two metrics being added are

from different metric time-series

__eq__(other)[source]

Overloading operator =.

Check whether two metrics are the same (are the same time-series regardless of their data)

Example Usage:

metric_1 = Metric(metric_data_1)

metric_2 = Metric(metric_data_2)

print(metric_1 == metric_2) # will print True if they belong to the same time-series

Returns:(bool) If two Metric objects belong to the same time-series, i.e. same name and label config, it will return True, else False
__str__()[source]

Make it print in a cleaner way when print function is used on a Metric object.

Example Usage:

metric_1 = Metric(metric_data_1)

print(metric_1) # will print the name, labels and the head of the dataframe

plot()[source]

Plot a very simple line graph for the metric time-series.

prometheus_api_client.metrics_list module

docstring for MetricsList.

class prometheus_api_client.metrics_list.MetricsList(metric_data_list)[source]

Bases: list

A Class to initialize a list of Metric objects at once.

Parameters:metric_data_list – (list|json) This is an individual metric or list of metrics received from prometheus as a result of a promql query.
Example Usage:
prom = PrometheusConnect()
my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'}
metric_data = prom.get_metric_range_data(metric_name='up', label_config=my_label_config)

metric_object_list = MetricsList(metric_data) # metric_object_list will be initialized as
                                              # a list of Metric objects for all the
                                              # metrics downloaded using get_metric query

prometheus_api_client.prometheus_connect module

A Class for collection of metrics from a Prometheus Host.

class prometheus_api_client.prometheus_connect.PrometheusConnect(url: str = 'http://127.0.0.1:9090', headers: dict = None, disable_ssl: bool = False)[source]

Bases: object

A Class for collection of metrics from a Prometheus Host.

Parameters:
  • url – (str) url for the prometheus host
  • headers – (dict) A dictionary of http headers to be used to communicate with the host. Example: {“Authorization”: “bearer my_oauth_token_to_the_host”}
  • disable_ssl – (bool) If set to True, will disable ssl certificate verification for the http requests made to the prometheus host
all_metrics(params: dict = None)[source]

Get the list of all the metrics that the prometheus host scrapes.

Parameters:params – (dict) Optional dictionary containing GET parameters to be sent along with the API request, such as “time”
Returns:(list) A list of names of all the metrics available from the specified prometheus host
Raises:(Http Response error) Raises an exception in case of a connection error
custom_query(query: str, params: dict = None)[source]

A method to send a custom query to a Prometheus Host.

This method takes as input a string which will be sent as a query to the specified Prometheus Host. This query is a PromQL query.

Parameters:
Returns:

(list) A list of metric data received in response of the query sent

Raises:

(Exception) Raises an exception in case of a connection error

get_current_metric_value(metric_name: str, label_config: dict = None, params: dict = None)[source]

A method to get the current metric value for the specified metric and label configuration.

Parameters:
  • metric_name – (str) The name of the metric
  • label_config – (dict) A dictionary that specifies metric labels and their values
  • params – (dict) Optional dictionary containing GET parameters to be sent along with the API request, such as “time”
Returns:

(list) A list of current metric values for the specified metric

Raises:

(Http Response error) Raises an exception in case of a connection error

Example Usage:

prom = PrometheusConnect()

my_label_config = {'cluster': 'my_cluster_id', 'label_2': 'label_2_value'}

prom.get_current_metric_value(metric_name='up', label_config=my_label_config)

get_metric_range_data(metric_name: str, label_config: dict = None, start_time: datetime.datetime = datetime.datetime(2020, 4, 22, 23, 2, 40, 207925), end_time: datetime.datetime = datetime.datetime(2020, 4, 22, 23, 12, 40, 207938), chunk_size: datetime.timedelta = None, store_locally: bool = False, params: dict = None)[source]

A method to get the current metric value for the specified metric and label configuration.

Parameters:
  • metric_name – (str) The name of the metric.
  • label_config – (dict) A dictionary specifying metric labels and their values.
  • start_time – (datetime) A datetime object that specifies the metric range start time.
  • end_time – (datetime) A datetime object that specifies the metric range end time.
  • chunk_size – (timedelta) Duration of metric data downloaded in one request. For example, setting it to timedelta(hours=3) will download 3 hours worth of data in each request made to the prometheus host
  • store_locally – (bool) If set to True, will store data locally at, “./metrics/hostname/metric_date/name_time.json.bz2”
  • params – (dict) Optional dictionary containing GET parameters to be sent along with the API request, such as “time”
Returns:

(list) A list of metric data for the specified metric in the given time range

Raises:

(Exception) Raises an exception in case of a connection error

Module contents

A collection of tools to collect and manipulate prometheus metrics.