From 84d20d3632fc53ab15c680952e54f3220a03805e Mon Sep 17 00:00:00 2001 From: jaanbaaz Date: Fri, 13 Sep 2024 11:55:50 +0530 Subject: [PATCH 1/2] prometheus added --- app.py | 25 +++++++++++++++++++++++++ prometheus.yml | 7 +++++++ 2 files changed, 32 insertions(+) create mode 100644 prometheus.yml diff --git a/app.py b/app.py index c411dea..93825ca 100644 --- a/app.py +++ b/app.py @@ -20,6 +20,8 @@ from utils.connect_db import connect_db from utils.helpers import * from datetime import datetime +from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST +import time scheduler = AsyncIOScheduler() @@ -31,6 +33,24 @@ app = Quart(__name__) app.config['TESTING']= False +# Create Prometheus metrics +REQUEST_COUNT = Counter('flask_app_request_count', 'Total HTTP requests', ['method', 'endpoint', 'status_code']) +REQUEST_LATENCY = Histogram('flask_app_request_latency_seconds', 'Request latency', ['method', 'endpoint']) + +# Record metrics for each request +@app.before_request +def before_request(): + request.start_time = time.time() + +@app.after_request +def after_request(response): + # Calculate request latency + latency = time.time() - request.start_time + # Record metrics + REQUEST_LATENCY.labels(request.method, request.path).observe(latency) + REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc() + return response + async def get_github_data(code, discord_id): @@ -446,5 +466,10 @@ async def start_scheduler(): scheduler.start() +# Metrics route for Prometheus scraping +@app.route('/metrics') +def metrics(): + return generate_latest(), 200, {'Content-Type': CONTENT_TYPE_LATEST} + if __name__ == '__main__': app.run() \ No newline at end of file diff --git a/prometheus.yml b/prometheus.yml new file mode 100644 index 0000000..989581d --- /dev/null +++ b/prometheus.yml @@ -0,0 +1,7 @@ +global: + scrape_interval: 15s # Scrape metrics every 15 seconds + +scrape_configs: + - job_name: 'flask_app' + static_configs: + - targets: ['localhost:5000'] # Replace with your Flask app's address if necessary \ No newline at end of file From a841dd5f2b1f9ab86de81bd42ab27d6b51a8c8bd Mon Sep 17 00:00:00 2001 From: jaanbaaz Date: Tue, 17 Sep 2024 12:58:40 +0530 Subject: [PATCH 2/2] prometheus.yml file updated --- prometheus.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prometheus.yml b/prometheus.yml index 989581d..512314c 100644 --- a/prometheus.yml +++ b/prometheus.yml @@ -2,6 +2,6 @@ global: scrape_interval: 15s # Scrape metrics every 15 seconds scrape_configs: - - job_name: 'flask_app' + - job_name: 'prometheus' static_configs: - - targets: ['localhost:5000'] # Replace with your Flask app's address if necessary \ No newline at end of file + - targets: ['localhost:9090'] # Replace with your Flask app's address if necessary \ No newline at end of file