Files
startup-product/pricing-service/recovery/monitor-production.py

54 lines
1.3 KiB
Python

import datetime
import json
import time
import urllib.request
import urllib.error
URL = "https://pricing.nageshwaran.dev/health"
print("timestamp_utc,http_status,healthy,version,latency_ms",
flush=True)
try:
while True:
started = time.monotonic()
timestamp = datetime.datetime.now(
datetime.timezone.utc
).isoformat(timespec="milliseconds")
status = 0
healthy = False
version = "unknown"
try:
with urllib.request.urlopen(URL, timeout=3) as response:
status = response.status
data = json.load(response)
healthy = (
status == 200
and data.get("status") == "healthy"
)
version = data.get("version", "not-reported")
except urllib.error.HTTPError as error:
status = error.code
except Exception:
pass
latency = round(
(time.monotonic() - started) * 1000
)
print(
f"{timestamp},{status},{str(healthy).lower()},"
f"{version},{latency}",
flush=True
)
elapsed = time.monotonic() - started
time.sleep(max(0, 1 - elapsed))
except KeyboardInterrupt:
pass