|
31 | 31 | _ACCESS_LOG_TEMPLATE = '%({x-forwarded-for}i)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' |
32 | 32 |
|
33 | 33 |
|
| 34 | +def _should_skip_access_log(request_path: str) -> bool: |
| 35 | + return request_path == "/_ci" or request_path.startswith("/_ci/") |
| 36 | + |
| 37 | + |
34 | 38 | def _format_access_log_timestamp() -> str: |
35 | 39 | return timezone.now().strftime("[%d/%b/%Y:%H:%M:%S %z]") |
36 | 40 |
|
@@ -369,51 +373,52 @@ def __call__(self, request): |
369 | 373 | duration_seconds = time.monotonic() - started_at |
370 | 374 | duration_ms = int(round(duration_seconds * 1000)) |
371 | 375 | request.META["astra.duration_seconds"] = duration_seconds |
372 | | - status_code = response.status_code if response is not None else 500 |
373 | | - if status_code >= 500: |
374 | | - outcome = "server_error" |
375 | | - elif status_code >= 400: |
376 | | - outcome = "client_error" |
377 | | - else: |
378 | | - outcome = "success" |
379 | | - |
380 | | - extra: dict[str, int | str] = { |
381 | | - "event": "astra.http.access", |
382 | | - "component": "http", |
383 | | - "outcome": outcome, |
384 | | - "http_status": status_code, |
385 | | - "request_method": request.method, |
386 | | - "request_path": request.path, |
387 | | - "duration_ms": duration_ms, |
388 | | - } |
389 | | - |
390 | | - request_query = str(request.META.get("QUERY_STRING") or "").strip() |
391 | | - if request_query: |
392 | | - extra["request_query"] = request_query |
393 | | - |
394 | | - request_id = str(request.META.get("HTTP_X_REQUEST_ID") or "").strip() |
395 | | - if request_id: |
396 | | - extra["request_id"] = request_id |
397 | | - |
398 | | - client_ip = _request_client_ip(request) |
399 | | - if client_ip: |
400 | | - extra["client_ip"] = client_ip |
401 | | - |
402 | | - user_id = try_get_username_from_user(request.user) if request.user.is_authenticated else None |
403 | | - if user_id: |
404 | | - extra["user_id"] = user_id |
405 | | - |
406 | | - if error is not None: |
407 | | - extra |= exception_log_fields(error) |
408 | | - |
409 | | - access_atoms = _build_access_log_atoms( |
410 | | - request, |
411 | | - status_code=status_code, |
412 | | - response=response, |
413 | | - client_ip=client_ip, |
414 | | - user_id=user_id, |
415 | | - ) |
416 | | - access_logger.info(_ACCESS_LOG_TEMPLATE, access_atoms, extra=extra) |
| 376 | + if not _should_skip_access_log(request.path): |
| 377 | + status_code = response.status_code if response is not None else 500 |
| 378 | + if status_code >= 500: |
| 379 | + outcome = "server_error" |
| 380 | + elif status_code >= 400: |
| 381 | + outcome = "client_error" |
| 382 | + else: |
| 383 | + outcome = "success" |
| 384 | + |
| 385 | + extra: dict[str, int | str] = { |
| 386 | + "event": "astra.http.access", |
| 387 | + "component": "http", |
| 388 | + "outcome": outcome, |
| 389 | + "http_status": status_code, |
| 390 | + "request_method": request.method, |
| 391 | + "request_path": request.path, |
| 392 | + "duration_ms": duration_ms, |
| 393 | + } |
| 394 | + |
| 395 | + request_query = str(request.META.get("QUERY_STRING") or "").strip() |
| 396 | + if request_query: |
| 397 | + extra["request_query"] = request_query |
| 398 | + |
| 399 | + request_id = str(request.META.get("HTTP_X_REQUEST_ID") or "").strip() |
| 400 | + if request_id: |
| 401 | + extra["request_id"] = request_id |
| 402 | + |
| 403 | + client_ip = _request_client_ip(request) |
| 404 | + if client_ip: |
| 405 | + extra["client_ip"] = client_ip |
| 406 | + |
| 407 | + user_id = try_get_username_from_user(request.user) if request.user.is_authenticated else None |
| 408 | + if user_id: |
| 409 | + extra["user_id"] = user_id |
| 410 | + |
| 411 | + if error is not None: |
| 412 | + extra |= exception_log_fields(error) |
| 413 | + |
| 414 | + access_atoms = _build_access_log_atoms( |
| 415 | + request, |
| 416 | + status_code=status_code, |
| 417 | + response=response, |
| 418 | + client_ip=client_ip, |
| 419 | + user_id=user_id, |
| 420 | + ) |
| 421 | + access_logger.info(_ACCESS_LOG_TEMPLATE, access_atoms, extra=extra) |
417 | 422 |
|
418 | 423 |
|
419 | 424 | class LoginRequiredMiddleware: |
|
0 commit comments