Prometheus Histogram with Label Recording

  • Share this:

Code introduction


This function creates a Prometheus histogram named 'random_histogram' and records a random value with three labels. The label names are 'label1', 'label2', 'label3', where 'label2' and 'label3' are used as label values.


Technology Stack : Prometheus Client, random

Code Type : Prometheus Client

Code Difficulty : Intermediate


                
                    
def random_histogram_value_labels(label1, label2, label3, value):
    from prometheus_client import Histogram
    import random

    # Create a Histogram object
    histogram = Histogram('random_histogram', 'A random histogram', labelnames=['label1', 'label2', 'label3'])

    # Generate a random value between 0 and 100 for demonstration
    random_value = random.randint(0, 100)

    # Record the value with labels
    histogram.observe(random_value, label1=label2, label2=label3)

    return random_value