Python 的 Newrelic 来跟踪所有函数

问题描述 投票:0回答:1

我的 python 服务有 python newrelic 代理。我使用 newrelic.ini 文件配置了所有内容。现在我想查看在我的服务上运行的所有函数的跟踪。

我看到了选项

transaction_tracer.function_trace
,但从文件中的解释来看,我似乎必须指定每个函数的个性,这太难维护了。有没有办法设置它或其他配置以便跟踪所有功能?

它在我的 python 服务中的外观如何: 它在 java 服务中的外观以及我对 python 服务的目标的一部分:

newrelic
1个回答
0
投票

我最终向所有类添加了一个元类,该元类在所有类函数上使用装饰器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
© www.soinside.com 2019 - 2024. All rights reserved.