Syntax
newrelic.agent.generator_trace(name=None, group=None, label=None, params=None, terminal=False)Used to instrument generators that aren't instrumented by default.
Description
generator_trace is a decorator for adding to generator functions. Adding this decorator lets you collect additional transaction information (including transaction trace information).
The generator_trace decorator can be used on generators and coroutines with agent version 2.102.0.85 or higher. Timing of these objects begins when consumption starts, and ends when the object is exhausted or goes out of scope.
You can use the decorator in conjunction with existing decorators, including those for static and class methods. When nesting multiple decorators, have the generator_trace decorator as the outermost decorator.
Alternate call forms
For setups where you cannot use the decorator, these alternate call forms are available:
The wrapper
If you know in advance where the specific generators you want to trace are, you could use the generator_trace decorator. However, if you don't know all the generators that need to be traced (for example, if they're being looked up dynamically as part of a routing system), then you must use the GeneratorTraceWrapper to wrap the generator at the time of registration or at the time of calling.
Path-based wrapping
wrap_generator_trace is used for wrapping generators outside of the code they're declared in. For example: you might use this to instrument library code that you don't want to modify.
For more about the differences between these call formats, see Different call formats.
Parameters
Parameters for decorator
newrelic.agent.generator_trace(name=None, group=None, label=None, params=None)This call includes these parameters:
Parameter | Description |
|---|---|
string | Optional. The generator function name. If not set, defaults to the captured name of the generator function. |
string | Optional. The If not supplied, the group will default to |
string | Optional. Adds a callout-style flag to the segment in a transaction trace. Default is |
dict | Optional. Custom parameters to add to the segment in transaction traces. |
Wrapper parameters
newrelic.agent.GeneratorTraceWrapper(wrapped, name=None, group=None, label=None, params=None)Parameters for the wrapper include all parameters for generator_trace and a wrapped parameter:
Parameter | Description |
|---|---|
function | Required. The generator function being wrapped. |
string or function | Optional. The generator function name. Could be a function that accepts the same parameters as the generator function being wrapped. If not set, defaults to the captured name of the generator function. |
string or function | Optional. The If not supplied, the group will default to |
string or function | Optional. Adds a callout-style flag to the segment in a transaction trace. Default is |
dict or function | Optional. Custom parameters to add to the segment in transaction traces. Could be a function that accepts the same parameters as the generator function being wrapped. |
Path-based wrapping parameters
newrelic.agent.wrap_generator_trace(module, object_path, name=None, group=None, label=None, params=None)Parameters include all parameters for generator_trace and these parameters:
Parameter | Description |
|---|---|
object | Required. The module containing the generator to be instrumented. |
string | The path to the location of the generator. |
Examples
generator_trace example
An example of using the generator_trace decorator:
import newrelic.agent
@newrelic.agent.generator_trace()def gen(): for i in range(5): yield iAn example of using the generator_trace decorator with async generator:
import newrelic.agent
@newrelic.agent.generator_trace()async def gen(): for i in range(5): yield iWrapper example
An example of using the GeneratorTraceWrapper:
import newrelic.agent
def gen(): for i in range(5): yield i
gen = newrelic.agent.GeneratorTraceWrapper(gen)If you want to name the metric after the endpoint name (rather than naming the metric based on the identifier for the generator being called), you can supply the name to use plus an alternate metric path prefix when the GeneratorTraceWrapper object is created.
import newrelic.agent
def gen(): for i in range(5): yield i
gen = newrelic.agent.GeneratorTraceWrapper(gen, name="gen_5", group="Python/Generator")If you want to name the metric dynamically (rather than naming the metric based on the identifier for the generator being called), you can supply the name as a generator when the GeneratorTraceWrapper object is created.
import newrelic.agent
def gen(x): for i in range(x): yield i
gen = newrelic.agent.GeneratorTraceWrapper(gen, name = lambda x: f"gen_{x}")