You can download this code by clicking the button below.
This code is now available for download.
This function uses the Prometheus client library to create a random value as a metric and registers it with a Prometheus server.
Technology Stack : Prometheus, Prometheus Client Library
Code Type : Prometheus Metric Generation
Code Difficulty : Intermediate
def random_metric_sample(client):
# This function generates a random metric sample from a Prometheus client.
# Import required libraries from Prometheus client library
from prometheus_client import CollectorRegistry, Gauge
# Initialize a registry for metrics
registry = CollectorRegistry()
# Create a Gauge metric
gauge = Gauge('random_metric', 'A random gauge metric', registry=registry)
# Generate a random number and set it as the value of the gauge
import random
gauge.set(random.randint(1, 100))
# Export the metrics
client.register(registry)
client.export_metrics()