data_source_generator

For general information on how the Python agent API calls relate to each other, see the API guide.

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

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

For explanations of these fields and some general usage tips, see the documentation for the New Relic Plugins custom metrics API.

Return value(s)

Returns a function.

Example(s)

Data source generator example

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

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

For more help

Recommendations for learning more: