Random Data Observation with Prometheus Counter and Histogram

  • Share this:

Code introduction


This function uses Prometheus' Counter and Histogram to record and observe randomly generated data. The Counter is used for simple counting, while the Histogram is used to collect the distribution of the data.


Technology Stack : Prometheus Client

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_histogramsample(data):
    from prometheus_client import Counter, Histogram
    import random

    # Create a Counter
    counter = Counter('my_counter', 'A simple counter')

    # Create a Histogram
    histogram = Histogram('my_histogram', 'A simple histogram')

    # Sample data
    for _ in range(10):
        value = random.randint(1, 100)
        counter.inc()
        histogram.observe(value)

    return counter.count, histogram.samples