-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.py
51 lines (36 loc) · 1.17 KB
/
example.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
import time
from flask import Flask, Response
from pytheus.backends import load_backend
from pytheus.backends.redis import MultiProcessRedisBackend
from pytheus.exposition import PROMETHEUS_CONTENT_TYPE, generate_metrics
from pytheus.metrics import Histogram
load_backend(
backend_class=MultiProcessRedisBackend,
backend_config={"host": "127.0.0.1", "port": 6379},
)
app = Flask(__name__)
histogram = Histogram("page_visits_latency_seconds", "used for testing")
histogram_labeled = Histogram(
"page_visits_latency_seconds_labeled",
"used for testing",
required_labels=["speed"],
default_labels={"speed": "normal"},
)
@app.route("/metrics")
def metrics():
data = generate_metrics()
return Response(data, headers={"Content-Type": PROMETHEUS_CONTENT_TYPE})
# track time with the context manager
@app.route("/")
def home():
with histogram.time():
with histogram_labeled.time():
return "hello world!"
# you can also track time with the decorator shortcut
@app.route("/slow")
@histogram
@histogram_labeled.labels({"speed": "slow"})
def slow():
time.sleep(3)
return "hello world! from slow!"
app.run(host="0.0.0.0", port=8080)