Skip to content

Commit

Permalink
Move proxy_error to util.proxy_error for reuse
Browse files Browse the repository at this point in the history
We can move our proxy_error wrapper so that it can be reused in our
PromqlQuery View to keep things consistent.
  • Loading branch information
kfdm committed Jul 3, 2024
1 parent ccb7bd5 commit fa655d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
27 changes: 5 additions & 22 deletions promgen/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@
logger = logging.getLogger(__name__)


def proxy_error(response):
"""
Return a wrapped proxy error
Taking a request.response object as input, return it slightly modified
with an extra header for debugging so that we can see where the request
failed
"""
r = HttpResponse(
response.content,
content_type=response.headers["content-type"],
status=response.status_code,
)
r.setdefault("X-PROMGEN-PROXY", response.url)
return r


class PrometheusProxy(View):
proxy_headers = {"HTTP_REFERER": "Referer"}

Expand Down Expand Up @@ -91,7 +74,7 @@ def get(self, request):
data.update(_json["data"])
except HTTPError:
logger.warning("Error with response")
return proxy_error(result)
return util.proxy_error(result)

return JsonResponse({"status": "success", "data": sorted(data)})

Expand All @@ -108,7 +91,7 @@ def get(self, request, label):
data.update(_json["data"])
except HTTPError:
logger.warning("Error with response")
return proxy_error(result)
return util.proxy_error(result)

return JsonResponse({"status": "success", "data": sorted(data)})

Expand All @@ -125,7 +108,7 @@ def get(self, request):
data += _json["data"]
except HTTPError:
logger.warning("Error with response")
return proxy_error(result)
return util.proxy_error(result)

return JsonResponse({"status": "success", "data": data})

Expand All @@ -143,7 +126,7 @@ def get(self, request):
data += _json["data"]["result"]
resultType = _json["data"]["resultType"]
except HTTPError:
return proxy_error(result)
return util.proxy_error(result)

return JsonResponse(
{"status": "success", "data": {"resultType": resultType, "result": data}}
Expand All @@ -163,7 +146,7 @@ def get(self, request):
data += _json["data"]["result"]
resultType = _json["data"]["resultType"]
except HTTPError:
return proxy_error(result)
return util.proxy_error(result)

return JsonResponse(
{"status": "success", "data": {"resultType": resultType, "result": data}}
Expand Down
17 changes: 17 additions & 0 deletions promgen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import requests
from django.conf import settings
from django.db.models import F
from django.http import HttpResponse

# Wrappers around request api to ensure we always attach our user agent
# https://github.com/requests/requests/blob/master/requests/api.py
Expand Down Expand Up @@ -120,6 +121,22 @@ def wrapped(field):

return wrapped

def proxy_error(response: requests.Response) -> HttpResponse:
"""
Return a wrapped proxy error
Taking a request.response object as input, return it slightly modified
with an extra header for debugging so that we can see where the request
failed
"""
r = HttpResponse(
response.content,
content_type=response.headers["content-type"],
status=response.status_code,
)
r.setdefault("X-PROMGEN-PROXY", response.url)
return r


# Comment wrappers to get the docstrings from the upstream functions
get.__doc__ = requests.get.__doc__
Expand Down
6 changes: 1 addition & 5 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,10 +1404,6 @@ def get(self, request):
response = util.get(f"{shard.url}/api/v1/query", params=params, headers=headers)
response.raise_for_status()
except HTTPError:
return HttpResponse(
response.content,
content_type=response.headers["content-type"],
status=response.status_code,
)
return util.proxy_error(response)

return HttpResponse(response.content, content_type="application/json")

0 comments on commit fa655d1

Please sign in to comment.