• EnglishEspañol日本語한국어Português
  • Log inStart now

data_source_generator (Python agent API)

Syntax

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

Return values

Returns a function.

Examples

Data source generator example

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

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()
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.