Create a custom Prometheus integration to track the largest cloud provider: Mother Earth.

Tree clouds

The open source monitoring system Prometheus integrates tracking multiple types of time series data, but if you don’t have the data you want, it’s easy to build one. A frequently used example uses a cloud provider’s custom integration, which uses the provider’s API to fetch specific metrics. However, in this case, we will integrate with the largest cloud provider: Earth.

Fortunately, the US government has measured the weather and provided a simple API for integration. Getting the weather forecast for the next hour from Red Hat headquarters is easy.

import requests
HOURLY_RED_HAT = "< https://api.weather.gov/gridpoints/RAH/73, 57 / forecast/hourly >"
def get_temperature():
    result = requests.get(HOURLY_RED_HAT)
    return result.json()["properties"] ["periods"] [0] ["temperature"]
Copy the code

Now that we’ve completed our integration with Earth, it’s time to make sure Prometheus understands what we want. We can create a registry using the Gauge in the Prometheus Python library: the temperature at Red Hat Headquarters.

from prometheus_client import CollectorRegistry, Gauge
def prometheus_temperature(num):
    registry = CollectorRegistry()
    g = Gauge("red_hat_temp"."Temperature at Red Hat HQ", registry=registry)
    g.set(num)
    return registry
Copy the code

Finally, we need to connect it to Prometheus in some way. This is somewhat dependent on Prometheus’ network topology: is it easier for Prometheus to communicate with our service, or vice versa?

The first is the commonly recommended scenario where, if possible, we need to build a Web server that exposes the registry entry and configure Prometheus to scrape it.

We can use Pyramid to build a simple Web server.

from pyramid.config import Configurator
from pyramid.response import Response
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
def metrics_web(request):
    registry = prometheus_temperature(get_temperature())
    return Response(generate_latest(registry),
                               content_type=CONTENT_TYPE_LATEST)
config = Configurator()
config.add_route('metrics'.'/metrics')
config.add_view(metrics_web, route_name='metrics')
app = config.make_wsgi_app()
Copy the code

This can be run using any Web Gateway Interface (WSGI) server. For example, assuming we put the code in earth.py, we could run it using Python -m twisted Web –wsgi earth.app.

Or, if our code is easier to connect to Prometheus, we can periodically push it to Prometheus’s push gateway.

import time
from prometheus_client import push_to_gateway
def push_temperature(url):
    while True:
        registry = prometheus_temperature(get_temperature())
        push_to_gateway(url, "temperature collector", registry)
        time.sleep(60*60)
Copy the code

The URL here is the URL of the push gateway. It usually ends with :9091.

Good luck building your custom Prometheus integration to keep track of everything!


Via: opensource.com/article/19/…

By Moshe Zadka, Lujun9972

This article is originally compiled by LCTT and released in Linux China