Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Move proxy_error to util.proxy_error for reuse #527

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")