【问题标题】:sentry sdk custom performance integration for python apppython 应用程序的 sentry sdk 自定义性能集成
【发布时间】:2022-11-18 04:23:52
【问题描述】:

Sentry 可以跟踪 celery 任务和 API 端点的性能 https://docs.sentry.io/product/performance/

我有自定义脚本,由 crone 午餐并做一组类似的任务

我想将 sentry_sdk 合并到我的脚本中以获取我的性能跟踪任务

任何建议如何做 https://getsentry.github.io/sentry-python/api.html#sentry_sdk.capture_event

【问题讨论】:

    标签: python performance sentry


    【解决方案1】:

    你不需要使用capture_event
    我建议改用sentry_sdk.start_transaction。它还允许跟踪您的功能性能。

    看我的例子

    from time import sleep
    from sentry_sdk import Hub, init, start_transaction
    
    init(
        dsn="dsn",
        traces_sample_rate=1.0,
    )
    
    
    def sentry_trace(func):
        def wrapper(*args, **kwargs):
            transaction = Hub.current.scope.transaction
            if transaction:
                with transaction.start_child(op=func.__name__):
                    return func(*args, **kwargs)
            else:
                with start_transaction(op=func.__name__, name=func.__name__):
                    return func(*args, **kwargs)
    
        return wrapper
    
    
    @sentry_trace
    def b():
        for i in range(1000):
            print(i)
    
    
    @sentry_trace
    def c():
        sleep(2)
        print(1)
    
    
    @sentry_trace
    def a():
        sleep(1)
        b()
        c()
    
    
    if __name__ == '__main__':
        a()
    

    启动此代码后,您可以看到交易a与孩子bc的基本信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2016-10-28
      • 1970-01-01
      • 2017-03-18
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多