prometheus
This commit is contained in:
parent
c79c363388
commit
76a87099b1
27
api.py
27
api.py
|
@ -1,12 +1,34 @@
|
||||||
from flask import Flask, jsonify
|
import time
|
||||||
|
from flask import Flask, jsonify, request
|
||||||
|
from prometheus_client import Counter, Summary,start_http_server
|
||||||
from timber import axe, LumberJack
|
from timber import axe, LumberJack
|
||||||
from config import _CLIENT_CONFIG
|
from config import _CLIENT_CONFIG
|
||||||
|
|
||||||
_VERSION = '0.1.1'
|
_VERSION = '0.1.2'
|
||||||
lumber = LumberJack()
|
lumber = LumberJack()
|
||||||
lumber.set_log_level('i')
|
lumber.set_log_level('i')
|
||||||
|
request.start_time = time.time()
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Create metrics
|
||||||
|
REQUEST_COUNT = Counter('api_requests_total', 'Total API Requests', ['method', 'endpoint'])
|
||||||
|
REQUEST_LATENCY = Summary('api_request_latency_seconds', 'API Request Latency', ['method', 'endpoint'])
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def before_request():
|
||||||
|
request.start_time = time.time()
|
||||||
|
|
||||||
|
@app.after_request
|
||||||
|
def after_request(response):
|
||||||
|
latency = time.time() - request.start_time
|
||||||
|
REQUEST_COUNT.labels(method=request.method, endpoint=request.path).inc()
|
||||||
|
REQUEST_LATENCY.labels(method=request.method, endpoint=request.path).observe(latency)
|
||||||
|
return response
|
||||||
|
|
||||||
|
@app.route('/metrics')
|
||||||
|
def metrics():
|
||||||
|
return str(REQUEST_COUNT.collect()), 200, {'Content-Type': 'text/plain'}
|
||||||
|
|
||||||
@app.route('/power/<hostname>', methods=['GET'])
|
@app.route('/power/<hostname>', methods=['GET'])
|
||||||
def get_status(hostname):
|
def get_status(hostname):
|
||||||
axe.info(f'Sending power 200 to {hostname}')
|
axe.info(f'Sending power 200 to {hostname}')
|
||||||
|
@ -22,4 +44,5 @@ def get_client_config(hostname):
|
||||||
return jsonify(_CLIENT_CONFIG), 200
|
return jsonify(_CLIENT_CONFIG), 200
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
start_http_server(8000)
|
||||||
app.run(debug=True, host='0.0.0.0')
|
app.run(debug=True, host='0.0.0.0')
|
|
@ -1,2 +1,4 @@
|
||||||
flask~=3.0.3
|
flask~=3.0.3
|
||||||
requests~=2.32.3
|
requests~=2.32.3
|
||||||
|
prometheus-client~=0.21.0
|
||||||
|
urllib3~=2.2.3
|
Loading…
Reference in New Issue