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

Feat/django prometheus #922

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions connect/api/prometheus/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

from django.http import HttpResponseForbidden
from django_prometheus.exports import ExportToDjangoView


def metrics_view(request):
auth_token = request.headers.get("Authorization")
prometheus_auth_token = os.environ.get("PROMETHEUS_AUTH_TOKEN")

expected_token = f"Bearer {prometheus_auth_token}"
if not auth_token or auth_token != expected_token:
return HttpResponseForbidden("Access denied")

return ExportToDjangoView(request)
3 changes: 3 additions & 0 deletions connect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@
"corsheaders",
"django_grpc_framework",
"stripe",
"django_prometheus",
]

MIDDLEWARE = [
"django_prometheus.middleware.PrometheusBeforeMiddleware",
"elasticapm.contrib.django.middleware.TracingMiddleware",
"elasticapm.contrib.django.middleware.Catch404Middleware",
"django.middleware.security.SecurityMiddleware",
Expand All @@ -168,6 +170,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_prometheus.middleware.PrometheusAfterMiddleware",
]

ROOT_URLCONF = "connect.urls"
Expand Down
2 changes: 2 additions & 0 deletions connect/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
)
from connect.billing.views import StripeHandler
from connect.api.v2 import routers as api_v2_urls
from connect.api.prometheus.view import metrics_view


api_v2_urls = [path("", include(api_v2_urls))]
Expand All @@ -51,6 +52,7 @@
path("v1/", include(rookly_api_v1_urls)),
path("v2/", include(api_v2_urls)),
url(r"^handlers/stripe/$", StripeHandler.as_view(), name="handlers.stripe_handler"),
path("api/prometheus/metrics", metrics_view, name="metrics_view"),
]

urlpatterns += staticfiles_urlpatterns()
Expand Down
33 changes: 30 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ gevent = "22.10.2"
setuptools = "*"
factory-boy = "^3.3.0"
pyjwt = "^2.9.0"
django-prometheus = "^2.3.1"

[tool.poetry.dev-dependencies]
flake8 = "~=3.9.2"
Expand Down
Loading