Fetching Random Histogram Value from Prometheus

  • Share this:

Code introduction


This function fetches a random value from a histogram metric in Prometheus. It accepts a Prometheus client object, metric name, and label values as parameters.


Technology Stack : Prometheus, Prometheus Python client library

Code Type : Prometheus metric query

Code Difficulty : Intermediate


                
                    
def random_histogram_value(client, metric_name, label_values):
    """
    This function fetches a random value from a histogram metric in Prometheus.
    """
    query = f"histogram_sum({metric_name}) by ({', '.join(label_values)})"
    result = client.query(query)
    samples = result.data.result[0].samples
    if samples:
        return samples[0].value
    else:
        return None