-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.py
33 lines (29 loc) · 979 Bytes
/
scrapper.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
from flask import request
from prometheus_client import Counter, Histogram
import time
import sys
# initializing objects metrics type counter
REQUEST_COUNT = Counter(
'request_count', 'App Request Count',
['app_name', 'method', 'endpoint', 'http_status']
)
# initializing objects metrics type Histogram
REQUEST_LATENCY = Histogram('request_latency_seconds', 'Request latency',
['app_name', 'endpoint']
)
def start_timer():
request.start_time = time.time()
def stop_timer(response):
#lable metrics
resp_time = time.time() - request.start_time
REQUEST_LATENCY.labels('webapp', request.path).observe(resp_time)
return response
def record_request_data(response):
# lable metrics
REQUEST_COUNT.labels('webapp', request.method, request.path,
response.status_code).inc()
return response
def setup_metrics(app):
app.before_request(start_timer)
app.after_request(record_request_data)
app.after_request(stop_timer)