L
L
LakeForest2022-01-20 12:38:58
Python
LakeForest, 2022-01-20 12:38:58

How to create transaction in sentry in python for grpc method?


[sentry] DEBUG: [Tracing] Adding `sentry-trace` header xxx- to outgoing request to https://xxx.xyz/api/1/envelope/ .
[sentry] ERROR: Unexpected status code: 403

In addition to errors, nothing can be created in sentry.
if SENTRY_DSN:
    sentry_sdk.init(
        dsn=SENTRY_DSN,
        debug=True,
        # вероятность, что транзакция будет отравлена в сентри
        traces_sample_rate=1.0, sample_rate=0.25
        )

def transaction_sentry(logger):

    def debug_request(func):
        @wraps(func)
        def inner(*args, **kwargs):
            task_name = f"{func.__name__}"
            logger.warn("Создана транзакция: `{}`".format(task_name))
            try:
                span = sentry_sdk.Hub.current.scope.span
                if span is None:
                    # нет выполняемых интервалов, создайте новую транзакцию
                    with sentry_sdk.start_transaction(name=task_name, sampled=True):
                        response = func(*args, **kwargs)
                        logger.warn(response)
                else:
                    # дочерняя задача транзакции
                    with span.start_child(op=task_name):
                        response = func(*args, **kwargs)
                return response
            except Exception as e:
                logger.warn(e)
                logger.exception(
                    "Ошибка при выполнении транзакции: `{}`".format(task_name))
                sentry_sdk.capture_exception(error=e)
                raise e
        return inner
    return debug_request

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question