forked from pokebotum/prometheus-delivery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (43 loc) · 1.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Created by Epic at 11/7/20
"""
from flask import Flask, request, jsonify
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import make_wsgi_app, Gauge, Counter, Histogram
from waitress import serve
from requests import get
from os import environ as env
metrics = {}
analytic_types = {
"gauge": Gauge,
"counter": Counter,
"histogram": Histogram
}
app = Flask(__name__)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
"/metrics": make_wsgi_app()
})
@app.route("/api/<string:name>/<string:analytic_type>", methods=["DELETE", "POST", "PATCH"])
def update_stats(name, analytic_type):
metric = metrics.get(name, None)
value = int(request.args.get("value", 1))
if metric is None:
metric_type = analytic_types.get(analytic_type)
if metric_type is None:
return None, 404
metric = metric_type(name, name)
metrics[name] = metric
if request.method == "DELETE":
metric.dec(value)
elif request.method == "POST":
if isinstance(metric, Histogram):
metric.observe(value)
return ""
metric.inc(value)
elif request.method == "PATCH":
metric.set(value)
return ""
@app.route("/api/query")
def query():
return jsonify(get(f"http://{env['PROMETHEUS_HOST']}:9090/api/v1/query_range", params=request.args).json())
serve(app, port=5050)