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

## Syntax

```py
newrelic.agent.data_source_generator(name=None, **properties)
```

Wraps a metric-data-generating data source.

## Description

The data source APIs provide a way to generate metrics using a [pull-style API](https://docs.newrelic.com/docs/agents/python-agent/supported-features/python-custom-metrics#push-versus-pull-interfaces) rather than the push-style API implemented by [`record_custom_metric`](https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/record_custom_metric). For more about why and how to use data sources for custom metrics, see [Custom metric data sources](https://docs.newrelic.com/docs/agents/python-agent/supported-features/python-custom-metrics#custom-metric-data-sources).

The `data_source_generator` decorator is used to wrap a simple metric-data-generating data source that directly returns an iterable/generator with the metrics for each sample. The function the decorator is applied to must take no arguments. This would be used where there is no need to retain state information between calls to generate any metrics, and where the one instance of the data source can be used against multiple applications.

## Parameters

| Parameter                 | Description                                                                                                                                                              |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `name` _string_           | Optional. The name of the data source. This is used only for logging purposes. If not provided, it defaults to the callable name derived from the decorated function.    |
| `properties` _dictionary_ | Optional. Any additional properties to pass to the data source factory. The possible fields for a dictionary are: - `count` - `total` - `min` - `max` - `sum_of_squares` |

## Return values

Returns a function.

## Examples

### Data source generator example [#example]

An example of using `data_source_generator` to wrap a function that returns metric values:

```py
import newrelic.agent
import psutil
import os

@newrelic.agent.data_source_generator(name='Memory_Usage')
def memory_metrics():
    pid = os.getpid()
    p = psutil.Process(pid)
    m = p.memory_info()
    yield ('Custom/Memory/Physical', float(m.rss) / (1024 * 1024))
    yield ('Custom/Memory/Virtual', float(m.vms) / (1024 * 1024))


@newrelic.agent.background_task()
def main():
    # Example code, business as usual
    print("Hello, world!")


if __name__ == "__main__":
    newrelic.agent.initialize(config_file="newrelic.ini")
    app = newrelic.agent.register_application()
    newrelic.agent.register_data_source(memory_metrics, app)
    
    main()
```
