Sentry

From Torben's Wiki

Python

import sentry_sdk

sentry_sdk.init(
    dsn="https://12345@my-server.com/12",
    environment="dev",
    # debug=True,
    enable_tracing=False,
)
# for self-signed certs
# use browser to export the WHOLE certificate chain to
# ca_certs="myCertBundle.crt",

Exceptions

Var 1: all unhandled Exceptions are sent to Sentry

# Example
division_by_zero = 1 / 0

Var 2: custom event

event_data = {
    "message": "My Custom Event Title",
    "transaction": "My Transaction",
    # "user_id": 123,
    # "action": "custom_event",
    # "data": {"key1": "value1", "key2": "value2"},
}
with sentry_sdk.push_scope() as scope:
    # scope.user = {"id": "user-123"}
    event = sentry_sdk.capture_event(event_data)

Var 3: Forward Handled Exception

try:
    division_by_zero = 1 / 0
except Exception as e:
    sentry_sdk.capture_exception(e)

Performance Date

# enable tracing
sentry_sdk.init(
...
   enable_tracing=True,
)
...
with sentry_sdk.start_transaction(op="task", name="DB"):
    # optional spans inside the transaction
    # with sentry_sdk.start_span(description="connect"):
    # (connection, cursor) = connect()
    # with sentry_sdk.start_span(description="execute sql"):
    # results = execute_sql(cursor=cursor, sql=sql)
    # with sentry_sdk.start_span(description="disconnect"):
    # cursor.close()
    time.sleep(3)