You can download this code by clicking the button below.
This code is now available for download.
This function generates a random Prometheus metric. It takes a label dictionary and a value, creates a random metric type (counter, gauge, histogram, or summary), and records the given value.
Technology Stack : prometheus_client, random
Code Type : Prometheus Metric Generator
Code Difficulty : Intermediate
def generate_random_metric(labels, value):
from prometheus_client import Counter
from random import choice
# Create a random counter with a random name
metric_name = f"random_metric_{choice(['counter', 'gauge', 'histogram', 'summary'])}"
counter = Counter(metric_name, labels)
# Record a random value for the metric
counter.labels(**labels).inc(value)
return counter