Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 .sampo/changesets/heroic-shaman-hiisi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

feat: Add os_distro information to events
22 changes: 12 additions & 10 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):

@parameterized.expand(
[
# name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, platform_method, platform_return, distro_info
# name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, expected_os_distro, platform_method, platform_return
(
"macOS",
"darwin",
Expand All @@ -2173,9 +2173,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
"3.8.10",
"Mac OS X",
"10.15.7",
None,
"mac_ver",
("10.15.7", "", ""),
None,
),
(
"Windows",
Expand All @@ -2185,9 +2185,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
"3.8.10",
"Windows",
"10",
None,
"win32_ver",
("10", "", "", ""),
None,
),
(
"Linux",
Expand All @@ -2197,9 +2197,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
"3.8.10",
"Linux",
"20.04",
"Ubuntu",
None,
None,
{"version": "20.04"},
),
]
)
Expand All @@ -2212,9 +2212,9 @@ def test_mock_system_context(
expected_version,
expected_os,
expected_os_version,
expected_os_distro,
platform_method,
platform_return,
distro_info,
):
"""Test that we can mock platform and sys for testing system_context"""
with mock.patch("posthog.utils.platform") as mock_platform:
Expand All @@ -2232,11 +2232,10 @@ def test_mock_system_context(

# Special handling for Linux which uses distro module
if sys_platform == "linux":
# Directly patch the get_os_info function to return our expected values
with mock.patch(
"posthog.utils.get_os_info",
return_value=(expected_os, expected_os_version),
):
with mock.patch("posthog.utils.distro") as mock_distro:
mock_distro.info.return_value = {"version": expected_os_version}
mock_distro.name.return_value = expected_os_distro or ""

from posthog.utils import system_context

context = system_context()
Expand All @@ -2254,6 +2253,9 @@ def test_mock_system_context(
"$os_version": expected_os_version,
}

if sys_platform == "linux":
expected_context["$os_distro"] = expected_os_distro

assert context == expected_context

@mock.patch("posthog.client.flags")
Expand Down
20 changes: 14 additions & 6 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,12 @@ def str_iequals(value, comparand):

def get_os_info():
"""
Returns standardized OS name and version information.
Returns standardized OS name, version and distro (in case of Linux) information.
Similar to how user agent parsing works in JS.
"""
os_name = ""
os_version = ""
os_distro = ""

platform_name = sys.platform

Expand All @@ -494,6 +495,9 @@ def get_os_info():
linux_info = distro.info()
if linux_info["version"]:
os_version = linux_info["version"]
linux_distro = distro.name()
if linux_distro:
os_distro = linux_distro

elif platform_name.startswith("freebsd"):
os_name = "FreeBSD"
Expand All @@ -505,15 +509,19 @@ def get_os_info():
if hasattr(platform, "release"):
os_version = platform.release()

return os_name, os_version
info = {
"$os": os_name,
"$os_version": os_version,
}
if os_name == "Linux":
info["$os_distro"] = os_distro

return info

def system_context() -> dict[str, Any]:
os_name, os_version = get_os_info()

def system_context() -> dict[str, Any]:
return {
"$python_runtime": platform.python_implementation(),
"$python_version": "%s.%s.%s" % (sys.version_info[:3]),
"$os": os_name,
"$os_version": os_version,
**get_os_info(),
}
Loading