portal: log and retry ariadne calls
This commit is contained in:
parent
5e67b47a16
commit
3eaf3b9151
@ -1,5 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@ -7,6 +9,8 @@ from flask import jsonify, request
|
|||||||
|
|
||||||
from . import settings
|
from . import settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AriadneError(Exception):
|
class AriadneError(Exception):
|
||||||
def __init__(self, message: str, status_code: int = 502) -> None:
|
def __init__(self, message: str, status_code: int = 502) -> None:
|
||||||
@ -39,17 +43,38 @@ def request_raw(
|
|||||||
if not enabled():
|
if not enabled():
|
||||||
raise AriadneError("ariadne not configured", 503)
|
raise AriadneError("ariadne not configured", 503)
|
||||||
|
|
||||||
|
url = _url(path)
|
||||||
|
attempts = max(1, settings.ARIADNE_RETRY_COUNT)
|
||||||
|
for attempt in range(1, attempts + 1):
|
||||||
try:
|
try:
|
||||||
with httpx.Client(timeout=settings.ARIADNE_TIMEOUT_SEC) as client:
|
with httpx.Client(timeout=settings.ARIADNE_TIMEOUT_SEC) as client:
|
||||||
return client.request(
|
resp = client.request(
|
||||||
method,
|
method,
|
||||||
_url(path),
|
url,
|
||||||
headers=_auth_headers(),
|
headers=_auth_headers(),
|
||||||
json=payload,
|
json=payload,
|
||||||
params=params,
|
params=params,
|
||||||
)
|
)
|
||||||
|
if resp.status_code >= 500:
|
||||||
|
logger.warning(
|
||||||
|
"ariadne error response",
|
||||||
|
extra={"method": method, "path": path, "status": resp.status_code},
|
||||||
|
)
|
||||||
|
return resp
|
||||||
except httpx.RequestError as exc:
|
except httpx.RequestError as exc:
|
||||||
|
logger.warning(
|
||||||
|
"ariadne request failed",
|
||||||
|
extra={
|
||||||
|
"method": method,
|
||||||
|
"path": path,
|
||||||
|
"attempt": attempt,
|
||||||
|
"timeout_sec": settings.ARIADNE_TIMEOUT_SEC,
|
||||||
|
"error": str(exc),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if attempt >= attempts:
|
||||||
raise AriadneError("ariadne unavailable", 502) from exc
|
raise AriadneError("ariadne unavailable", 502) from exc
|
||||||
|
time.sleep(settings.ARIADNE_RETRY_BACKOFF_SEC * attempt)
|
||||||
|
|
||||||
|
|
||||||
def proxy(
|
def proxy(
|
||||||
|
|||||||
@ -50,6 +50,8 @@ KEYCLOAK_ADMIN_REALM = os.getenv("KEYCLOAK_ADMIN_REALM", KEYCLOAK_REALM)
|
|||||||
|
|
||||||
ARIADNE_URL = os.getenv("ARIADNE_URL", "").strip()
|
ARIADNE_URL = os.getenv("ARIADNE_URL", "").strip()
|
||||||
ARIADNE_TIMEOUT_SEC = float(os.getenv("ARIADNE_TIMEOUT_SEC", "10"))
|
ARIADNE_TIMEOUT_SEC = float(os.getenv("ARIADNE_TIMEOUT_SEC", "10"))
|
||||||
|
ARIADNE_RETRY_COUNT = int(os.getenv("ARIADNE_RETRY_COUNT", "2"))
|
||||||
|
ARIADNE_RETRY_BACKOFF_SEC = float(os.getenv("ARIADNE_RETRY_BACKOFF_SEC", "0.2"))
|
||||||
|
|
||||||
ACCOUNT_ALLOWED_GROUPS = [
|
ACCOUNT_ALLOWED_GROUPS = [
|
||||||
g.strip()
|
g.strip()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user