Using trace decorators with Tornado coroutines

There are ordering requirements for using the New Relic Python agent tracing API with applications that use Tornado 4 and higher with agent version v2.102.0.85 and higher.

For general API instructions, see the Python Agent API guide.

Trace decorator ordering

When adding trace API calls to Tornado coroutines that are decorated with tornado.gen.coroutine, decorator ordering is critical to proper instrumentation.

With most trace APIs, the Python agent will automatically detect any attempt to trace a generator or native coroutine. In order for the detection to work correctly, the decorator must be able to examine the underlying function prior to it being wrapped by tornado.gen.coroutine.

Example

In the following example, function_trace is placed immediately above the function definition, which allows the the Python agent to detect it correctly.

import newrelic.agent
import tornado.gen

@tornado.gen.coroutine
@newrelic.agent.function_trace()
def my_coroutine():
  yield tornado.gen.sleep(0)

For more help

Additional documentation resources include:

Recommendations for learning more: