Working code (python 3.5+):
import os
import newrelic.agent
newrelic_app_register_timeout = int(os.environ.get('NEW_RELIC_APP_REGISTER_TIMEOUT', 10))
newrelic_app = newrelic.agent.register_application(timeout=newrelic_app_register_timeout)
def async_newrelic_background_task(f):
async def wrapper(*args, **kwargs):
transaction = newrelic.agent.current_transaction()
if transaction:
return await f(*args, **kwargs)
name = newrelic.agent.callable_name(f)
with newrelic.agent.BackgroundTask(newrelic_app, name):
return await f(*args, **kwargs)
return wrapper
How to use:
from my_module import async_newrelic_background_task
@async_newrelic_background_task
async def my_coroutine():
await sleep(0.5)
return 'my_coroutine'