Skip to content

Commit 492ea23

Browse files
authored
Merge pull request #599 from Open-Source-Legal/JSv4/update-posthog-telemetry
Update Posthog to Use Singleton Approach
2 parents 778f3bb + 1242ea3 commit 492ea23

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

.github/workflows/claude-code-review.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,3 @@ jobs:
5454
# See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
5555
# or https://docs.claude.com/en/docs/claude-code/cli-reference for available options
5656
claude_args: '--allowed-tools "Bash(gh issue view:*),Bash(gh search:*),Bash(gh issue list:*),Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr list:*)"'
57-

.github/workflows/claude.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,3 @@ jobs:
4747
# See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
4848
# or https://docs.claude.com/en/docs/claude-code/cli-reference for available options
4949
# claude_args: '--allowed-tools Bash(gh pr:*)'
50-

config/telemetry.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import logging
44
from datetime import datetime, timezone
55

6+
import posthog
67
from django.conf import settings
7-
from posthog import Posthog
88

99
logger = logging.getLogger(__name__)
1010

@@ -46,11 +46,8 @@ def record_event(event_type: str, properties: dict | None = None) -> bool:
4646
return False
4747

4848
try:
49-
client = Posthog(
50-
project_api_key=settings.POSTHOG_API_KEY, host=settings.POSTHOG_HOST
51-
)
52-
53-
client.capture(
49+
# Use the globally configured posthog module (initialized in UsersConfig.ready())
50+
posthog.capture(
5451
distinct_id=installation_id,
5552
event=f"opencontracts.{event_type}",
5653
properties={

opencontractserver/tests/test_telemetry.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from unittest.mock import MagicMock, patch
2+
from unittest.mock import patch
33

44
from django.test import TestCase, override_settings
55

@@ -13,11 +13,9 @@ def setUp(self):
1313
self.mock_installation = Installation.get()
1414
self.installation_id = self.mock_installation.id
1515

16-
# Set up PostHog mock
17-
self.posthog_patcher = patch("config.telemetry.Posthog")
18-
self.mock_posthog_class = self.posthog_patcher.start()
19-
self.mock_posthog = MagicMock()
20-
self.mock_posthog_class.return_value = self.mock_posthog
16+
# Set up PostHog mock - now patching the module-level capture function
17+
self.posthog_patcher = patch("config.telemetry.posthog.capture")
18+
self.mock_posthog_capture = self.posthog_patcher.start()
2119

2220
def tearDown(self):
2321
self.posthog_patcher.stop()
@@ -34,10 +32,10 @@ def test_record_event_success(self):
3432
result = record_event("test_event", {"test_prop": "value"})
3533

3634
self.assertTrue(result)
37-
self.mock_posthog.capture.assert_called_once()
35+
self.mock_posthog_capture.assert_called_once()
3836

3937
# Verify the capture call arguments
40-
call_args = self.mock_posthog.capture.call_args[1]
38+
call_args = self.mock_posthog_capture.call_args[1]
4139
self.assertEqual(call_args["distinct_id"], str(self.installation_id))
4240
self.assertEqual(call_args["event"], "opencontracts.test_event")
4341
self.assertEqual(call_args["properties"]["package"], "opencontracts")
@@ -58,7 +56,7 @@ def test_record_event_telemetry_disabled(self):
5856
result = record_event("test_event")
5957

6058
self.assertFalse(result)
61-
self.mock_posthog.capture.assert_not_called()
59+
self.mock_posthog_capture.assert_not_called()
6260

6361
def test_record_event_installation_inactive(self):
6462
"""Test when installation exists but is inactive"""
@@ -67,11 +65,11 @@ def test_record_event_installation_inactive(self):
6765
result = record_event("test_event")
6866

6967
self.assertFalse(result)
70-
self.mock_posthog.capture.assert_not_called()
68+
self.mock_posthog_capture.assert_not_called()
7169

7270
def test_record_event_posthog_error(self):
7371
"""Test when PostHog client raises an error"""
74-
self.mock_posthog.capture.side_effect = Exception("PostHog Error")
72+
self.mock_posthog_capture.side_effect = Exception("PostHog Error")
7573

7674
with override_settings(MODE="DEV", TELEMETRY_ENABLED=True):
7775
result = record_event("test_event")
@@ -85,10 +83,10 @@ def test_record_event_without_properties(self):
8583
result = record_event("test_event")
8684

8785
self.assertTrue(result)
88-
self.mock_posthog.capture.assert_called_once()
86+
self.mock_posthog_capture.assert_called_once()
8987

9088
# Verify only default properties are present
91-
properties = self.mock_posthog.capture.call_args[1]["properties"]
89+
properties = self.mock_posthog_capture.call_args[1]["properties"]
9290
self.assertEqual(
9391
set(properties.keys()), {"package", "timestamp", "installation_id"}
9492
)

opencontractserver/users/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ class UsersConfig(AppConfig):
77
verbose_name = _("Users")
88

99
def ready(self):
10+
import posthog
11+
from django.conf import settings
12+
13+
# Initialize PostHog globally as per official Django integration
14+
if settings.TELEMETRY_ENABLED:
15+
posthog.api_key = settings.POSTHOG_API_KEY
16+
posthog.host = settings.POSTHOG_HOST
17+
1018
try:
1119
import opencontractserver.users.signals # noqa F401
1220
except ImportError:

0 commit comments

Comments
 (0)