-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.py
60 lines (46 loc) · 2.04 KB
/
app.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
52
53
54
55
56
57
58
59
60
import random
import time
from flask import Flask, render_template_string, abort
from prometheus_client import generate_latest, REGISTRY, Counter, Gauge, Histogram
app = Flask(__name__)
# A counter to count the total number of HTTP requests
REQUESTS = Counter('http_requests_total', 'Total HTTP Requests (count)', ['method', 'endpoint', 'status_code'])
# A gauge (i.e. goes up and down) to monitor the total number of in progress requests
IN_PROGRESS = Gauge('http_requests_inprogress', 'Number of in progress HTTP requests')
# A histogram to measure the latency of the HTTP requests
TIMINGS = Histogram('http_request_duration_seconds', 'HTTP request latency (seconds)')
# Standard Flask route stuff.
@app.route('/')
# Helper annotation to measure how long a method takes and save as a histogram metric.
@TIMINGS.time()
# Helper annotation to increment a gauge when entering the method and decrementing when leaving.
@IN_PROGRESS.track_inprogress()
def hello_world():
REQUESTS.labels(method='GET', endpoint="/", status_code=200).inc() # Increment the counter
return 'Hello, World!'
# Note I'm intentionally failing occasionally to simulate a flakey service.
@app.route('/slow')
@TIMINGS.time()
@IN_PROGRESS.track_inprogress()
def slow_request():
v = random.expovariate(1.0 / 1.3)
if v > 3:
REQUESTS.labels(method='GET', endpoint="/slow", status_code=500).inc()
abort(500)
time.sleep(v)
REQUESTS.labels(method='GET', endpoint="/slow", status_code=200).inc()
return render_template_string('<h1>Wow, that took {{v}} s!</h1>', v=v)
@app.route('/hello/<name>')
@IN_PROGRESS.track_inprogress()
@TIMINGS.time()
def index(name):
REQUESTS.labels(method='GET', endpoint="/hello/<name>", status_code=200).inc()
return render_template_string('<b>Hello {{name}}</b>!', name=name)
@app.route('/metrics')
@IN_PROGRESS.track_inprogress()
@TIMINGS.time()
def metrics():
REQUESTS.labels(method='GET', endpoint="/metrics", status_code=200).inc()
return generate_latest(REGISTRY)
if __name__ == "__main__":
app.run(host='0.0.0.0')