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
12 changes: 12 additions & 0 deletions openfeature-provider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to `openfeature-provider-posthog` are documented here. This
file is maintained by [Sampo](https://github.com/bruits/sampo) from changesets in
`.sampo/changesets/` that target `pypi/openfeature-provider-posthog`.

## 0.1.167 — 2026-11-17

### Patch changes

- Updated dependencies: posthog@7.91.4

## 0.1.166 — 2026-11-17

### Patch changes

- Updated dependencies: posthog@7.91.3

## 0.1.165 — 2026-11-16

### Patch changes
Expand Down
2 changes: 1 addition & 1 deletion openfeature-provider/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "openfeature-provider-posthog"
version = "0.1.165"
version = "0.1.167"
description = "Official PostHog provider for the OpenFeature Python SDK."
readme = "README.md"
authors = [{ name = "PostHog", email = "engineering@posthog.com" }]
Expand Down
12 changes: 12 additions & 0 deletions posthog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# posthog

## 7.91.4 — 2026-11-17

### Patch changes

- [8a88a289](https://github.com/posthog/posthog-python/commit/8a88a289fe530d9983ddc5531480e3c84490d1ae) Exception autocapture now sends a truncated string for source lines over 1024 bytes in a frame's `context_line`, `pre_context` and `post_context`. Before, those lines were sent as `null`. — Thanks @Toraastrta for your first contribution 🎉!

## 7.91.3 — 2026-11-17

### Patch changes

- [5b7a91a3](https://github.com/posthog/posthog-python/commit/5b7a91a31f167090565c2934afa6c99a33207898) `capture_exception()` called without an exception now marks the exception it takes from `sys.exc_info()` as captured, so a later automatic capture of the same exception no longer sends a second `$exception` event. — Thanks @MaiAditya for your first contribution 🎉!

## 7.91.2 — 2026-11-16

### Patch changes
Expand Down
10 changes: 8 additions & 2 deletions posthog/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,10 @@ def capture_exception(
self.log.warning("No exception information available")
return None

if exception is None and exception_is_already_captured(exc_info):
self.log.debug("Exception already captured, skipping")
return None

exceptions = exceptions_from_error_tuple(exc_info)
event = handle_in_app(
{"exception": {"values": exceptions}},
Expand Down Expand Up @@ -1086,8 +1090,10 @@ def capture_exception(
options=kwargs.get("options"),
_capture_trace_context=True,
)
if exception is not None and result is not None:
mark_exception_as_captured(exception, result)
# Mark the exception as captured to prevent duplicate captures,
# including one taken from sys.exc_info()
if result is not None:
mark_exception_as_captured(exc_info, result)
return result
except Exception as error:
self.log.exception("Failed to capture exception: %s", error)
Expand Down
11 changes: 8 additions & 3 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,10 @@ def capture_exception(
self.log.warning("No exception information available")
return None

if exception is None and exception_is_already_captured(exc_info):
self.log.debug("Exception already captured, skipping")
return None

# Format stack trace for cymbal
capture_metadata_input = dict(kwargs).get("_capture_metadata")
capture_metadata = (
Expand Down Expand Up @@ -3025,9 +3029,10 @@ def capture_exception(
_capture_trace_context=True,
)

# Mark the exception as captured to prevent duplicate captures
if exception is not None and res is not None:
mark_exception_as_captured(exception, res)
# Mark the exception as captured to prevent duplicate captures,
# including one taken from sys.exc_info()
if res is not None:
mark_exception_as_captured(exc_info, res)

return res
except Exception as e:
Expand Down
22 changes: 14 additions & 8 deletions posthog/exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def get_lines_from_file(
loader=None, # type: Optional[Any]
module=None, # type: Optional[str]
):
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
# type: (...) -> Tuple[List[str], Optional[str], List[str]]
context_lines = 5
source = _get_source_lines(filename, loader, module)

Expand All @@ -423,14 +423,20 @@ def get_lines_from_file(
lower_bound = max(0, lineno - context_lines)
upper_bound = min(lineno + 1 + context_lines, len(source))

def strip_line(line):
# type: (str) -> str
# strip_string wraps a truncated line in an AnnotatedValue, which the
# event payload can't serialize, so keep only the truncated text.
stripped = strip_string(line.strip("\r\n"), max_length=max_length)
if isinstance(stripped, AnnotatedValue):
return cast(str, stripped.value)
return stripped

try:
pre_context = [
strip_string(line.strip("\r\n"), max_length=max_length)
for line in source[lower_bound:lineno]
]
context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
pre_context = [strip_line(line) for line in source[lower_bound:lineno]]
context_line = strip_line(source[lineno])
post_context = [
strip_string(line.strip("\r\n"), max_length=max_length)
strip_line(line)
for line in source[(lineno + 1) : upper_bound] # noqa: E203
]
return pre_context, context_line, post_context
Expand Down Expand Up @@ -464,7 +470,7 @@ def get_source_context(
tb_lineno, # type: int
max_value_length=None, # type: Optional[int]
):
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
# type: (...) -> Tuple[List[str], Optional[str], List[str]]
try:
abs_path = frame.f_code.co_filename # type: Optional[str]
except Exception:
Expand Down
46 changes: 46 additions & 0 deletions posthog/test/test_exception_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,49 @@ def test_never_raised_exception_stack_omits_sdk_frames_in_renamed_package():
functions = [frame["function"] for frame in exception["stacktrace"]["frames"]]
assert functions[-1] == "handler"
assert exception["mechanism"]["synthetic"] is True


def _captured_events(client_class, capture_handled, pass_exception):
from posthog.async_client import AsyncClient
from posthog.client import Client

clients = {"Client": Client, "AsyncClient": AsyncClient}
client = clients[client_class]("phc_test", send=False)
with patch.object(client, "capture", wraps=client.capture) as capture:
try:
capture_handled(client, pass_exception)
except ZeroDivisionError:
pass
return [call.args[0] for call in capture.call_args_list]


def _handled_then_reraised_in_context(client, pass_exception):
from posthog.contexts import new_context

with new_context(capture_exceptions=True, client=client):
try:
1 / 0
except ZeroDivisionError as error:
client.capture_exception(error if pass_exception else None)
raise


def _handled_twice(client, pass_exception):
try:
1 / 0
except ZeroDivisionError as error:
client.capture_exception(error if pass_exception else None)
client.capture_exception(error if pass_exception else None)


@pytest.mark.parametrize("client_class", ["Client", "AsyncClient"])
@pytest.mark.parametrize("pass_exception", [True, False])
@pytest.mark.parametrize(
"capture_handled", [_handled_then_reraised_in_context, _handled_twice]
)
def test_exception_from_sys_exc_info_is_captured_once(
client_class, capture_handled, pass_exception
):
assert _captured_events(client_class, capture_handled, pass_exception) == [
"$exception"
]
17 changes: 17 additions & 0 deletions posthog/test/test_exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def unavailable(module):
) == ([], "current", ["after"])


def test_source_context_truncates_long_lines_to_strings(tmp_path):
long_line = "x" * 1100
source = tmp_path / "application.py"
source.write_text("\n".join([long_line, long_line, long_line]))

pre_context, context_line, post_context = exception_utils.get_lines_from_file(
str(source), 1
)

truncated = "x" * 1021 + "..."
assert (pre_context, context_line, post_context) == (
[truncated],
truncated,
[truncated],
)


def test_source_context_handles_changed_files(tmp_path):
source = tmp_path / "application.py"
source.write_text("current\n")
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "7.91.2"
VERSION = "7.91.4"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "posthog"
version = "7.91.2"
version = "7.91.4"
description = "Integrate PostHog into any python application."
authors = [{ name = "PostHog", email = "engineering@posthog.com" }]
maintainers = [{ name = "PostHog", email = "engineering@posthog.com" }]
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading