forked from PrefectHQ/prometheus-prefect-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (51 loc) · 1.9 KB
/
main.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import logging
import time
from metrics.metrics import PrefectMetrics
from metrics.healthz import PrefectHealthz
from prometheus_client import start_http_server, REGISTRY
if __name__ == "__main__":
"""
Main entry point for the PrefectMetrics exporter.
"""
# Get environment variables or use default values
loglevel = str(os.getenv("LOG_LEVEL", "INFO"))
max_retries = int(os.getenv("MAX_RETRIES", "3"))
metrics_port = int(os.getenv("METRICS_PORT", "8000"))
offset_minutes = int(os.getenv("OFFSET_MINUTES", "5"))
url = str(os.getenv("PREFECT_API_URL", "https://localhost/api"))
api_key = str(os.getenv("PREFECT_API_KEY", ""))
# Configure logging
logging.basicConfig(level=loglevel, format='%(asctime)s - %(name)s - [%(levelname)s] %(message)s')
logger = logging.getLogger("prometheus-prefect-exporter")
# Configure headers for HTTP requests
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
if api_key:
headers['Authorization'] = f"Bearer {api_key}"
# check endpoint
PrefectHealthz(
url = url,
headers = headers,
max_retries = max_retries,
logger = logger
).get_health_check()
# Create an instance of the PrefectMetrics class
metrics = PrefectMetrics(
url = url,
headers = headers,
offset_minutes = offset_minutes,
max_retries = max_retries,
logger = logger
)
# Register the metrics with Prometheus
logger.info(f"Inizializing metrics...")
REGISTRY.register(metrics)
# Start the HTTP server to expose Prometheus metrics
start_http_server(metrics_port)
logger.info(f"Exporter listening on port :{metrics_port}")
# Run the loop to collect Prefect metrics
while True:
time.sleep(5)