-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle.py
More file actions
28 lines (20 loc) · 767 Bytes
/
Copy pathlifecycle.py
File metadata and controls
28 lines (20 loc) · 767 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
"""Lifecycle management for metrics server.
Handles Prometheus metrics server startup and port checks.
"""
import socket
from loguru import logger
from prometheus_client import start_http_server
def start_metrics_server(port: int) -> None:
"""Start the Prometheus metrics server on the specified port."""
if _port_in_use(port):
logger.error("Port {} already in use; metrics disabled", port)
return
logger.info("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