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

## Syntax

```py
data_source_factory(name=None, **properties)
```

Wraps a data source defined as a factory.

## Description

The data source APIs provide a way to generate [metric timeslice data](https://docs.newrelic.com/docs/data-analysis/metrics/analyze-your-metrics/data-collection-metric-timeslice-event-data#timeslice-data) 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`. 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_factory` decorator wraps a data source defined as a factory. The decorator can be applied to a class or a function. The class constructor or function must accept arguments of `settings` (configuration settings for the data source) and `environ` (information about context in which the data source is being used).

The resulting object must be a callable which directly returns an iterable/generator with the metrics for each sample.

## 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_factory example [#example]

An example:

```py
import os
import time
import multiprocessing

@newrelic.agent.data_source_factory(name='CPU Usage')
class CPUMetricsDataSource(object):

    def __init__(self, settings, environ):
        self.last_timestamp = None
        self.times = None
 
    def start(self):
        self.last_timestamp = time.time()
        self.times = os.times()
 
    def stop(self):
        self.last_timestamp = None
        self.times = None

    def __call__(self):
        if self.times is None:
            return

        now = time.time()
        new_times = os.times()
        elapsed_time = now - self.last_timestamp
        user_time = new_times[0] - self.times[0]
        utilization = user_time / (elapsed_time*multiprocessing.cpu_count())
        self.last_timestamp = now
        self.times = new_times

        yield ('Custom/CPU/User Time', user_time)
        yield ('Custom/CPU/User/Utilization', utilization)

newrelic.agent.register_data_source(CPUMetricsDataSource)
```
