Non-stop user warning messages when running Sanic + Uvicorn + New Relic

I ran into some issues running sanic in ASGI mode with New Relic. NR added support for Uvicorn recently, but using New Relic with sanic + uvicorn will give User Warning on every single request:

UserWarning:

You have set a listener for "before_server_start" in ASGI mode. It will be executed as early as possible, but not before the ASGI server is started

This warning does show up on server start when not using NR. But it happens once, and so while annoying it’s tolerable.

When the app is started with uvicorn and newrelic-admin, this warning shows up on every request to any endpoint. It is as if the app got restarted on every request (except that I don’t think that the app is restarting because none of my server start scripts get run and I don’t get the startup logs either). And while it does not break the app, it is extremely annoying to see this message on every request.

Versions:

newrelic==5.20.0.149
sanic==20.6.3
Python 3.7.7
Python 3.8.2

I have tried a few variations of commands that give this behavior:

Version 1: NR gunicorn app with UvicornWorker - gives repeated warning

newrelic-admin \
    run-program \
    /path/to/bin/gunicorn \
    app.app:app \
    --bind 0.0.0.0:8000 \
    --workers=4 \
    --backlog 2048 \
    --worker-class uvicorn.workers.UvicornWorker \
    --timeout 30 \
    --keep-alive 30 \
    --worker-connections 1000 \
    --max-requests 30000 \
    --max-requests-jitter 5000

Version 2: NR uvicorn app - gives repeated warning

newrelic-admin \
    run-program \
    /path/to/bin/uvicorn \
    app.app:app \
    --host 0.0.0.0 \
    --port 8000 \
    --workers=4 \
    --backlog 2048 \
    --loop uvloop \
    --no-access-log \
    --proxy-headers 

Version 3: NR sanic app - eventually I gave up and settled on just running sanic directly. This will not give any warning, but is also expected since uvicorn is not involved

newrelic-admin \
    run-program \
    /path/to/bin/python \
    -m sanic \
    app.app.app \
    --host=0.0.0.0 \
    --port=8000 \
    --workers=4

Any help would be appreciated — if you think that this is only related to NR then I will try to resolve with them. You are just generally a lot more helpful than the support that they give me!

Also, if there is any way I could just disable this warning, that would be helpful also — because like I said, it doesn’t actually affect the app at all, it‘s just that seeing this warning every second pollutes the log tremendously.

Thank you!

Not familiar with new relic. But, if you are getting that on EVERY request, then Sanic is being started on every request. Sounds like a highly inefficient architecture.

Alright. I will talk to them about it.

They are quite a big company for APM and recently added Sanic to their officially supported frameworks, so I thought that perhaps they have worked with someone here… I guess not!

https://docs.newrelic.com/docs/agents/python-agent/getting-started/compatibility-requirements-python-agent

I’m not sure what version they’re actually supporting; the link to sanic they provide hasn’t been used for more than 2 years.

This is also only apparently for WSGI support, which means you must run the server in synchronous mode. There doesn’t appear to be any async support - and no ASGI support. Which is likely the issue you’re running into.

From https://docs.newrelic.com/docs/agents/python-agent/installation/standard-python-agent-install

Start or restart your Python web application or WSGI server. Wait five minutes and look for data in your New Relic account.

I am using their latest release.

Python Agent 5.20.0.149

Notes

This release of the Python agent adds instrumentation for uvicorn and asyncpg .

The agent can be installed using easy_install/pip/distribute via the Python Package Index or can be downloaded > directly from the New Relic download site .

New Features

  • Adds uvicorn instrumentation ASGI applications are now automatically wrapped when using the uvicorn ASGI server.
  • Adds asyncpg instrumentation Support has been added for recording database transactions when using asyncpg database client module for PostgresSQL.

https://docs.newrelic.com/docs/release-notes/agent-release-notes/python-release-notes

I’d suggest you open a ticket with New Relic - it looks like they autogenerate the agent configuration which seems like it should attach to uvicorn, not to sanic directly… but I don’t know enough about the python agent to provide any more direction.

Update: I have opened a ticket with New Relic and they gave me this reply:

As of right now, this does not seem to be a compatibility issue with Sanic. More so an issue on how were sending data:

This was followed up with a new release, and I can confirm that the latest release Python Agent 5.20.1.150 fixed the issue.

https://docs.newrelic.com/docs/release-notes/agent-release-notes/python-release-notes/python-agent-5201150

2 Likes

Glad that it got fixed!

Thanks for following up @smlbiobot :beers: