Skip to content

Commit 3e29948

Browse files
authored
Cursor generated rules (#4493)
It also added performance and aws related files but I want to keep it simple for now. This adds: * quick reference * testing guide * project overview * core architecture * integration guide
1 parent ae06ef1 commit 3e29948

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed

.cursor/rules/core-architecture.mdc

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: false
5+
---
6+
# Core Architecture
7+
8+
## Scope and Client Pattern
9+
10+
The Sentry SDK uses a **Scope and Client** pattern for managing state and context:
11+
12+
### Scope
13+
- [sentry_sdk/scope.py](mdc:sentry_sdk/scope.py) - Holds contextual data
14+
- Holds a reference to the Client
15+
- Contains tags, extra data, user info, breadcrumbs
16+
- Thread-local storage for isolation
17+
18+
### Client
19+
- [sentry_sdk/client.py](mdc:sentry_sdk/client.py) - Handles event processing
20+
- Manages transport and event serialization
21+
- Applies sampling and filtering
22+
23+
## Key Components
24+
25+
### API Layer
26+
- [sentry_sdk/api.py](mdc:sentry_sdk/api.py) - Public API functions
27+
- `init()` - Initialize the SDK
28+
- `capture_exception()` - Capture exceptions
29+
- `capture_message()` - Capture custom messages
30+
- `set_tag()`, `set_user()`, `set_context()` - Add context
31+
- `start_transaction()` - Performance monitoring
32+
33+
### Transport
34+
- [sentry_sdk/transport.py](mdc:sentry_sdk/transport.py) - Event delivery
35+
- `HttpTransport` - HTTP transport to Sentry servers
36+
- Handles retries, rate limiting, and queuing
37+
38+
### Integrations System
39+
- [sentry_sdk/integrations/__init__.py](mdc:sentry_sdk/integrations/__init__.py) - Integration registry
40+
- Base `Integration` class for all integrations
41+
- Automatic setup and teardown
42+
- Integration-specific configuration
43+
44+
## Data Flow
45+
46+
### Event Capture Flow
47+
1. **Exception occurs** or **manual capture** called
48+
2. **get_current_scope** gets the active current scope
49+
2. **get_isolation_scope** gets the active isolation scope
50+
3. **Scope data** (tags, user, context) is attached
51+
4. **Client.process_event()** processes the event
52+
5. **Sampling** and **filtering** applied
53+
6. **Transport** sends to Sentry servers
54+
55+
### Performance Monitoring Flow
56+
1. **Transaction started** with `start_transaction()`
57+
2. **Spans** created for operations within transaction with `start_span()`
58+
3. **Timing data** collected automatically
59+
4. **Transaction finished** and sent to Sentry
60+
61+
## Context Management
62+
63+
### Scope Stack
64+
- **Global scope**: Default scope for the process
65+
- **Isolation scope**: Isolated scope for specific operations, manages concurrency isolation
66+
- **Current scope**: Active scope for current execution context
67+
68+
### Scope Operations
69+
- `configure_scope()` - Modify current scope
70+
- `new_scope()` - Create isolated scope
71+
72+
## Integration Architecture
73+
74+
### Integration Lifecycle
75+
1. **Registration**: Integration registered during `init()`
76+
2. **Setup**: `setup_once()` called to install hooks
77+
3. **Runtime**: Integration monitors and captures events
78+
4. **Teardown**: Integration cleaned up on shutdown
79+
80+
### Common Integration Patterns
81+
- **Monkey patching**: Replace functions/methods with instrumented versions
82+
- **Signal handlers**: Hook into framework signals/events
83+
- **Middleware**: Add middleware to web frameworks
84+
- **Exception handlers**: Catch and process exceptions
85+
86+
### Integration Configuration
87+
```python
88+
# Example integration setup
89+
sentry_sdk.init(
90+
dsn="your-dsn",
91+
integrations=[
92+
DjangoIntegration(),
93+
CeleryIntegration(),
94+
RedisIntegration(),
95+
],
96+
traces_sample_rate=1.0,
97+
)
98+
```
99+
100+
## Error Handling
101+
102+
### Exception Processing
103+
- **Automatic capture**: Unhandled exceptions captured automatically
104+
- **Manual capture**: Use `capture_exception()` for handled exceptions
105+
- **Context preservation**: Stack traces, local variables, and context preserved
106+
107+
### Breadcrumbs
108+
- **Automatic breadcrumbs**: Framework operations logged automatically
109+
- **Manual breadcrumbs**: Use `add_breadcrumb()` for custom events
110+
- **Breadcrumb categories**: HTTP, database, navigation, etc.
111+
112+
## Performance Monitoring
113+
114+
### Transaction Tracking
115+
- **Automatic transactions**: Web requests, background tasks
116+
- **Custom transactions**: Use `start_transaction()` for custom operations
117+
- **Span tracking**: Database queries, HTTP requests, custom operations
118+
- **Performance data**: Timing, resource usage, custom measurements
119+
120+
### Sampling
121+
- **Transaction sampling**: Control percentage of transactions captured
122+
- **Dynamic sampling**: Adjust sampling based on context

.cursor/rules/integrations-guide.mdc

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: false
5+
---
6+
# Integrations Guide
7+
8+
## Integration Categories
9+
10+
The Sentry Python SDK includes integrations for popular frameworks, libraries, and services:
11+
12+
### Web Frameworks
13+
- [sentry_sdk/integrations/django/](mdc:sentry_sdk/integrations/django) - Django web framework
14+
- [sentry_sdk/integrations/flask/](mdc:sentry_sdk/integrations/flask) - Flask microframework
15+
- [sentry_sdk/integrations/fastapi/](mdc:sentry_sdk/integrations/fastapi) - FastAPI framework
16+
- [sentry_sdk/integrations/starlette/](mdc:sentry_sdk/integrations/starlette) - Starlette ASGI framework
17+
- [sentry_sdk/integrations/sanic/](mdc:sentry_sdk/integrations/sanic) - Sanic async framework
18+
- [sentry_sdk/integrations/tornado/](mdc:sentry_sdk/integrations/tornado) - Tornado web framework
19+
- [sentry_sdk/integrations/pyramid/](mdc:sentry_sdk/integrations/pyramid) - Pyramid framework
20+
- [sentry_sdk/integrations/bottle/](mdc:sentry_sdk/integrations/bottle) - Bottle microframework
21+
- [sentry_sdk/integrations/chalice/](mdc:sentry_sdk/integrations/chalice) - AWS Chalice
22+
- [sentry_sdk/integrations/quart/](mdc:sentry_sdk/integrations/quart) - Quart async framework
23+
- [sentry_sdk/integrations/falcon/](mdc:sentry_sdk/integrations/falcon) - Falcon framework
24+
- [sentry_sdk/integrations/litestar/](mdc:sentry_sdk/integrations/litestar) - Litestar framework
25+
- [sentry_sdk/integrations/starlite/](mdc:sentry_sdk/integrations/starlite) - Starlite framework
26+
27+
### Task Queues and Background Jobs
28+
- [sentry_sdk/integrations/celery/](mdc:sentry_sdk/integrations/celery) - Celery task queue
29+
- [sentry_sdk/integrations/rq/](mdc:sentry_sdk/integrations/rq) - Redis Queue
30+
- [sentry_sdk/integrations/huey/](mdc:sentry_sdk/integrations/huey) - Huey task queue
31+
- [sentry_sdk/integrations/arq/](mdc:sentry_sdk/integrations/arq) - Arq async task queue
32+
- [sentry_sdk/integrations/dramatiq/](mdc:sentry_sdk/integrations/dramatiq) - Dramatiq task queue
33+
34+
### Databases and Data Stores
35+
- [sentry_sdk/integrations/sqlalchemy/](mdc:sentry_sdk/integrations/sqlalchemy) - SQLAlchemy ORM
36+
- [sentry_sdk/integrations/asyncpg/](mdc:sentry_sdk/integrations/asyncpg) - AsyncPG PostgreSQL
37+
- [sentry_sdk/integrations/pymongo/](mdc:sentry_sdk/integrations/pymongo) - PyMongo MongoDB
38+
- [sentry_sdk/integrations/redis/](mdc:sentry_sdk/integrations/redis) - Redis client
39+
- [sentry_sdk/integrations/clickhouse_driver/](mdc:sentry_sdk/integrations/clickhouse_driver) - ClickHouse driver
40+
41+
### Cloud and Serverless
42+
- [sentry_sdk/integrations/aws_lambda/](mdc:sentry_sdk/integrations/aws_lambda) - AWS Lambda
43+
- [sentry_sdk/integrations/gcp/](mdc:sentry_sdk/integrations/gcp) - Google Cloud Platform
44+
- [sentry_sdk/integrations/serverless/](mdc:sentry_sdk/integrations/serverless) - Serverless framework
45+
46+
### HTTP and Networking
47+
- [sentry_sdk/integrations/requests/](mdc:sentry_sdk/integrations/requests) - Requests HTTP library
48+
- [sentry_sdk/integrations/httpx/](mdc:sentry_sdk/integrations/httpx) - HTTPX async HTTP client
49+
- [sentry_sdk/integrations/aiohttp/](mdc:sentry_sdk/integrations/aiohttp) - aiohttp async HTTP
50+
- [sentry_sdk/integrations/grpc/](mdc:sentry_sdk/integrations/grpc) - gRPC framework
51+
52+
### AI and Machine Learning
53+
- [sentry_sdk/integrations/openai/](mdc:sentry_sdk/integrations/openai) - OpenAI API
54+
- [sentry_sdk/integrations/anthropic/](mdc:sentry_sdk/integrations/anthropic) - Anthropic Claude
55+
- [sentry_sdk/integrations/cohere/](mdc:sentry_sdk/integrations/cohere) - Cohere AI
56+
- [sentry_sdk/integrations/huggingface_hub/](mdc:sentry_sdk/integrations/huggingface_hub) - Hugging Face Hub
57+
- [sentry_sdk/integrations/langchain/](mdc:sentry_sdk/integrations/langchain) - LangChain framework
58+
59+
### GraphQL
60+
- [sentry_sdk/integrations/graphene/](mdc:sentry_sdk/integrations/graphene) - Graphene GraphQL
61+
- [sentry_sdk/integrations/ariadne/](mdc:sentry_sdk/integrations/ariadne) - Ariadne GraphQL
62+
- [sentry_sdk/integrations/strawberry/](mdc:sentry_sdk/integrations/strawberry) - Strawberry GraphQL
63+
- [sentry_sdk/integrations/gql/](mdc:sentry_sdk/integrations/gql) - GQL GraphQL client
64+
65+
### Feature Flags and Configuration
66+
- [sentry_sdk/integrations/launchdarkly/](mdc:sentry_sdk/integrations/launchdarkly) - LaunchDarkly
67+
- [sentry_sdk/integrations/unleash/](mdc:sentry_sdk/integrations/unleash) - Unleash
68+
- [sentry_sdk/integrations/statsig/](mdc:sentry_sdk/integrations/statsig) - Statsig
69+
- [sentry_sdk/integrations/openfeature/](mdc:sentry_sdk/integrations/openfeature) - OpenFeature
70+
71+
### Other Integrations
72+
- [sentry_sdk/integrations/logging/](mdc:sentry_sdk/integrations/logging) - Python logging
73+
- [sentry_sdk/integrations/loguru/](mdc:sentry_sdk/integrations/loguru) - Loguru logging
74+
- [sentry_sdk/integrations/opentelemetry/](mdc:sentry_sdk/integrations/opentelemetry) - OpenTelemetry
75+
- [sentry_sdk/integrations/ray/](mdc:sentry_sdk/integrations/ray) - Ray distributed computing
76+
- [sentry_sdk/integrations/spark/](mdc:sentry_sdk/integrations/spark) - Apache Spark
77+
- [sentry_sdk/integrations/beam/](mdc:sentry_sdk/integrations/beam) - Apache Beam
78+
79+
## Integration Usage
80+
81+
### Basic Integration Setup
82+
```python
83+
import sentry_sdk
84+
from sentry_sdk.integrations.django import DjangoIntegration
85+
from sentry_sdk.integrations.celery import CeleryIntegration
86+
87+
sentry_sdk.init(
88+
dsn="your-dsn",
89+
integrations=[
90+
DjangoIntegration(),
91+
CeleryIntegration(),
92+
],
93+
traces_sample_rate=1.0,
94+
)
95+
```
96+
97+
### Integration Configuration
98+
Most integrations accept configuration parameters:
99+
```python
100+
from sentry_sdk.integrations.django import DjangoIntegration
101+
from sentry_sdk.integrations.redis import RedisIntegration
102+
103+
sentry_sdk.init(
104+
dsn="your-dsn",
105+
integrations=[
106+
DjangoIntegration(
107+
transaction_style="url", # Customize transaction naming
108+
),
109+
RedisIntegration(
110+
cache_prefixes=["myapp:"], # Filter cache operations
111+
),
112+
],
113+
)
114+
```
115+
116+
### Integration Testing
117+
Each integration has corresponding tests in [tests/integrations/](mdc:tests/integrations):
118+
- [tests/integrations/django/](mdc:tests/integrations/django) - Django integration tests
119+
- [tests/integrations/flask/](mdc:tests/integrations/flask) - Flask integration tests
120+
- [tests/integrations/celery/](mdc:tests/integrations/celery) - Celery integration tests
121+
122+
## Integration Development
123+
124+
### Creating New Integrations
125+
1. **Create integration file** in [sentry_sdk/integrations/](mdc:sentry_sdk/integrations)
126+
2. **Inherit from Integration base class**
127+
3. **Implement setup_once() method**
128+
4. **Add to integration registry**
129+
130+
### Integration Base Class
131+
```python
132+
from sentry_sdk.integrations import Integration
133+
134+
class MyIntegration(Integration):
135+
identifier = "my_integration"
136+
137+
def __init__(self, param=None):
138+
self.param = param
139+
140+
@staticmethod
141+
def setup_once():
142+
# Install hooks, monkey patches, etc.
143+
pass
144+
```
145+
146+
### Common Integration Patterns
147+
- **Monkey patching**: Replace functions with instrumented versions
148+
- **Middleware**: Add middleware to web frameworks
149+
- **Signal handlers**: Hook into framework signals
150+
- **Exception handlers**: Catch and process exceptions
151+
- **Context managers**: Add context to operations
152+
153+
### Integration Best Practices
154+
- **Zero configuration**: Work without user setup
155+
- **Check integration status**: Use `sentry_sdk.get_client().get_integration()`
156+
- **No side effects**: Don't alter library behavior
157+
- **Graceful degradation**: Handle missing dependencies
158+
- **Comprehensive testing**: Test all integration features

.cursor/rules/project-overview.mdc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: false
5+
---
6+
# Sentry Python SDK - Project Overview
7+
8+
## What is this project?
9+
10+
The Sentry Python SDK is the official Python SDK for [Sentry](mdc:https://sentry.io), an error monitoring and performance monitoring platform. It helps developers capture errors, exceptions, traces and profiles from Python applications.
11+
12+
## Key Files and Directories
13+
14+
### Core SDK
15+
- [sentry_sdk/__init__.py](mdc:sentry_sdk/__init__.py) - Main entry point, exports all public APIs
16+
- [sentry_sdk/api.py](mdc:sentry_sdk/api.py) - Public API functions (init, capture_exception, etc.)
17+
- [sentry_sdk/client.py](mdc:sentry_sdk/client.py) - Core client implementation
18+
- [sentry_sdk/scope.py](mdc:sentry_sdk/scope.py) - Scope holds contextual metadata such as tags that are applied automatically to events and envelopes
19+
- [sentry_sdk/transport.py](mdc:sentry_sdk/transport.py) - HTTP Transport that sends the envelopes to Sentry's servers
20+
- [sentry_sdk/worker.py](mdc:sentry_sdk/worker.py) - Background threaded worker with a queue to manage transport requests
21+
- [sentry_sdk/serializer.py](mdc:sentry_sdk/serializer.py) - Serializes the payload along with truncation logic
22+
23+
### Integrations
24+
- [sentry_sdk/integrations/](mdc:sentry_sdk/integrations) - Framework and library integrations
25+
- [sentry_sdk/integrations/__init__.py](mdc:sentry_sdk/integrations/__init__.py) - Integration registry
26+
- [sentry_sdk/integrations/django/](mdc:sentry_sdk/integrations/django) - Django framework integration
27+
- [sentry_sdk/integrations/flask/](mdc:sentry_sdk/integrations/flask) - Flask framework integration
28+
- [sentry_sdk/integrations/fastapi/](mdc:sentry_sdk/integrations/fastapi) - FastAPI integration
29+
- [sentry_sdk/integrations/celery/](mdc:sentry_sdk/integrations/celery) - Celery task queue integration
30+
- [sentry_sdk/integrations/aws_lambda/](mdc:sentry_sdk/integrations/aws_lambda) - AWS Lambda integration
31+
32+
### Configuration and Setup
33+
- [setup.py](mdc:setup.py) - Package configuration and dependencies
34+
- [pyproject.toml](mdc:pyproject.toml) - Modern Python project configuration
35+
- [tox.ini](mdc:tox.ini) - Test matrix configuration for multiple Python versions and integrations
36+
- [requirements-*.txt](mdc:requirements-testing.txt) - Various dependency requirements
37+
38+
### Documentation and Guides
39+
- [README.md](mdc:README.md) - Project overview and quick start
40+
- [CONTRIBUTING.md](mdc:CONTRIBUTING.md) - Development and contribution guidelines
41+
- [MIGRATION_GUIDE.md](mdc:MIGRATION_GUIDE.md) - Migration from older versions
42+
- [CHANGELOG.md](mdc:CHANGELOG.md) - Version history and changes
43+
44+
### Testing
45+
- [tests/](mdc:tests) - Comprehensive test suite
46+
- [tests/integrations/](mdc:tests/integrations) - Integration-specific tests
47+
- [tests/conftest.py](mdc:tests/conftest.py) - Pytest configuration and fixtures

.cursor/rules/quick-reference.mdc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: false
5+
---
6+
# Quick Reference
7+
8+
## Common Commands
9+
10+
### Development Setup
11+
```bash
12+
make .venv
13+
source .venv/bin/activate # Windows: .venv\Scripts\activate
14+
```
15+
16+
### Testing
17+
18+
Our test matrix is implemented in [tox](mdc:https://tox.wiki).
19+
The following runs the whole test suite and takes a long time.
20+
21+
```bash
22+
source .venv/bin/activate
23+
tox
24+
```
25+
26+
Prefer testing a single environment instead while developing.
27+
28+
```bash
29+
tox -e py3.12-common
30+
```
31+
32+
For running a single test, use the pattern:
33+
34+
```bash
35+
tox -e py3.12-common -- project/tests/test_file.py::TestClassName::test_method
36+
```
37+
38+
For testing specific integrations, refer to the test matrix in [sentry_sdk/tox.ini](mdc:sentry_sdk/tox.ini) for finding an entry.
39+
For example, to test django, use:
40+
41+
```bash
42+
tox -e py3.12-django-v5.2.3
43+
```
44+
45+
### Code Quality
46+
47+
Our `linters` tox environment runs `black` for formatting, `flake8` for linting and `mypy` for type checking.
48+
49+
```bash
50+
tox -e linters
51+
```

0 commit comments

Comments
 (0)