Official Python SDK for Sentry error monitoring and performance tracking with extensive framework integrations
—
SDK initialization and configuration management including DSN setup, sampling rates, integrations, transport options, and environment-specific settings.
Initialize the Sentry SDK with configuration options including DSN, sampling rates, integrations, and various client settings.
def init(
dsn: Optional[str] = None,
integrations: Optional[List[Integration]] = None,
default_integrations: bool = True,
auto_enabling_integrations: bool = True,
disabled_integrations: Optional[List[Union[type, str]]] = None,
environment: Optional[str] = None,
release: Optional[str] = None,
traces_sample_rate: Optional[float] = None,
traces_sampler: Optional[Callable[[Dict[str, Any]], float]] = None,
profiles_sample_rate: Optional[float] = None,
profiles_sampler: Optional[Callable[[Dict[str, Any]], float]] = None,
max_breadcrumbs: int = 100,
attach_stacktrace: bool = False,
send_default_pii: bool = False,
in_app_include: Optional[List[str]] = None,
in_app_exclude: Optional[List[str]] = None,
before_send: Optional[Callable[[Event, Hint], Optional[Event]]] = None,
before_send_transaction: Optional[Callable[[Event, Hint], Optional[Event]]] = None,
transport: Optional[Transport] = None,
http_proxy: Optional[str] = None,
https_proxy: Optional[str] = None,
shutdown_timeout: int = 2,
debug: bool = False,
**kwargs
) -> None:
"""
Initialize the Sentry SDK.
Parameters:
- dsn: Data Source Name for your Sentry project
- integrations: List of integrations to enable
- default_integrations: Enable default integrations (logging, stdlib, etc.)
- auto_enabling_integrations: Enable auto-detected integrations (Django, Flask, etc.)
- disabled_integrations: List of integration types/names to disable
- environment: Environment name (e.g., 'production', 'staging')
- release: Release version identifier
- traces_sample_rate: Percentage of transactions to capture (0.0 to 1.0)
- traces_sampler: Function to determine transaction sampling
- profiles_sample_rate: Percentage of transactions to profile (0.0 to 1.0)
- profiles_sampler: Function to determine profiling sampling
- max_breadcrumbs: Maximum number of breadcrumbs to store
- attach_stacktrace: Attach stack traces to non-exception events
- send_default_pii: Send personally identifiable information
- in_app_include: List of modules to consider "in-app"
- in_app_exclude: List of modules to exclude from "in-app"
- before_send: Event processor function for events
- before_send_transaction: Event processor function for transactions
- transport: Custom transport implementation
- http_proxy: HTTP proxy URL
- https_proxy: HTTPS proxy URL
- shutdown_timeout: Timeout for SDK shutdown in seconds
- debug: Enable debug logging
"""Usage Example:
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
dsn="https://your-dsn@sentry.io/project-id",
environment="production",
release="my-app@1.0.0",
traces_sample_rate=0.1, # 10% of transactions
profiles_sample_rate=0.1, # 10% of transactions
integrations=[
DjangoIntegration(
transaction_style='url',
middleware_spans=True,
signals_spans=True,
),
RedisIntegration(),
],
max_breadcrumbs=50,
attach_stacktrace=True,
send_default_pii=False,
before_send=lambda event, hint: event if should_send_event(event) else None,
)Check whether the SDK has been properly initialized and is ready to capture events.
def is_initialized() -> bool:
"""
Check if Sentry SDK has been initialized.
Returns:
bool: True if SDK is initialized and client is active, False otherwise
"""Usage Example:
import sentry_sdk
if not sentry_sdk.is_initialized():
sentry_sdk.init(dsn="https://your-dsn@sentry.io/project-id")
# Now safe to use other SDK functions
sentry_sdk.capture_message("SDK is ready!")The DSN is the connection string that tells the SDK where to send events. It includes the protocol, public key, server address, and project ID.
Format: https://{PUBLIC_KEY}@{HOSTNAME}/{PROJECT_ID}
Control what percentage of events and transactions are sent to Sentry to manage volume and costs.
Install with Tessl CLI
npx tessl i tessl/pypi-sentry-sdk