-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
351 lines (277 loc) · 9.2 KB
/
__init__.py
File metadata and controls
351 lines (277 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
Checkend - Python SDK for error monitoring.
A lightweight, zero-dependency error monitoring SDK for Python applications.
"""
import atexit
import contextvars
from typing import Any, Optional
from checkend.client import Client
from checkend.configuration import Configuration
from checkend.notice import Notice
from checkend.testing import Testing
from checkend.version import VERSION
from checkend.worker import Worker
__version__ = VERSION
__all__ = [
"configure",
"notify",
"notify_sync",
"set_context",
"get_context",
"set_user",
"get_user",
"set_request",
"get_request",
"clear",
"flush",
"stop",
"reset",
"Configuration",
"Testing",
]
# Context variables for request-scoped data (use None as default, not mutable {})
_context_var: contextvars.ContextVar[Optional[dict[str, Any]]] = contextvars.ContextVar(
"checkend_context", default=None
)
_user_var: contextvars.ContextVar[Optional[dict[str, Any]]] = contextvars.ContextVar(
"checkend_user", default=None
)
_request_var: contextvars.ContextVar[Optional[dict[str, Any]]] = contextvars.ContextVar(
"checkend_request", default=None
)
# Global state
_configuration: Optional[Configuration] = None
_worker: Optional[Worker] = None
_initialized: bool = False
def configure(**options) -> Configuration:
"""
Configure the Checkend SDK.
Args:
api_key: Your Checkend ingestion API key (required)
endpoint: API endpoint URL (default: https://app.checkend.com)
environment: Environment name (auto-detected if not provided)
enabled: Whether error reporting is enabled (default: True in production)
async_send: Whether to send errors asynchronously (default: True)
max_queue_size: Maximum queue size for async sending (default: 1000)
timeout: HTTP request timeout in seconds (default: 15)
filter_keys: List of keys to filter from payloads
ignored_exceptions: List of exception classes or patterns to ignore
before_notify: List of callbacks to run before sending (return False to skip)
logger: Custom logger instance
debug: Enable debug logging (default: False)
Returns:
Configuration instance
"""
global _configuration, _worker, _initialized
_configuration = Configuration(**options)
if _configuration.async_send and _configuration.enabled:
_worker = Worker(_configuration)
_worker.start()
_initialized = True
# Register shutdown handler
atexit.register(_shutdown)
return _configuration
def get_configuration() -> Optional[Configuration]:
"""Get the current configuration."""
return _configuration
def notify(
exception: BaseException,
context: Optional[dict[str, Any]] = None,
user: Optional[dict[str, Any]] = None,
request: Optional[dict[str, Any]] = None,
fingerprint: Optional[str] = None,
tags: Optional[list[str]] = None,
) -> Optional[int]:
"""
Report an exception to Checkend asynchronously.
Args:
exception: The exception to report
context: Additional context data
user: User information
request: Request information
fingerprint: Custom fingerprint for grouping
tags: List of tags for the error
Returns:
Notice ID if sent synchronously, None if queued or skipped
"""
if not _initialized or not _configuration or not _configuration.enabled:
return None
# Check if exception should be ignored
if _should_ignore(exception):
return None
# Build notice
notice = _build_notice(
exception,
context=context,
user=user,
request=request,
fingerprint=fingerprint,
tags=tags,
)
# Run before_notify callbacks
if not _run_before_notify(notice):
return None
# Handle testing mode
if Testing.is_enabled():
Testing._add_notice(notice)
return None
# Send asynchronously or synchronously
if _configuration.async_send and _worker:
_worker.push(notice)
return None
else:
client = Client(_configuration)
response = client.send(notice)
return response.get("id") if response else None
def notify_sync(
exception: BaseException,
context: Optional[dict[str, Any]] = None,
user: Optional[dict[str, Any]] = None,
request: Optional[dict[str, Any]] = None,
fingerprint: Optional[str] = None,
tags: Optional[list[str]] = None,
) -> Optional[dict[str, Any]]:
"""
Report an exception to Checkend synchronously.
Args:
exception: The exception to report
context: Additional context data
user: User information
request: Request information
fingerprint: Custom fingerprint for grouping
tags: List of tags for the error
Returns:
API response dict with 'id' and 'problem_id', or None if skipped
"""
if not _initialized or not _configuration or not _configuration.enabled:
return None
# Check if exception should be ignored
if _should_ignore(exception):
return None
# Build notice
notice = _build_notice(
exception,
context=context,
user=user,
request=request,
fingerprint=fingerprint,
tags=tags,
)
# Run before_notify callbacks
if not _run_before_notify(notice):
return None
# Handle testing mode
if Testing.is_enabled():
Testing._add_notice(notice)
return {"id": 0, "problem_id": 0}
client = Client(_configuration)
return client.send(notice)
def set_context(context: dict[str, Any]) -> None:
"""Set context data for the current request/task."""
current = _context_var.get() or {}
updated = current.copy()
updated.update(context)
_context_var.set(updated)
def get_context() -> dict[str, Any]:
"""Get the current context data."""
value = _context_var.get()
return value.copy() if value else {}
def set_user(user: dict[str, Any]) -> None:
"""Set user information for the current request/task."""
_user_var.set(user)
def get_user() -> dict[str, Any]:
"""Get the current user information."""
value = _user_var.get()
return value.copy() if value else {}
def set_request(request: dict[str, Any]) -> None:
"""Set request information for the current request."""
_request_var.set(request)
def get_request() -> dict[str, Any]:
"""Get the current request information."""
value = _request_var.get()
return value.copy() if value else {}
def clear() -> None:
"""Clear all context, user, and request data."""
_context_var.set(None)
_user_var.set(None)
_request_var.set(None)
def flush(timeout: Optional[float] = None) -> None:
"""
Wait for all queued notices to be sent.
Args:
timeout: Maximum time to wait in seconds (default: 5)
"""
if _worker:
_worker.flush(timeout or 5.0)
def stop(timeout: Optional[float] = None) -> None:
"""
Stop the worker and wait for pending notices.
Args:
timeout: Maximum time to wait in seconds (default: 5)
"""
global _worker
if _worker:
_worker.stop(timeout or 5.0)
_worker = None
def reset() -> None:
"""Reset all state (useful for testing)."""
global _configuration, _worker, _initialized
stop()
_configuration = None
_initialized = False
clear()
Testing.teardown()
def _shutdown() -> None:
"""Shutdown handler called on program exit."""
stop(timeout=5.0)
def _should_ignore(exception: BaseException) -> bool:
"""Check if an exception should be ignored."""
if not _configuration:
return False
from checkend.filters.ignore_filter import IgnoreFilter
ignore_filter = IgnoreFilter(_configuration.ignored_exceptions)
return ignore_filter.should_ignore(exception)
def _build_notice(
exception: BaseException,
context: Optional[dict[str, Any]] = None,
user: Optional[dict[str, Any]] = None,
request: Optional[dict[str, Any]] = None,
fingerprint: Optional[str] = None,
tags: Optional[list[str]] = None,
) -> Notice:
"""Build a Notice from an exception."""
from checkend.notice_builder import NoticeBuilder
# Merge context from various sources
merged_context = get_context()
if context:
merged_context.update(context)
# Merge user from various sources
merged_user = get_user()
if user:
merged_user.update(user)
# Merge request from various sources
merged_request = get_request()
if request:
merged_request.update(request)
builder = NoticeBuilder(_configuration)
return builder.build(
exception,
context=merged_context,
user=merged_user,
request=merged_request,
fingerprint=fingerprint,
tags=tags,
)
def _run_before_notify(notice: Notice) -> bool:
"""Run before_notify callbacks. Returns False if notice should be skipped."""
if not _configuration or not _configuration.before_notify:
return True
for callback in _configuration.before_notify:
try:
result = callback(notice)
if result is False:
return False
except Exception:
# Ignore callback errors
pass
return True