Skip to content

Commit 2086037

Browse files
author
Gpshfrd
committed
lab done
1 parent 0861c52 commit 2086037

29 files changed

Lines changed: 329 additions & 6 deletions

ansible/inventory/hosts.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[webservers]
2-
lab04-vm ansible_host=62.84.113.180 ansible_user=ubuntu
2+
lab04-vm ansible_host=158.160.111.187 ansible_user=ubuntu

app_python/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import socket
44
import logging
55
import sys
6+
import time
67
from datetime import datetime, timezone
78
from pythonjsonlogger import jsonlogger
89

910
from flask import Flask, jsonify, request
11+
from prometheus_client import Counter, Histogram, Gauge, generate_latest
1012

1113
### Configuration
1214

@@ -36,6 +38,25 @@
3638

3739
START_TIME = datetime.now(timezone.utc)
3840

41+
### Prometeus metrics
42+
43+
http_requests_total = Counter(
44+
"http_requests_total",
45+
"Total HTTP requests",
46+
["method", "endpoint", "status"]
47+
)
48+
49+
http_request_duration_seconds = Histogram(
50+
"http_request_duration_seconds",
51+
"HTTP request duration in seconds",
52+
["method", "endpoint"]
53+
)
54+
55+
http_requests_in_progress = Gauge(
56+
"http_requests_in_progress",
57+
"Number of HTTP requests in progress"
58+
)
59+
3960
### Helper functions
4061

4162
def get_uptime():
@@ -101,6 +122,10 @@ def health():
101122
"uptime_seconds": uptime_seconds,
102123
})
103124

125+
@app.route("/metrics", methods=["GET"])
126+
def metrics():
127+
return generate_latest(), 200, {"Content-Type": "text/plain"}
128+
104129
### Error Handlers
105130
@app.errorhandler(404)
106131
def not_found(error):
@@ -128,6 +153,10 @@ def handle_error(e):
128153

129154
@app.before_request
130155
def log_request():
156+
request.start_time = time.time()
157+
158+
http_requests_in_progress.inc()
159+
131160
logger.info(
132161
"Request received",
133162
extra={
@@ -139,6 +168,21 @@ def log_request():
139168

140169
@app.after_request
141170
def log_response(response):
171+
duration = time.time() - request.start_time
172+
173+
http_requests_total.labels(
174+
method=request.method,
175+
endpoint=request.path,
176+
status=response.status_code
177+
).inc()
178+
179+
http_request_duration_seconds.labels(
180+
method=request.method,
181+
endpoint=request.path
182+
).observe(duration)
183+
184+
http_requests_in_progress.dec()
185+
142186
logger.info(
143187
"Response sent",
144188
extra={

app_python/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Flask==3.1.0
22
pytest
3-
python-json-logger
3+
python-json-logger
4+
prometheus-client==0.23.1

monitoring/docker-compose.yml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ services:
5555
volumes:
5656
- grafana-data:/var/lib/grafana
5757
environment:
58-
- GF_AUTH_ANONYMOUS_ENABLED=false
58+
- GF_AUTH_ANONYMOUS_ENABLED=true
5959
# - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
6060
networks:
6161
- logging
@@ -76,6 +76,34 @@ services:
7676
retries: 5
7777
start_period: 10s
7878

79+
prometheus:
80+
image: prom/prometheus:v3.9.0
81+
ports:
82+
- "9090:9090"
83+
volumes:
84+
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
85+
- prometheus-data:/prometheus
86+
command:
87+
- '--config.file=/etc/prometheus/prometheus.yml'
88+
- '--storage.tsdb.retention.time=15d'
89+
- '--storage.tsdb.retention.size=10GB'
90+
networks:
91+
- logging
92+
healthcheck:
93+
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:9090/-/healthy || exit 1"]
94+
interval: 10s
95+
timeout: 5s
96+
retries: 5
97+
98+
deploy:
99+
resources:
100+
limits:
101+
cpus: '1.0'
102+
memory: 1G
103+
reservations:
104+
cpus: '0.5'
105+
memory: 512M
106+
79107
app-python:
80108
build: ../app_python
81109
ports:
@@ -94,17 +122,16 @@ services:
94122
reservations:
95123
cpus: '0.25'
96124
memory: 128M
97-
98125
healthcheck:
99-
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5000/health')"]
126+
test: ["CMD-SHELL", "curl -f http://localhost:8000/health || exit 1"]
100127
interval: 10s
101128
timeout: 5s
102129
retries: 5
103-
start_period: 10s
104130

105131
volumes:
106132
loki-data:
107133
grafana-data:
134+
prometheus-data:
108135

109136
networks:
110137
logging:
File renamed without changes.
File renamed without changes.

monitoring/docs/screenshots/curl localhost:8000.png renamed to monitoring/docs/lab07/screenshots/curl localhost:8000.png

File renamed without changes.

monitoring/docs/screenshots/curl localhost:9080 targets.png renamed to monitoring/docs/lab07/screenshots/curl localhost:9080 targets.png

File renamed without changes.

monitoring/docs/screenshots/docker compose ps 1.png renamed to monitoring/docs/lab07/screenshots/docker compose ps 1.png

File renamed without changes.

monitoring/docs/screenshots/docker compose ps 4.png renamed to monitoring/docs/lab07/screenshots/docker compose ps 4.png

File renamed without changes.

0 commit comments

Comments
 (0)