newrelic / newrelic-python-agent

New Relic Python Agent
https://docs.newrelic.com/docs/agents/python-agent
Apache License 2.0
176 stars 99 forks source link

"TypeError: int() argument must be a string" with ASGI + Django: #1150

Closed artemgordinskiy closed 3 months ago

artemgordinskiy commented 4 months ago

Since instrumenting my Django application, I started seeing these errors:

[ERROR] Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/newrelic/api/asgi_application.py", line 357, in nr_async_asgi
    return await coro
           ^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/newrelic/common/async_proxy.py", line 124, in throw
    return self.__wrapped__.throw(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/newrelic/api/asgi_application.py", line 69, in __call__
    return await self.app(scope, receive, self.send_inject_browser_agent)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
    return await application(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/django/asgi.py", line 101, in sentry_patched_asgi_handler
    return await middleware(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/asgi.py", line 144, in _run_asgi3
    return await self._run_app(scope, receive, send, asgi_version=3)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/asgi.py", line 235, in _run_app
    raise exc from None
  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/asgi.py", line 230, in _run_app
    return await self.app(
           ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/newrelic/api/asgi_application.py", line 307, in nr_async_asgi
    return await wrapped(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/asgi.py", line 170, in __call__
    await self.handle(scope, receive, send)
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/asgi.py", line 216, in handle
    task.result()
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/asgi.py", line 195, in process_request
    await self.send_response(response, send)
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/asgi.py", line 357, in send_response
    await send(
  File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/asgi.py", line 223, in _sentry_wrapped_send
    return await send(event)
           ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/newrelic/api/asgi_application.py", line 177, in send_inject_browser_agent
    content_length = int(header_value)
                     ^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

Description

This error occurs only on some requests.

Expected Behavior

No error happening, which was the case before I installed the SDK.

Troubleshooting or NR Diag results

Steps to Reproduce

  1. Install sentry_sdk
  2. Make an HTTP request

Your Environment

Run command:

[”/usr/local/bin/gunicorn”, “–bind”, “0.0.0.0:8000”, “–workers”, “1”, “–threads”, “8”, “–timeout”, “0”, “example.asgi:application”]

Runtime Name: CPython Version: 3.11.9(3.11.9 (main, May 14 2024, 06:28:33) [GCC 12.2.0])

Packages

Package Version
aiofiles 23.2.1
aiohttp 3.9.4
asgiref 3.7.2
channels 4.1.0
channels-redis 4.2.0
django 5.0.6
django-allauth 0.62.1
django-allauth-2fa 0.11.1
django-cors-headers 4.3.1
django-environ 0.11.2
django-hijack 3.4.5
django-htmx 1.17.3
django-otp 1.3.0
django-redis 5.4.0
django-storages 1.14.3
django-timezone-field 6.1.0
django-waffle 4.1.0
djangorestframework 3.15.1
gunicorn 22.0.0
httptools 0.6.1
httpx 0.27.0
newrelic 9.9.0
psycopg 3.1.18
psycopg-c 3.1.18
sqlparse 0.4.4
uvicorn 0.29.0
uvloop 0.19.0
websockets 12.0
sentry.python.django 2.1.1

Additional context

workato-integration[bot] commented 4 months ago

https://new-relic.atlassian.net/browse/NR-273265

hmstepanek commented 3 months ago

This looks like it's a bug in our asgi wrapper browser injection instrumentation. It doesn't handle the case where there is no content-length header. I've opened a PR to fix this issue.