Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.8.0 - 2025-06-10

- fix: export scoped, rather than tracked, decorator
- feat: allow use of contexts without error tracking

## 4.7.0 - 2025-06-10

- feat: add support for parse endpoint in responses API (no longer beta)
Expand Down
2 changes: 1 addition & 1 deletion posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
tag = tag
get_tags = get_tags
clear_tags = clear_tags
tracked = scoped
scoped = scoped

"""Settings."""
api_key = None # type: Optional[str]
Expand Down
55 changes: 30 additions & 25 deletions posthog/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@ def _get_current_context() -> Dict[str, Any]:


@contextmanager
def new_context(fresh=False):
def new_context(fresh=False, capture_exceptions=True):
"""
Create a new context scope that will be active for the duration of the with block.
Any tags set within this scope will be isolated to this context. Any exceptions raised
Create a new context scope that will be active for the duration of the with block.
Any tags set within this scope will be isolated to this context. Any exceptions raised
or events captured within the context will be tagged with the context tags.

Args:
fresh: Whether to start with a fresh context (default: False).
If False, inherits tags from parent context.
If True, starts with no tags.

Examples:
# Inherit parent context tags
with posthog.new_context():
posthog.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")

# Start with fresh context (no inherited tags)
with posthog.new_context(fresh=True):
posthog.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")
Args:
fresh: Whether to start with a fresh context (default: False).
If False, inherits tags from parent context.
If True, starts with no tags.
capture_exceptions: Whether to capture exceptions raised within the context (default: True).
If True, captures exceptions and tags them with the context tags before propagating them.
If False, exceptions will propagate without being tagged or captured.

Examples:
# Inherit parent context tags
with posthog.new_context():
posthog.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")

# Start with fresh context (no inherited tags)
with posthog.new_context(fresh=True):
posthog.tag("request_id", "123")
# Both this event and the exception will be tagged with the context tags
posthog.capture("event_name", {"property": "value"})
raise ValueError("Something went wrong")

"""
from posthog import capture_exception
Expand All @@ -49,7 +52,8 @@ def new_context(fresh=False):
try:
yield
except Exception as e:
capture_exception(e)
if capture_exceptions:
capture_exception(e)
raise
finally:
_context_stack.reset(token)
Expand Down Expand Up @@ -88,13 +92,14 @@ def clear_tags() -> None:
F = TypeVar("F", bound=Callable[..., Any])


def scoped(fresh=False):
def scoped(fresh=False, capture_exceptions=True):
"""
Decorator that creates a new context for the function. Simply wraps
the function in a with posthog.new_context(): block.

Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)

Example:
@posthog.scoped()
Expand All @@ -114,7 +119,7 @@ def decorator(func: F) -> F:

@wraps(func)
def wrapper(*args, **kwargs):
with new_context(fresh=fresh):
with new_context(fresh=fresh, capture_exceptions=capture_exceptions):
return func(*args, **kwargs)

return cast(F, wrapper)
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "4.7.0"
VERSION = "4.8.0"

if __name__ == "__main__":
print(VERSION, end="") # noqa: T201
Loading