diff --git a/openfeature-provider/CHANGELOG.md b/openfeature-provider/CHANGELOG.md index dd1423d8f..355c92386 100644 --- a/openfeature-provider/CHANGELOG.md +++ b/openfeature-provider/CHANGELOG.md @@ -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 diff --git a/openfeature-provider/pyproject.toml b/openfeature-provider/pyproject.toml index fed64fa5d..4aecc0922 100644 --- a/openfeature-provider/pyproject.toml +++ b/openfeature-provider/pyproject.toml @@ -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" }] diff --git a/posthog/CHANGELOG.md b/posthog/CHANGELOG.md index 9d9cd52c4..c79e2cac6 100644 --- a/posthog/CHANGELOG.md +++ b/posthog/CHANGELOG.md @@ -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 diff --git a/posthog/async_client.py b/posthog/async_client.py index c11153207..7401e3740 100644 --- a/posthog/async_client.py +++ b/posthog/async_client.py @@ -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}}, @@ -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) diff --git a/posthog/client.py b/posthog/client.py index 67a10c710..d71aa13d7 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -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 = ( @@ -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: diff --git a/posthog/exception_utils.py b/posthog/exception_utils.py index 29c5fa6ec..da0d1dbcd 100644 --- a/posthog/exception_utils.py +++ b/posthog/exception_utils.py @@ -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) @@ -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 @@ -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: diff --git a/posthog/test/test_exception_capture.py b/posthog/test/test_exception_capture.py index d8d2f0cd9..6422a92d9 100644 --- a/posthog/test/test_exception_capture.py +++ b/posthog/test/test_exception_capture.py @@ -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" + ] diff --git a/posthog/test/test_exception_utils.py b/posthog/test/test_exception_utils.py index e719c8c31..063194618 100644 --- a/posthog/test/test_exception_utils.py +++ b/posthog/test/test_exception_utils.py @@ -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") diff --git a/posthog/version.py b/posthog/version.py index d29da7e01..04490f216 100644 --- a/posthog/version.py +++ b/posthog/version.py @@ -1 +1 @@ -VERSION = "7.91.2" +VERSION = "7.91.4" diff --git a/pyproject.toml b/pyproject.toml index 217f1b1e9..7eeaa0c9e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }] diff --git a/uv.lock b/uv.lock index 593e7336a..746417428 100644 --- a/uv.lock +++ b/uv.lock @@ -2898,7 +2898,7 @@ wheels = [ [[package]] name = "openfeature-provider-posthog" -version = "0.1.165" +version = "0.1.167" source = { editable = "openfeature-provider" } dependencies = [ { name = "openfeature-sdk" }, @@ -3455,7 +3455,7 @@ wheels = [ [[package]] name = "posthog" -version = "7.91.2" +version = "7.91.4" source = { editable = "." } dependencies = [ { name = "distro" },