2019-06-25 14:28:46 1221瀏覽
今天千鋒扣丁學(xué)堂Python培訓(xùn)老師給大家分享一篇關(guān)于使用Python和Prometheus跟蹤天氣的詳細(xì)介紹,首先開源監(jiān)控系統(tǒng)Prometheus集成了跟蹤多種類型的時(shí)間序列數(shù)據(jù),但如果沒有集成你想要的數(shù)據(jù),那么很容易構(gòu)建一個(gè)。一個(gè)經(jīng)常使用的例子使用云端提供商的自定義集成,它使用提供商的API抓取特定的指標(biāo)。但是,在這個(gè)例子中,我們將與最大云端提供商集成:地球。
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"]
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
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()
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)
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】
查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>