@@ -82,9 +82,46 @@ python metrics_simple.py
8282
8383### 6. [ Advanced Metrics] ( metrics_example.py )
8484
85- More comprehensive metrics example (requires updates to work with current API).
85+ Comprehensive metrics and observability example:
86+ - Multiple metrics collectors setup
87+ - Query performance monitoring
88+ - Connection health tracking
89+ - Prometheus integration example
90+ - FastAPI integration patterns
8691
87- ### 7. [ Monitoring Configuration] ( monitoring/ )
92+ ** Run:**
93+ ``` bash
94+ python metrics_example.py
95+ ```
96+
97+ ### 7. [ Context Manager Safety] ( context_manager_safety_demo.py )
98+
99+ Demonstrates proper context manager usage:
100+ - Context manager isolation
101+ - Error safety in queries and streaming
102+ - Concurrent operations with shared resources
103+ - Resource cleanup guarantees
104+
105+ ** Run:**
106+ ``` bash
107+ python context_manager_safety_demo.py
108+ ```
109+
110+ ### 8. [ Thread Pool Configuration] ( thread_pool_configuration.py )
111+
112+ Shows how to configure thread pools for different workloads:
113+ - Web application configuration
114+ - Batch processing setup
115+ - Thread pool size comparison
116+ - Thread starvation demonstration
117+ - Thread pool monitoring
118+
119+ ** Run:**
120+ ``` bash
121+ python thread_pool_configuration.py
122+ ```
123+
124+ ### 9. [ Monitoring Configuration] ( monitoring/ )
88125
89126Production-ready monitoring configurations:
90127- ** alerts.yml** - Prometheus alerting rules for:
@@ -114,25 +151,48 @@ All examples require:
114151 # pip install async-cassandra
115152 ` ` `
116153
117- # # Common Patterns Demonstrated
154+ # # Best Practices Demonstrated
155+
156+ # ## MANDATORY: Always Use Context Managers
157+ All examples follow the required pattern:
158+ ` ` ` python
159+ # ALWAYS use context managers for resource management
160+ async with AsyncCluster([" localhost" ]) as cluster:
161+ async with cluster.connect () as session:
162+ # Your code here
163+ pass
164+ ` ` `
165+
166+ # ## MANDATORY: Always Use PreparedStatements
167+ For any query with parameters:
168+ ` ` ` python
169+ # Prepare statement once
170+ stmt = await session.prepare(
171+ " INSERT INTO users (id, name) VALUES (?, ?)"
172+ )
173+ # Execute many times
174+ await session.execute(stmt, [user_id, name])
175+ ` ` `
176+
177+ # ## Common Patterns Demonstrated
118178
119- # ## Connection Management
120- - Using context managers for automatic cleanup
179+ # ### Connection Management
180+ - Using context managers for automatic cleanup (REQUIRED)
121181- Proper cluster and session lifecycle
122182- Connection health monitoring
123183
124- # ## Error Handling
184+ # ### Error Handling
125185- Catching and handling Cassandra exceptions
126186- Retry strategies with idempotency
127187- Graceful degradation
128188
129- # ## Performance Optimization
130- - Prepared statements for repeated queries
189+ # ### Performance Optimization
190+ - Prepared statements for repeated queries (REQUIRED)
131191- Concurrent query execution
132- - Streaming for large datasets
192+ - Streaming for large datasets with context managers
133193- Appropriate fetch sizes
134194
135- # ## Monitoring & Observability
195+ # ### Monitoring & Observability
136196- Metrics collection
137197- Performance tracking
138198- Health checks
@@ -159,11 +219,14 @@ Examples use local Cassandra by default. Network latency may vary with remote cl
159219# # Contributing
160220
161221We welcome new examples! When contributing:
222+ - ** MUST use context managers** for all cluster/session/streaming operations
223+ - ** MUST use PreparedStatements** for all parameterized queries
162224- Include clear documentation in the code
163225- Handle errors appropriately
164226- Clean up resources (drop keyspaces/tables)
165227- Test with Python 3.12
166228- Update this README
229+ - Follow the patterns shown in existing examples
167230
168231# # Support
169232
0 commit comments