Get the App
SLTechnology News&Howtos  ›  Development  › 

How to track the weather using Python and Prometheus

Shulou Source: shulou.com Published: 2022-06-02 23:03:15 09月16日 Update

How to use Python and Prometheus to track the weather, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

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

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

Fortunately, the US government has measured the weather and provided a simple API for integration. It's easy to get the weather forecast for the next hour at Red Hat headquarters.

Import requestsHOURLY_RED_HAT = "" def get_temperature (): result = requests.get (HOURLY_RED_HAT) return result.json () ["properties"] ["periods"] [0] ["temperature"]

Now that we have completed our integration with Earth, it is time to make sure that Prometheus understands what we want. We can use gauge in the Prometheus Python library to create a registry entry: the temperature of Red Hat headquarters.

From prometheus_client import CollectorRegistry, Gaugedef prometheus_temperature (num): registry= CollectorRegistry () g = Gauge ("red_hat_temp", "Temperature at Red Hat HQ", registry=registry) g.set (num) return registry

*, we need to connect it to Prometheus in some way. This is somewhat dependent on Prometheus's network topology: whether it is easier for Prometheus to communicate with our services, or whether it is easier to reverse.

* is usually recommended. If possible, we need to build a Web server with a public registration entry and configure Prometheus to scrape it.

We can use Pyramid to build a simple Web server.

From pyramid.config import Configuratorfrom pyramid.response import Responsefrom prometheus_client import generate_latest, CONTENT_TYPE_LATESTdef 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 ()

This can be run using any Web Gateway Interface (WSGI) server. For example, suppose we put the code in earth.py, and we can run it using python-m twisted web-- wsgi earth.app.

Or, if our code is easier to connect to Prometheus, we can push it to Prometheus's push gateway on a regular basis.

Import timefrom prometheus_client import push_to_gatewaydef push_temperature (url): while True: registry = prometheus_temperature (get_temperature ()) push_to_gateway (url, "temperature collector", registry) time.sleep (60 / 60)

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

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Provider service weather tracking cloud earth server gateway code example content headquarters data red hat help push run clear lucky portal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology OPPO Reno Linux NVidia