@@ -12,31 +12,34 @@ def _get_current_context() -> Dict[str, Any]:
1212
1313
1414@contextmanager
15- def new_context (fresh = False ):
15+ def new_context (fresh = False , capture_exceptions = True ):
1616 """
17- Create a new context scope that will be active for the duration of the with block.
18- Any tags set within this scope will be isolated to this context. Any exceptions raised
17+ Create a new context scope that will be active for the duration of the with block.
18+ Any tags set within this scope will be isolated to this context. Any exceptions raised
1919 or events captured within the context will be tagged with the context tags.
2020
21- Args:
22- fresh: Whether to start with a fresh context (default: False).
23- If False, inherits tags from parent context.
24- If True, starts with no tags.
25-
26- Examples:
27- # Inherit parent context tags
28- with posthog.new_context():
29- posthog.tag("request_id", "123")
30- # Both this event and the exception will be tagged with the context tags
31- posthog.capture("event_name", {"property": "value"})
32- raise ValueError("Something went wrong")
33-
34- # Start with fresh context (no inherited tags)
35- with posthog.new_context(fresh=True):
36- posthog.tag("request_id", "123")
37- # Both this event and the exception will be tagged with the context tags
38- posthog.capture("event_name", {"property": "value"})
39- raise ValueError("Something went wrong")
21+ Args:
22+ fresh: Whether to start with a fresh context (default: False).
23+ If False, inherits tags from parent context.
24+ If True, starts with no tags.
25+ capture_exceptions: Whether to capture exceptions raised within the context (default: True).
26+ If True, captures exceptions and tags them with the context tags before propagating them.
27+ If False, exceptions will propagate without being tagged or captured.
28+
29+ Examples:
30+ # Inherit parent context tags
31+ with posthog.new_context():
32+ posthog.tag("request_id", "123")
33+ # Both this event and the exception will be tagged with the context tags
34+ posthog.capture("event_name", {"property": "value"})
35+ raise ValueError("Something went wrong")
36+
37+ # Start with fresh context (no inherited tags)
38+ with posthog.new_context(fresh=True):
39+ posthog.tag("request_id", "123")
40+ # Both this event and the exception will be tagged with the context tags
41+ posthog.capture("event_name", {"property": "value"})
42+ raise ValueError("Something went wrong")
4043
4144 """
4245 from posthog import capture_exception
@@ -49,7 +52,8 @@ def new_context(fresh=False):
4952 try :
5053 yield
5154 except Exception as e :
52- capture_exception (e )
55+ if capture_exceptions :
56+ capture_exception (e )
5357 raise
5458 finally :
5559 _context_stack .reset (token )
@@ -88,13 +92,14 @@ def clear_tags() -> None:
8892F = TypeVar ("F" , bound = Callable [..., Any ])
8993
9094
91- def scoped (fresh = False ):
95+ def scoped (fresh = False , capture_exceptions = True ):
9296 """
9397 Decorator that creates a new context for the function. Simply wraps
9498 the function in a with posthog.new_context(): block.
9599
96100 Args:
97101 fresh: Whether to start with a fresh context (default: False)
102+ capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)
98103
99104 Example:
100105 @posthog.scoped()
@@ -114,7 +119,7 @@ def decorator(func: F) -> F:
114119
115120 @wraps (func )
116121 def wrapper (* args , ** kwargs ):
117- with new_context (fresh = fresh ):
122+ with new_context (fresh = fresh , capture_exceptions = capture_exceptions ):
118123 return func (* args , ** kwargs )
119124
120125 return cast (F , wrapper )
0 commit comments