@@ -14,25 +14,29 @@ def new_context(fresh=False):
1414 # TODO - we could extend this context idea to also apply to other event types eventually,
1515 # but right now it only applies to exceptions...
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
19- within the context will be captured and tagged with the context tags.
20-
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("user_id", "123")
30- raise ValueError("Something went wrong")
31-
32- # Start with fresh context (no inherited tags)
33- with posthog.new_context(fresh=True):
34- posthog.tag("user_id", "123")
35- raise ValueError("Something went wrong")
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
19+ or events captured within the context will be tagged with the context tags.
20+
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")
3640
3741 """
3842 import posthog
@@ -86,9 +90,8 @@ def clear_tags() -> None:
8690
8791def scoped (fresh = False ):
8892 """
89- Decorator that creates a new context for the function, wraps the function in a
90- try/except block, and if an exception occurs, captures it with the current context
91- tags before re-raising it.
93+ Decorator that creates a new context for the function. Simply wraps
94+ the function in a with posthog.new_context(): block.
9295
9396 Args:
9497 fresh: Whether to start with a fresh context (default: False)
@@ -98,8 +101,12 @@ def scoped(fresh=False):
98101 def process_payment(payment_id):
99102 posthog.tag("payment_id", payment_id)
100103 posthog.tag("payment_method", "credit_card")
104+
105+ # This event will be captured with tags
106+ posthog.capture("payment_started")
101107 # If this raises an exception, it will be captured with tags
102108 # and then re-raised
109+ some_risky_function()
103110 """
104111
105112 def decorator (func : F ) -> F :
0 commit comments