sentry sdk custom performance integration for python app

Question:

Sentry can track performance for celery tasks and API endpoints
https://docs.sentry.io/product/performance/

I have custom script that are lunching by crone and do set of similar tasks

I want to incorporated sentry_sdk into my script to get performance tracing of my tasks

Any advise how to do it with
https://getsentry.github.io/sentry-python/api.html#sentry_sdk.capture_event

Answers:

You don’t need use capture_event
I would suggest to use sentry_sdk.start_transaction instead. It also allows track your function performance.

Look at my example

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()

After starting this code you can see basic info of transaction a with childs b and c
enter image description here

Answered By: zoola969
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.