Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/packages/python/examples/metr…
Browse files Browse the repository at this point in the history
…ics_django/django-3.2.20
  • Loading branch information
domharrington authored Jul 26, 2023
2 parents f0e2597 + 609a644 commit e76fc41
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 69 deletions.
132 changes: 66 additions & 66 deletions packages/node/package-lock.json

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

4 changes: 2 additions & 2 deletions packages/python/readme_metrics/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __call__(self, request):
try:
request.rm_start_dt = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
request.rm_start_ts = int(time.time() * 1000)
if request.headers["Content-Length"] or request.body:
request.rm_content_length = request.headers["Content-Length"] or "0"
if request.headers.get("Content-Length") or request.body:
request.rm_content_length = request.headers.get("Content-Length") or "0"
request.rm_body = request.body or ""
except Exception as e:
# Errors in the Metrics SDK should never cause the application to
Expand Down
13 changes: 12 additions & 1 deletion packages/python/readme_metrics/tests/django_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def test(self):
assert middleware.get_response == get_response
middleware.metrics_core = Mock()

# the middleware should call get_response(request)
request = Mock()
request.headers = {"Content-Length": "123"}
middleware(request)
# the middleware should call get_response(request)
get_response.assert_called_once_with(request)

# the middleware should set request.rm_start_dt to roughly the current
Expand All @@ -52,3 +53,13 @@ def test(self):
assert call_args[0][0] == request
assert isinstance(call_args[0][1], ResponseInfoWrapper)
assert call_args[0][1].headers.get("X-Header") == "X Value!"
assert (
getattr(request, "rm_content_length") == request.headers["Content-Length"]
)

def test_missing_content_length(self):
middleware = MetricsMiddleware(Mock(), config=mock_config)
request = Mock()
request.headers = {}
middleware(request)
assert getattr(request, "rm_content_length") == "0"

0 comments on commit e76fc41

Please sign in to comment.