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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
test:
strategy:
matrix:
python-version: [3.8, 3.9, '3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand Down
3 changes: 2 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ conda:

sphinx:
builder: html
fail_on_warning: true
fail_on_warning: true
configuration: doc/_config.yml
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

REQUIRES = [
"pyyaml",
"posthog",
"posthog>=3.0",
'importlib-metadata;python_version<"3.8"',
]

Expand Down
30 changes: 24 additions & 6 deletions src/ploomber_core/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ def __init__(self, api_key, package_name, version, *, print_cloud_message=True):
self.version = version
self.print_cloud_message = print_cloud_message

# Initialize PostHog client
try:
self._posthog_client = posthog.Posthog(
api_key, host="https://us.i.posthog.com"
)
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't a good way to handle an incompatibilty, instead we can raise an error telling users they must upgrade posthog or downgrade ploomber-core

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: ca6301a

raise ImportError(
"Failed to initialize posthog client. This likely means your posthog "
"version is incompatible. To fix this, either upgrade to posthog>=3.0 "
"or downgrade to ploomber-core<=0.2.26"
) from e

@classmethod
def from_package(cls, package_name, *, print_cloud_message=True, api_key=None):
"""
Expand All @@ -440,7 +452,6 @@ def log_api(self, action, client_time=None, total_runtime=None, metadata=None):
if missing like timestamp, event id and stats information.
"""

posthog.project_api_key = self.api_key
metadata = metadata or {}

event_id = uuid4()
Expand Down Expand Up @@ -516,12 +527,19 @@ def log_api(self, action, client_time=None, total_runtime=None, metadata=None):
"metadata": metadata,
}

if is_install:
posthog.capture(
distinct_id=uid, event="install_success_indirect", properties=props
)
if self._posthog_client is not None:
if is_install:
self._posthog_client.capture(
distinct_id=uid,
event="install_success_indirect",
properties=props,
)

posthog.capture(distinct_id=uid, event=action, properties=props)
self._posthog_client.capture(
distinct_id=uid, event=action, properties=props
)
else:
raise RuntimeError("Log call failed: PostHog client not initialized")

# NOTE: should we log differently depending on the error type?
# NOTE: how should we handle chained exceptions?
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ def external_access(request, monkeypatch_session):
# https://github.com/pytest-dev/pytest/issues/7061#issuecomment-611892868
external_access = MagicMock()
external_access.get_something = MagicMock(return_value="Mock was used.")
monkeypatch_session.setattr(posthog, "capture", external_access.get_something)

mock_posthog_instance = MagicMock()
mock_posthog_instance.capture = external_access.get_something
mock_posthog_class = MagicMock(return_value=mock_posthog_instance)

monkeypatch_session.setattr(posthog, "Posthog", mock_posthog_class)
yield
26 changes: 19 additions & 7 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,11 @@ def fake_capture(*args, **kwargs):
log = logging.getLogger("posthog")
log.error("some error happened")

monkeypatch.setattr(posthog, "capture", fake_capture)
mock_posthog_instance = Mock()
mock_posthog_instance.capture = fake_capture
mock_posthog_class = Mock(return_value=mock_posthog_instance)

monkeypatch.setattr(posthog, "Posthog", mock_posthog_class)
_telemetry = telemetry.Telemetry(MOCK_API_KEY, "ploomber", "0.14.0")

with caplog.at_level(logging.ERROR, logger="posthog"):
Expand All @@ -838,8 +842,12 @@ def fake_capture(*args, **kwargs):
# TODO: test more of the values (I'm adding ANY to many of them)
def test_log_api_stored_values(monkeypatch):
mock_info = Mock(return_value=(True, "fake-uuid", False))
mock = Mock()
monkeypatch.setattr(telemetry.posthog, "capture", mock)
mock_capture = Mock()
mock_posthog_instance = Mock()
mock_posthog_instance.capture = mock_capture
mock_posthog_class = Mock(return_value=mock_posthog_instance)

monkeypatch.setattr(telemetry.posthog, "Posthog", mock_posthog_class)
monkeypatch.setattr(telemetry, "_get_telemetry_info", mock_info)

_telemetry = telemetry.Telemetry(MOCK_API_KEY, "some-package", "1.2.2")
Expand All @@ -851,7 +859,7 @@ def test_log_api_stored_values(monkeypatch):
f"{sys.version_info.micro}"
)

mock.assert_called_once_with(
mock_capture.assert_called_once_with(
distinct_id="fake-uuid",
event="some-action",
properties={
Expand All @@ -876,8 +884,12 @@ def test_log_api_stored_values(monkeypatch):

def test_log_call_stored_values(monkeypatch):
mock_info = Mock(return_value=(True, "fake-uuid", False))
mock = Mock()
monkeypatch.setattr(telemetry.posthog, "capture", mock)
mock_capture = Mock()
mock_posthog_instance = Mock()
mock_posthog_instance.capture = mock_capture
mock_posthog_class = Mock(return_value=mock_posthog_instance)

monkeypatch.setattr(telemetry.posthog, "Posthog", mock_posthog_class)
monkeypatch.setattr(telemetry, "_get_telemetry_info", mock_info)
monkeypatch.setattr(telemetry.sys, "argv", ["/path/to/bin", "arg2", "arg2"])

Expand All @@ -894,7 +906,7 @@ def my_function():
f"{sys.version_info.micro}"
)

assert mock.call_args_list == [
assert mock_capture.call_args_list == [
call(
distinct_id="fake-uuid",
event="some-package-some-action-success",
Expand Down
10 changes: 7 additions & 3 deletions tests/telemetry/test_telemetry_log_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
@pytest.fixture
def mock_posthog(monkeypatch):
mock_info = Mock(return_value=(True, "UUID", False))
mock = Mock()
monkeypatch.setattr(telemetry_module.posthog, "capture", mock)
mock_capture = Mock()
mock_posthog_instance = Mock()
mock_posthog_instance.capture = mock_capture
mock_posthog_class = Mock(return_value=mock_posthog_instance)

monkeypatch.setattr(telemetry_module.posthog, "Posthog", mock_posthog_class)
monkeypatch.setattr(telemetry_module, "_get_telemetry_info", mock_info)
yield mock
yield mock_capture


@pytest.mark.parametrize(
Expand Down
Loading