我的 python 服务有 python newrelic 代理。我使用 newrelic.ini 文件配置了所有内容。现在我想查看在我的服务上运行的所有函数的跟踪。
我看到了选项
transaction_tracer.function_trace
,但从文件中的解释来看,我似乎必须指定每个函数的个性,这太难维护了。有没有办法设置它或其他配置以便跟踪所有功能?
我最终向所有类添加了一个元类,该元类在所有类函数上使用装饰器trace_function。
from types import FunctionType
import newrelic.agent
class TraceClassFunctionsMetaclass(type):
def __new__(cls, class_name: str, bases: tuple, class_attributes: dict):
for attr_name, attr_value in class_attributes.items():
if isinstance(attr_value, FunctionType) and attr_name != '__init__':
class_attributes[attr_name] = newrelic.agent.FunctionTraceWrapper(attr_value)
return super(TraceClassFunctionsMetaclass, cls).__new__(cls, class_name, bases, class_attributes)
用途:
class Codec(metaclass=TraceClassFunctionsMetaclass):
pass