| 1 |
from secrets import compare_digest |
| 2 |
|
| 3 |
from typing import Optional |
| 4 |
|
| 5 |
from django.contrib.admin.views.decorators import staff_member_required |
| 6 |
from django.http import Http404, HttpRequest, HttpResponse, JsonResponse |
| 7 |
from django.shortcuts import render |
| 8 |
from django.views.decorators.cache import never_cache |
| 9 |
|
| 10 |
from . import settings as django_rq_settings |
| 11 |
from .utils import get_cron_schedulers, get_scheduler_statistics, get_statistics |
| 12 |
from .views import each_context |
| 13 |
|
| 14 |
try: |
| 15 |
import prometheus_client |
| 16 |
|
| 17 |
from .contrib.prometheus import RQCollector |
| 18 |
except ImportError: |
| 19 |
prometheus_client = RQCollector = None # type: ignore[assignment, misc] |
| 20 |
|
| 21 |
registry = None |
| 22 |
|
| 23 |
|
| 24 |
def is_authorized(request: HttpRequest) -> bool: |
| 25 |
api_token = django_rq_settings.get_api_token() |
| 26 |
auth_header = request.headers.get("Authorization", "") |
| 27 |
token = None |
| 28 |
|
| 29 |
if auth_header.startswith("Bearer "): |
| 30 |
token = auth_header.removeprefix("Bearer ").strip() |
| 31 |
|
| 32 |
is_staff = getattr(request.user, 'is_staff', False) |
| 33 |
return bool(is_staff or (api_token and token and compare_digest(api_token, token))) |
| 34 |
|
| 35 |
|
| 36 |
@never_cache |
| 37 |
def prometheus_metrics(request): |
| 38 |
if not is_authorized(request): |
| 39 |
return JsonResponse( |
| 40 |
{ |
| 41 |
"error": True, |
| 42 |
"description": "Missing bearer token. Set token in headers and configure RQ_API_TOKEN in settings.py", |
| 43 |
}, |
| 44 |
status=401, |
| 45 |
) |
| 46 |
|
| 47 |
global registry |
| 48 |
|
| 49 |
if not RQCollector or not prometheus_client: # type: ignore[truthy-function] |
| 50 |
raise Http404('prometheus_client has not been installed; install using extra "django-rq[prometheus]"') |
| 51 |
|
| 52 |
if not registry: |
| 53 |
registry = prometheus_client.CollectorRegistry(auto_describe=True) |
| 54 |
registry.register(RQCollector()) |
| 55 |
|
| 56 |
encoder, content_type = prometheus_client.exposition.choose_encoder(request.META.get('HTTP_ACCEPT', '')) |
| 57 |
if 'name[]' in request.GET: |
| 58 |
registry = registry.restricted_registry(request.GET.getlist('name[]')) |
| 59 |
|
| 60 |
return HttpResponse(encoder(registry), headers={'Content-Type': content_type}) |
| 61 |
|
| 62 |
|
| 63 |
@never_cache |
| 64 |
@staff_member_required |
| 65 |
def stats(request: HttpRequest) -> HttpResponse: |
| 66 |
context_data = { |
| 67 |
**each_context(request), |
| 68 |
**get_statistics(run_maintenance_tasks=True), |
| 69 |
**get_scheduler_statistics(), |
| 70 |
"view_metrics": RQCollector is not None, |
| 71 |
"cron_schedulers": get_cron_schedulers(), |
| 72 |
} |
| 73 |
return render(request, 'django_rq/stats.html', context_data) |
| 74 |
|
| 75 |
|
| 76 |
@never_cache |
| 77 |
def stats_json(request: HttpRequest, token: Optional[str] = None) -> JsonResponse: |
| 78 |
if not is_authorized(request): |
| 79 |
api_token = django_rq_settings.get_api_token() |
| 80 |
if token and compare_digest(token, api_token): |
| 81 |
return JsonResponse(get_statistics()) |
| 82 |
else: |
| 83 |
return JsonResponse( |
| 84 |
{ |
| 85 |
"error": True, |
| 86 |
"description": "Missing bearer token. Set token in headers and configure RQ_API_TOKEN in settings.py", |
| 87 |
}, |
| 88 |
status=401, |
| 89 |
) |
| 90 |
|
| 91 |
return JsonResponse(get_statistics()) |