prometheus 要监控自定义数据,那就要遵守metrics数据格式,不管你用什么语言写的,只要返回metrics的数据,接口提供给prometheus,Prometheus会拉取数据存入到数据库。
开始,prometheus监控招行黄金,这里我用的与市场差价来写的,python 语言
import json
import re
import requests
from flask import Flask ,Response
import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.192.400 QQBrowser/11.5.5250.400',
'Cookie': '',
"authority": "api.jijinhao.com",
"quoteCenter": "/quoteCenter/realTime.htm?codes=JO_92233&isCalc=true&_=1679559547196",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
,'upgrade-insecure-requests': '1'}
app = Flask(__name__)
url="https://api.jijinhao.com/quoteCenter/realTime.htm?codes=JO_92233&isCalc=true&_=1679559547199"
@app.route('/metrics')
def metrics():
v=get(3.2, 2.4)
REGISTRY = CollectorRegistry(auto_describe=False)
muxStatus = Gauge("bid", v['bid'], registry=REGISTRY)
manageStatus = Gauge("ask", v['ask'], registry=REGISTRY)
muxStatus.set(v['bid'])
manageStatus.set(v['ask'])
return Response(prometheus_client.generate_latest(REGISTRY), mimetype="text/plain")
return get(3.2,2.4)
@app.route('/api')
def api():
return get(3.2,2.4)
def get(Bid,Ask):
res=requests.get(url,headers=headers)
b = re.findall(r'({.*?})', res.text)
jd1=json.loads(b[0]+"}")['JO_92233']
a={"ask": (jd1['q63']+Bid).__str__() ,"bid": (jd1['q63']+Ask+Bid).__str__()}
return a
app.run(host='0.0.0.0')
运行得到的接口是127.0.0.1:5000/metrics
这里像不像exporter的客户端,这里可以相当如一个exporter
我们就可以正常的操作prometheus了 ,配置文件
效果
配置grafana图标。这里我自己编辑了一个
模板json
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 4,
"links": [
{
"asDropdown": true,
"icon": "external link",
"includeVars": true,
"keepTime": true,
"tags": [],
"targetBlank": true,
"title": "New link",
"tooltip": "",
"type": "dashboards",
"url": ""
}
],
"liveNow": false,
"panels": [
{
"gridPos": {
"h": 4,
"w": 23,
"x": 0,
"y": 0
},
"id": 9,
"libraryPanel": {
"name": "表格",
"uid": "x0G2RdfVk"
},
"title": "表格"
},
{
"datasource": {
"type": "prometheus",
"uid": "_7mJVCb4k"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 5,
"w": 23,
"x": 0,
"y": 4
},
"id": 4,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "_7mJVCb4k"
},
"editorMode": "builder",
"expr": "ask{instance=\"182.61.27.56:5000\", job=\"黄金\"}",
"hide": false,
"legendFormat": "__auto",
"range": true,
"refId": "A"
}
],
"title": "卖出价格",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "_7mJVCb4k"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 23,
"x": 0,
"y": 9
},
"id": 6,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "_7mJVCb4k"
},
"editorMode": "builder",
"expr": "bid{instance=\"182.61.27.56:5000\", job=\"黄金\"}",
"legendFormat": "__auto",
"range": true,
"refId": "A"
}
],
"title": "买入价格",
"type": "timeseries"
}
],
"refresh": "",
"revision": 1,
"schemaVersion": 38,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-5m",
"to": "now"
},
"timepicker": {
"nowDelay": "2s",
"refresh_intervals": [
"5s"
]
},
"timezone": "",
"title": "招商活期黄金",
"uid": "8gzG3vBVz",
"version": 10,
"weekStart": ""
}