You can extend the Python agent's monitoring to trace time spent in additional functions or methods of classes by modifying your configuration file. This form of custom instrumentation is simpler to implement than API calls, and does not require you to modify your code. However, for more complex needs, you may need to implement Python instrumentation by API.
To extend instrumentation to designated functions and class methods, add them to the
transaction_tracer.function_trace setting within the
newrelic section of the agent configuration file. The identifier for a function should have the form
module:function and that of a class method
To list more than one item, use either of these methods:
- One line: Separate each item with a space.
- Multiple lines: Follow the ini file convention to indent the subsequent lines.
When data is reported for the instrumented function, the metric will have a name in the form
Function/module:class.function. In the performance breakdown for a transaction, the category will show as
Function and the segment will be
module:class.function. For slow transaction traces, only the segment name appears. Note that where a function is actually returning a generator, only the time spent in returning the generator will be recorded and not the consumption of the values from the generator.
When wrapping functions by listing them in the agent configuration file, you cannot designate class methods this way when the method is a member of a class which is implemented in C code by a C extension module. This is because it is not possible to modify the method table of a type implemented using the Python C API.
When using the
transaction_tracer.function_trace setting in the agent configuration file, the name of the function will be used in the metric name, with it being classified as a
Function. If it is necessary to override what function name may be used in the metric name or classify it differently, then an alternate means of defining the function trace in the configuration file can be used. The equivalent for:
transaction_tracer.function_trace = dumbdbm:opendumbdbm:_Database._commit
would be to create two new configuration sections in the agent configuration file, one for each function to be traced:
[function-trace:dumbdbm-1]enabled = truefunction = dumbdbm:openname = dumbdbm:opengroup = Function[function-trace:dumbdbm-2]enabled = truefunction = dumbdbm:_Database._commitname = dumbdbm:_Database._commitgroup = Function
The ini file section name should start with
function-trace:. The name component that follows that prefix can be anything but should be unique across all function trace sections in the configuration file. The
name settings can be overridden as necessary to arrive at the desired metric name. The
enabled setting defaults to
false and needs to be set to
true to enable the function trace.
For simple custom instrumentation purposes, the config file method works well. For more precise and customized instrumentation and agent behavior adjustments, you will want to use the Python agent API.
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's data security and licenses documentation.