\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/cloudlinux/venv/lib64/python3.11/site-packages/clsentry/

Viewing File: __init__.py

# coding=utf-8

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
import logging
import sentry_sdk

from clcommon.utils import get_rhn_systemid_value, get_username
from clcommon.lib.network import get_ip_addr, get_hostname

from clsentry.client import (
    UserlandClient,
    SafeRequestsHTTPTransportSentrySdk,
    ThreadedHttpTransport,
    get_user_tags,
    before_send
)
from clsentry.utils import get_pkg_version

from raven.handlers.logging import SentryHandler
from raven.conf import setup_logging
from sentry_sdk.integrations.logging import LoggingIntegration

CLLIB_DSN = 'https://9713d1296f804031b058b8f2d789d7ac:8ddacae32d8246cf8b25cf826bf3fc0a@cl.sentry.cloudlinux.com/12'
__all__ = ('init_sentry_client', 'init_cllib_sentry_client', 'init_sentry_sdk_client')


def init_sentry_client(project, release, dsn, handle=True, custom_length=None):
    """
    @deprecated use init_sentry_sdk_client
    Create generic sentry client and install logging hooks
    """
    sentry = UserlandClient(dsn, release=release)
    # set user context settings, like id or email
    sentry.user_context({
        'id': get_rhn_systemid_value('system_id') or get_ip_addr(get_hostname()) or get_hostname() or get_username()
    })
    # and also set project name
    sentry.tags['Project'] = project

    if custom_length:
        sentry.string_max_length = custom_length

    if handle:
        # setup handler, so we can track logging.error's
        handler = SentryHandler(sentry, level=logging.ERROR)
        setup_logging(handler)
    return sentry


def init_cllib_sentry_client():
    """
    Create sentry client for cllib package
    and install logging hooks
    """
    return init_sentry_client(
        'python-cllib', release=get_pkg_version('alt-python27-cllib'), dsn=CLLIB_DSN)


def init_sentry_sdk_client(project, release, dsn, handle=True, custom_length=1000,
                           transport=SafeRequestsHTTPTransportSentrySdk, environment=None):
    """
    Initialize the Sentry SDK client and configure logging integration.

    :param project: Project name.
    :param release: Application release version.
    :param dsn: Sentry DSN.
    :param handle: Enable logging integration (default: True).
    :param custom_length: Max length for captured values (default: 1000).
    :param transport: Transport method, can be 'threading' or a transport class.
    :param environment: Environment name (default: None). If None or empty string,
                        defaults to production environment.
    :return: Configured Sentry SDK instance.
    """

    sentry_sdk.init(
        dsn=dsn,
        release=release,
        environment=environment or None,
        transport=ThreadedHttpTransport if transport == 'threading' else transport,
        ignore_errors=[KeyboardInterrupt],
        max_value_length=custom_length,
        before_send=before_send,
        attach_stacktrace=True,
        _experiments={"auto_enabling_integrations": False},
        integrations=[LoggingIntegration(level=logging.ERROR, event_level=logging.ERROR)] if handle else [],
    )

    with sentry_sdk.configure_scope() as scope:
        scope.set_user({
            'id': get_rhn_systemid_value('system_id') or get_ip_addr(get_hostname()) or get_hostname() or get_username()
        })
        for key, value in get_user_tags().items():
            scope.set_tag(key, value)
        scope.set_tag("Project", project)

    return sentry_sdk


def sentry_sdk_send_message(message: str, level: str = "info", tags: dict = None, extra: dict = None,
                            attachments: list[dict] = None, user: dict = None, fingerprint: list = None,
                            contexts: dict = None, transaction: str = None):
    """
    Send a message to Sentry with additional context.

    :param message: The text of the message.
    :param level: Logging level (debug, info, warning, error, fatal).
    :param tags: Additional tags (dict). For example: {"foo": "bar"}
    :param extra: Additional data (dict). For example: {"foo": "bar"}
    :param attachments: Additional files (list). With "path" or "bytes", "filename", and optional "content_type.
    :param user: User information (dict). With "id", and "username".
    :param fingerprint: Fingerprint information (list). For example: ["my-custom-events-group"]
    :param contexts: Additional context data (dict). For example: {"character": {"age": 19}}
    :param transaction: Transaction name (string). For example: UserListView

    Docs: https://docs.sentry.io/platforms/python/enriching-events/
    """

    with sentry_sdk.push_scope() as scope:
        if tags:
            for key, value in tags.items():
                scope.set_tag(key, value)

        if extra:
            for key, value in extra.items():
                scope.set_extra(key, value)

        if attachments:
            for attachment in attachments:
                scope.add_attachment(**attachment)

        if user:
            scope.set_user(user)

        if fingerprint:
            scope.fingerprint = fingerprint

        if contexts:
            for key, value in contexts.items():
                scope.set_context(key, value)

        if transaction:
            scope.set_transaction_name(transaction)

        sentry_sdk.capture_message(message, level=level)