diff --git a/trustgraph-flow/trustgraph/gateway/endpoint/metrics.py b/trustgraph-flow/trustgraph/gateway/endpoint/metrics.py index d17d111b..903a199c 100644 --- a/trustgraph-flow/trustgraph/gateway/endpoint/metrics.py +++ b/trustgraph-flow/trustgraph/gateway/endpoint/metrics.py @@ -47,26 +47,37 @@ class MetricsEndpoint: if not self.auth.permitted(token, self.operation): return web.HTTPUnauthorized() + path = request.match_info["path"] + url = ( + self.prometheus_url + "/api/v1/" + path + "?" + + request.query_string + ) + try: - path = request.match_info["path"] - async with aiohttp.ClientSession() as session: - - url = ( - self.prometheus_url + "/api/v1/" + path + "?" + - request.query_string - ) - async with session.get(url) as resp: return web.Response( status=resp.status, text=await resp.text() ) + except aiohttp.ClientConnectionError as e: + + # Upstream unreachable (connect refused, DNS failure, + # server disconnect). Distinguish from our own errors so + # callers know where the fault is. + logger.error(f"Metrics upstream {url} unreachable: {e}") + return web.Response( + status=502, + text=f"Bad Gateway: metrics upstream unreachable: {e}", + ) + except Exception as e: - logging.error(f"Exception: {e}") - - raise web.HTTPInternalServerError() + logger.error(f"Metrics proxy exception: {e}", exc_info=True) + return web.Response( + status=500, + text=f"Internal Server Error: {e}", + )