Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 14, 2024
1 parent 7d91672 commit e4ebbe9
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
3 changes: 2 additions & 1 deletion apitally/client/request_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ class RequestDict(TypedDict):
path: Optional[str]
url: str
headers: Dict[str, str]
size: Optional[int]
consumer: Optional[str]


class ResponseDict(TypedDict):
status_code: int
response_time: float
headers: Dict[str, str]
size: int | None
size: Optional[int]


class TempGzipFile:
Expand Down
11 changes: 10 additions & 1 deletion apitally/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import sys
from importlib.metadata import PackageNotFoundError, version
from typing import Dict, Optional
from typing import Dict, Optional, Union


def parse_int(x: Union[str, int, None]) -> Optional[int]:
if x is None:
return None
try:
return int(x)
except ValueError:
return None

Check warning on line 12 in apitally/common.py

View check run for this annotation

Codecov / codecov/patch

apitally/common.py#L11-L12

Added lines #L11 - L12 were not covered by tests


def get_versions(*packages, app_version: Optional[str] = None) -> Dict[str, str]:
Expand Down
17 changes: 5 additions & 12 deletions apitally/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from apitally.client.consumers import Consumer as ApitallyConsumer
from apitally.client.logging import get_logger
from apitally.client.request_logging import RequestLoggingConfig
from apitally.common import get_versions
from apitally.common import get_versions, parse_int


if TYPE_CHECKING:
Expand Down Expand Up @@ -98,10 +98,11 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
response = self.get_response(request)
response_time = time.perf_counter() - start_time
response_size = (
_to_int(response["Content-Length"])
parse_int(response["Content-Length"])
if response.has_header("Content-Length")
else (len(response.content) if not response.streaming else None)
)
request_size = parse_int(request.headers.get("Content-Length"))
path = self.get_path(request)

try:
Expand All @@ -120,7 +121,7 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
path=path,
status_code=response.status_code,
response_time=response_time,
request_size=request.headers.get("Content-Length"),
request_size=request_size,
response_size=response_size,
)
except Exception: # pragma: no cover
Expand Down Expand Up @@ -163,6 +164,7 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
"path": path,
"url": request.build_absolute_uri(),
"headers": dict(request.headers),
"size": request_size,
"consumer": consumer_identifier,
},
response={
Expand Down Expand Up @@ -364,12 +366,3 @@ def _check_import(name: str) -> bool:
return True
except ImportError:
return False


def _to_int(x: Union[str, int, None]) -> Optional[int]:
if x is None:
return None
try:
return int(x)
except ValueError:
return None
1 change: 1 addition & 0 deletions apitally/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def add_request(
"path": path,
"url": request.url,
"headers": dict(request.headers),
"size": request.content_length,
"consumer": consumer_identifier,
},
response={
Expand Down
11 changes: 7 additions & 4 deletions apitally/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from apitally.client.client_asyncio import ApitallyClient
from apitally.client.consumers import Consumer as ApitallyConsumer
from apitally.client.request_logging import RequestLoggingConfig
from apitally.common import get_versions
from apitally.common import get_versions, parse_int


__all__ = ["ApitallyPlugin", "ApitallyConsumer", "RequestLoggingConfig"]
Expand Down Expand Up @@ -122,13 +122,15 @@ def add_request(
response_time: float,
response_headers: Headers,
response_body: bytes,
response_size: int = 0,
response_size: Optional[int] = None,
) -> None:
if response_status < 100:
return # pragma: no cover
path = self.get_path(request)
if self.filter_path(path):
return
request_size = parse_int(request.headers.get("Content-Length"))
response_size = response_size or parse_int(response_headers.get("Content-Length"))

consumer = self.get_consumer(request)
consumer_identifier = consumer.identifier if consumer else None
Expand All @@ -141,8 +143,8 @@ def add_request(
path=path,
status_code=response_status,
response_time=response_time,
request_size=request.headers.get("Content-Length"),
response_size=response_size or response_headers.get("Content-Length"),
request_size=request_size,
response_size=response_size,
)

if response_status == 400 and response_body and len(response_body) < 4096:
Expand Down Expand Up @@ -186,6 +188,7 @@ def add_request(
"path": path,
"url": str(request.url),
"headers": dict(request.headers),
"size": request_size,
"consumer": consumer_identifier,
},
response={
Expand Down
12 changes: 8 additions & 4 deletions apitally/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from apitally.client.client_asyncio import ApitallyClient
from apitally.client.consumers import Consumer as ApitallyConsumer
from apitally.client.request_logging import RequestLoggingConfig
from apitally.common import get_versions
from apitally.common import get_versions, parse_int


__all__ = ["ApitallyMiddleware", "ApitallyConsumer", "RequestLoggingConfig"]
Expand Down Expand Up @@ -117,10 +117,13 @@ def add_request(
response_time: float,
response_headers: Headers,
response_body: bytes,
response_size: int = 0,
response_size: Optional[int] = None,
exception: Optional[BaseException] = None,
) -> None:
path = self.get_path(request)
request_size = parse_int(request.headers.get("Content-Length"))
response_size = response_size or parse_int(response_headers.get("Content-Length"))

consumer = self.get_consumer(request)
consumer_identifier = consumer.identifier if consumer else None
self.client.consumer_registry.add_or_update_consumer(consumer)
Expand All @@ -134,8 +137,8 @@ def add_request(
path=path,
status_code=response_status,
response_time=response_time,
request_size=request.headers.get("Content-Length"),
response_size=response_size or response_headers.get("Content-Length"),
request_size=request_size,
response_size=response_size,
)
if response_status == 422 and response_body and response_headers.get("Content-Type") == "application/json":
with contextlib.suppress(json.JSONDecodeError):
Expand Down Expand Up @@ -163,6 +166,7 @@ def add_request(
"path": path,
"url": str(request.url),
"headers": dict(request.headers),
"size": request_size,
"consumer": consumer_identifier,
},
response={
Expand Down
2 changes: 2 additions & 0 deletions tests/test_client_request_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def test_request_logger():
"path": "/test",
"url": "http://localhost:8000/test?foo=bar",
"headers": {"Accept": "application/json"},
"size": 100,
"consumer": "test",
}
response: ResponseDict = {
"status_code": 200,
Expand Down

0 comments on commit e4ebbe9

Please sign in to comment.