Random Histogram Sample Recording with Prometheus

  • Share this:

Code introduction


This function uses Prometheus' Histogram to record a sample with a random value. It first creates a Histogram with specific labels, then generates a random value and records a sample with that value.


Technology Stack : Prometheus_client, random

Code Type : Prometheus Metrics

Code Difficulty : Intermediate


                
                    
def random_histogram_value(labels, value):
    from prometheus_client import Histogram
    from random import randint

    # Create a Histogram with a specific buckets configuration
    histogram = Histogram('random_histogram', 'A random histogram', labels=labels)

    # Record a sample with random value
    random_value = randint(1, 100)
    histogram.observe(random_value, labels)

    # Return the histogram to allow further operations if needed
    return histogram