-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle.py
More file actions
31 lines (21 loc) · 829 Bytes
/
Copy pathlifecycle.py
File metadata and controls
31 lines (21 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Lifecycle management for metrics server.
Handles Prometheus metrics server startup and port checks.
"""
import socket
from prometheus_client import start_http_server
from sample_python_app.core.logging import setup_logger
logger = setup_logger("normal")
def start_metrics_server(port: int) -> None:
"""Start the Prometheus metrics server on the specified port."""
if _port_in_use(port):
logger.error(f"Port {port} already in use; metrics disabled")
return
logger.info(f"Starting Prometheus metrics on 0.0.0.0:{port}")
start_http_server(port, addr="0.0.0.0")
def _port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.bind(("localhost", port))
except OSError:
return True
return False