-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
674 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
urlpatterns = [] | ||
from django.urls import path | ||
|
||
from myhpi.core.views import metrics_view | ||
|
||
urlpatterns = [path("metrics", metrics_view, name="prometheus-django-metrics")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
# Create your views here. | ||
import prometheus_client | ||
from django.conf import settings | ||
from django.http import HttpResponse | ||
|
||
|
||
def metrics_view(request): | ||
provided_key = request.headers.get("X-API-KEY") | ||
if provided_key and provided_key == settings.METRICS_API_KEY: | ||
metrics_page = prometheus_client.generate_latest(prometheus_client.REGISTRY) | ||
return HttpResponse(metrics_page, content_type=prometheus_client.CONTENT_TYPE_LATEST) | ||
else: | ||
return HttpResponse("Unauthorized", status=401) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from myhpi.tests.core.utils import MyHPIPageTestCase | ||
|
||
|
||
class MetricsTests(MyHPIPageTestCase): | ||
def test_unauthorized_metrics(self): | ||
with self.settings(METRICS_API_KEY="TEST_KEY"): | ||
response = self.client.get("/metrics") | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_authorized_metrics(self): | ||
with self.settings(METRICS_API_KEY="TEST_KEY"): | ||
response = self.client.get("/metrics", **{"HTTP_X-API-KEY": "TEST_KEY"}) | ||
self.assertEqual(response.status_code, 200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters