diff --git a/ansible/roles/prometheus/templates/prometheus.yml.j2 b/ansible/roles/prometheus/templates/prometheus.yml.j2 index 8340de1..59a0934 100644 --- a/ansible/roles/prometheus/templates/prometheus.yml.j2 +++ b/ansible/roles/prometheus/templates/prometheus.yml.j2 @@ -3,9 +3,15 @@ global: evaluation_interval: 15s scrape_configs: - - job_name: "node" + - job_name: "node-exporter" static_configs: - targets: - "{{ hostvars['monitoring-1'].ansible_host }}:9100" - "{{ hostvars['app-1'].ansible_host }}:9100" + + - job_name: "flask_app" + metrics_path: /metrics + static_configs: + - targets: + - "{{ hostvars['app-1'].ansible_host }}:80" diff --git a/app/requirements.txt b/app/requirements.txt index e25f291..cc69717 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1,3 +1,4 @@ flask==3.0.0 gunicorn==21.2.0 +prometheus-client diff --git a/app/src/routes/metrics.py b/app/src/routes/metrics.py index 0a7cb54..f3cf94f 100644 --- a/app/src/routes/metrics.py +++ b/app/src/routes/metrics.py @@ -1,15 +1,27 @@ -import random import time -from flask import Blueprint, jsonify +from flask import Blueprint +from prometheus_client import CONTENT_TYPE_LATEST, Gauge, generate_latest metrics_bp = Blueprint("metrics", __name__) start_time = time.time() +uptime_gauge = Gauge("app_uptime_seconds", "App uptime in seconds") +random_gauge = Gauge("app_random_value", "Random value") -@metrics_bp.route("/metrics/custom") +start_time = time.time() + + +@metrics_bp.route("/metrics") def metrics(): - uptime = time.time() - start_time - random_value = random.randint(1, 100) - return jsonify({"uptime_seconds": uptime, "random_value": random_value}), 200 + uptime_gauge.set(time.time() - start_time) + # random_gauge.set(...) + return generate_latest(), 200, {"Content-Type": CONTENT_TYPE_LATEST} + + +# @metrics_bp.route("/metrics/custom") +# def metrics_custom(): +# uptime = time.time() - start_time +# random_value = random.randint(1, 100) +# return jsonify({"uptime_seconds": uptime, "random_value": random_value}), 200