Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion app_python/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, Response
import os
import platform
import socket
Expand All @@ -9,6 +9,9 @@
import time
import uuid
from contextlib import asynccontextmanager
from prometheus_client import Counter, Histogram, Gauge, generate_latest, CONTENT_TYPE_LATEST
import asyncio


HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 5000))
Expand Down Expand Up @@ -39,6 +42,77 @@ def format(self, record):
app = FastAPI()
start_time = datetime.now()

http_requests_total = Counter(
'http_requests_total',
'Total HTTP requests',
['method', 'endpoint', 'status']
)

http_request_duration_seconds = Histogram(
'http_request_duration_seconds',
'HTTP request duration',
['method', 'endpoint']
)

http_requests_in_progress = Gauge(
'http_requests_in_progress',
'HTTP requests currently being processed'
)

# Application-specific metrics

uptime_seconds = Gauge(
'app_uptime_seconds',
'Application uptime in seconds'
)

endpoint_response_size_bytes = Histogram(
'endpoint_response_size_bytes',
'Response payload size in bytes',
['endpoint']
)


@app.middleware("http")
async def dispatch(request, call_next):
if request.url.path == "/metrics":
return await call_next(request)

http_requests_in_progress.inc()

start_time = time.time()
status_code = 500
response = None
try:
response = await call_next(request)
status_code = response.status_code
except Exception as e:
status_code = 500
http_requests_in_progress.dec()
raise
finally:
duration = time.time() - start_time

http_requests_total.labels(
method=request.method,
endpoint=request.url.path,
status=status_code
).inc()

http_request_duration_seconds.labels(
method=request.method,
endpoint=request.url.path
).observe(duration)

if response and hasattr(response, 'body'):
response_size = len(response.body)
endpoint_response_size_bytes.labels(
endpoint=request.url.path).observe(response_size)

http_requests_in_progress.dec()

return response


@app.middleware("http")
async def log_requests(request: Request, call_next):
Expand Down Expand Up @@ -88,9 +162,25 @@ async def lifespan(app: FastAPI):
logger.info("Application starting up", extra={
"extra_info": {"config": startup_config}})

async def update_uptime():
while True:
uptime_seconds.set(get_uptime()['seconds'])
await asyncio.sleep(5) # Update every 5 seconds

uptime_task = asyncio.create_task(update_uptime())

yield

logger.info("Application shutting down")
uptime_task.cancel()


@app.get('/metrics')
def metrics():
return Response(
generate_latest(),
media_type=CONTENT_TYPE_LATEST
)


@app.get("/")
Expand Down
3 changes: 2 additions & 1 deletion app_python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ jsonschema==4.23.0
flake8==7.3.0
mccabe==0.7.0
pycodestyle==2.14.0
pyflakes==3.4.0
pyflakes==3.4.0
prometheus-client==0.23.1
27 changes: 27 additions & 0 deletions monitoring/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ services:
cpus: '0.5'
memory: 256M

prometheus:
image: prom/prometheus:v3.9.0
container_name: prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.retention.time=15d'
- '--storage.tsdb.retention.size=10GB'
ports:
- 9090:9090
restart: unless-stopped
volumes:
- ./prometheus:/etc/prometheus
- prometheus-data:/prometheus
networks:
- logging
deploy:
resources:
limits:
cpus: '1.0'
memory: 1G
healthcheck:
test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:9090/-/healthy || exit 1"]
interval: 10s
timeout: 5s
retries: 5

grafana:
image: grafana/grafana:12.3.1
container_name: grafana
Expand Down Expand Up @@ -87,6 +113,7 @@ services:
volumes:
loki-data:
grafana-data:
prometheus-data:

networks:
logging:
Loading
Loading