---
title: generator_trace (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/generatortrace-python-agent-api
---

## Syntax

```py
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](https://docs.newrelic.com/docs/accounts-partnerships/education/getting-started-new-relic/glossary#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 [#alternate]

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 [#path-based]

`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](https://docs.newrelic.com/docs/python-agent-api-different-call-forms).

## Parameters

### Parameters for decorator [#main-parameters]

```py
newrelic.agent.generator_trace(name=None, group=None, label=None, params=None)
```

This call includes these parameters:

| Parameter        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` _string_  | Optional. The generator function name. If not set, defaults to the captured name of the generator function.                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `group` _string_ | Optional. The `group` represents the naming structure for the `name` parameter. This is used in the UI for segregating the [transaction types](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page#tx_functions). If not supplied, the group will default to `Function` in expectation that the name is of the form `module:class.generator` or `module:generator` and represents the name of the generator function being executed. If you are creating a custom group, it's recommended you prefix it with `Python/`. |
| `label` _string_ | Optional. Adds a callout-style flag to the segment in a transaction trace. Default is `None`.                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `params` _dict_  | Optional. Custom parameters to add to the segment in transaction traces.                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |

### Wrapper parameters

```py
newrelic.agent.GeneratorTraceWrapper(wrapped, name=None, group=None, label=None, params=None)
```

Parameters for the wrapper include all [parameters](#main-parameters) for `generator_trace` and a `wrapped` parameter:

| Parameter                      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `wrapped` _function_           | Required. The generator function being wrapped.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `name` _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.                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `group` _string_ or _function_ | Optional. The `group` represents the naming structure for the `name` parameter. This is used in the UI for segregating the [transaction types](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page#tx_functions). If not supplied, the group will default to `Function` in expectation that the name is of the form `module:class.generator` or `module:generator` and represents the name of the generator function being executed. If you are creating a custom group, it's recommended you prefix it with `Python/`. Could be a function that accepts the same parameters as the generator function being wrapped. |
| `label` _string_ or _function_ | Optional. Adds a callout-style flag to the segment in a transaction trace. Default is `None`. Could be a function that accepts the same parameters as the generator function being wrapped.                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `params` _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 [#path-wrap-parameters]

```py
newrelic.agent.wrap_generator_trace(module, object_path, name=None, group=None, label=None, params=None)
```

Parameters include all [parameters](#main-parameters) for `generator_trace` and these parameters:

| Parameter              | Description                                                       |
| ---------------------- | ----------------------------------------------------------------- |
| `module` _object_      | Required. The module containing the generator to be instrumented. |
| `object_path` _string_ | The path to the location of the generator.                        |

## Examples

### `generator_trace` example [#trace_example]

An example of using the `generator_trace` decorator:

```py
import newrelic.agent

@newrelic.agent.generator_trace()
def gen():
    for i in range(5):
        yield i
```

An example of using the `generator_trace` decorator with async generator:

```py
import newrelic.agent

@newrelic.agent.generator_trace()
async def gen():
    for i in range(5):
        yield i
```

### Wrapper example [#trace-wrap-example]

An example of using the `GeneratorTraceWrapper`:

```py
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.

```py
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.

```py
import newrelic.agent

def gen(x):
    for i in range(x):
        yield i

gen = newrelic.agent.GeneratorTraceWrapper(gen, name = lambda x: f"gen_{x}")
```
