-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
35 lines (24 loc) · 831 Bytes
/
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
import logging
import os
import requests
from flask import Flask, jsonify, request
from prometheus_client import Counter, start_wsgi_server
log = logging.getLogger(__name__)
app = Flask(__name__)
start_wsgi_server(int(os.environ.get('PROMETHEUS_PORT', 9095)))
http_get_metric = Counter('http_get', ' Http GET metric', ['url', 'code'])
@app.route('/status', methods=['GET'])
def hello():
return 'ok'
@app.route('/', methods=['POST'])
def url_status():
data = request.json
url = data['url']
try:
r = requests.get(url)
code = r.status_code
except requests.exceptions.HTTPError as error:
log.error('An error occurred when requesting to %s: %s', url, error)
code = 500
http_get_metric.labels(url=url, code=code).inc()
return jsonify({'url': url, 'code': code})