forked from prometheus-community/prometheus-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
31 lines (23 loc) · 1019 Bytes
/
server.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
import time
from flask import Flask, Response, jsonify, request
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
LATENCY_HISTOGRAM = Histogram('myapp_request_latency_seconds', 'myapp request latency', ['method', 'endpoint'])
REQUEST_COUNTER = Counter('myapp_request_count', 'myapp HTTP request count', ['method', 'endpoint', 'status'])
app = Flask(__name__)
def before_request():
request.start_time = time.time()
def after_request(response):
request_latency = time.time() - request.start_time
LATENCY_HISTOGRAM.labels(request.method, request.path).observe(request_latency)
REQUEST_COUNTER.labels(request.method, request.path, response.status_code).inc()
return response
@app.route("/metrics")
def metrics():
return generate_latest(), 200
@app.route("/<path:path>")
def other_routes(path):
return "Hello world", 200
if __name__ == '__main__':
app.before_request(before_request)
app.after_request(after_request)
app.run(host='0.0.0.0')