自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Python和Prometheus跟蹤天氣

開發(fā) 后端 開源
開源監(jiān)控系統(tǒng) Prometheus 集成了跟蹤多種類型的時(shí)間序列數(shù)據(jù),但如果沒有集成你想要的數(shù)據(jù),那么很容易構(gòu)建一個(gè)。一個(gè)經(jīng)常使用的例子使用云端提供商的自定義集成,它使用提供商的 API 抓取特定的指標(biāo)。

[[264315]]

創(chuàng)建自定義 Prometheus 集成以跟蹤***的云端提供商:地球母親。

開源監(jiān)控系統(tǒng) Prometheus 集成了跟蹤多種類型的時(shí)間序列數(shù)據(jù),但如果沒有集成你想要的數(shù)據(jù),那么很容易構(gòu)建一個(gè)。一個(gè)經(jīng)常使用的例子使用云端提供商的自定義集成,它使用提供商的 API 抓取特定的指標(biāo)。但是,在這個(gè)例子中,我們將與***云端提供商集成:地球。

幸運(yùn)的是,美國(guó)政府已經(jīng)測(cè)量了天氣并為集成提供了一個(gè)簡(jiǎn)單的 API。獲取紅帽總部下一個(gè)小時(shí)的天氣預(yù)報(bào)很簡(jiǎn)單。

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

現(xiàn)在我們已經(jīng)完成了與地球的集成,現(xiàn)在是確保 Prometheus 能夠理解我們想要內(nèi)容的時(shí)候了。我們可以使用 Prometheus Python 庫(kù)中的 gauge 創(chuàng)建一個(gè)注冊(cè)項(xiàng):紅帽總部的溫度。

  1. from prometheus_client import CollectorRegistry, Gauge
  2. def prometheus_temperature(num):
  3. registry = CollectorRegistry()
  4. g = Gauge("red_hat_temp", "Temperature at Red Hat HQ", registry=registry)
  5. g.set(num)
  6. return registry

***,我們需要以某種方式將它連接到 Prometheus。這有點(diǎn)依賴 Prometheus 的網(wǎng)絡(luò)拓?fù)洌菏?Prometheus 與我們的服務(wù)通信更容易,還是反向更容易。

***種是通常建議的情況,如果可能的話,我們需要構(gòu)建一個(gè)公開注冊(cè)入口的 Web 服務(wù)器,并配置 Prometheus 收刮(scrape)它。

我們可以使用 Pyramid 構(gòu)建一個(gè)簡(jiǎn)單的 Web 服務(wù)器。

  1. from pyramid.config import Configurator
  2. from pyramid.response import Response
  3. from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
  4. def metrics_web(request):
  5. registry = prometheus_temperature(get_temperature())
  6. return Response(generate_latest(registry),
  7. content_type=CONTENT_TYPE_LATEST)
  8. config = Configurator()
  9. config.add_route('metrics', '/metrics')
  10. config.add_view(metrics_web, route_name='metrics')
  11. app = config.make_wsgi_app()

這可以使用任何 Web 網(wǎng)關(guān)接口(WSGI)服務(wù)器運(yùn)行。例如,假設(shè)我們將代碼放在 earth.py 中,我們可以使用 python -m twisted web --wsgi earth.app 來(lái)運(yùn)行它。

或者,如果我們的代碼連接到 Prometheus 更容易,我們可以定期將其推送到 Prometheus 的推送網(wǎng)關(guān)。

  1. import time
  2. from prometheus_client import push_to_gateway
  3. def push_temperature(url):
  4. while True:
  5. registry = prometheus_temperature(get_temperature())
  6. push_to_gateway(url, "temperature collector", registry)
  7. time.sleep(60*60)

這里的 URL 是推送網(wǎng)關(guān)的 URL。它通常以 :9091 結(jié)尾。

祝你構(gòu)建自定義 Prometheus 集成成功,以便跟蹤一切! 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2023-08-08 09:00:00

開源Prometheus

2021-07-01 11:29:45

KubernetesGrafana監(jiān)控

2023-12-27 18:05:13

2020-06-03 21:29:53

BLELoRa室內(nèi)定位

2025-02-17 07:00:00

ORB對(duì)象跟蹤器計(jì)算機(jī)視覺

2024-09-12 17:19:43

YOLO目標(biāo)檢測(cè)深度學(xué)習(xí)

2024-10-10 17:05:00

2024-08-27 12:40:59

2021-06-23 15:59:22

PythonRequestsHTTP庫(kù)

2010-05-27 14:02:04

SVN使用說(shuō)明

2016-12-12 14:31:42

戴爾

2009-08-25 15:58:03

C#跟蹤和調(diào)試語(yǔ)句

2021-09-30 08:54:58

prometheus監(jiān)控遠(yuǎn)端服務(wù)

2022-05-19 08:21:02

vmalert監(jiān)控

2023-10-09 07:31:25

2022-07-08 08:00:31

Prometheus監(jiān)控

2021-09-28 07:48:54

prometheus監(jiān)控遠(yuǎn)端服務(wù)

2021-10-08 06:22:00

Prometheus 儀表化應(yīng)用運(yùn)維

2010-05-20 18:00:52

Eclipse下使用S

2010-05-25 16:52:39

SVN中使用Git
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)