Skip to content

Commit c3f097f

Browse files
authored
feat: Add os_distro information to events (#467)
1 parent ecd197a commit c3f097f

3 files changed

Lines changed: 31 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
feat: Add os_distro information to events

posthog/test/test_client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
21642164

21652165
@parameterized.expand(
21662166
[
2167-
# name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, platform_method, platform_return, distro_info
2167+
# name, sys_platform, version_info, expected_runtime, expected_version, expected_os, expected_os_version, expected_os_distro, platform_method, platform_return
21682168
(
21692169
"macOS",
21702170
"darwin",
@@ -2173,9 +2173,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
21732173
"3.8.10",
21742174
"Mac OS X",
21752175
"10.15.7",
2176+
None,
21762177
"mac_ver",
21772178
("10.15.7", "", ""),
2178-
None,
21792179
),
21802180
(
21812181
"Windows",
@@ -2185,9 +2185,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
21852185
"3.8.10",
21862186
"Windows",
21872187
"10",
2188+
None,
21882189
"win32_ver",
21892190
("10", "", "", ""),
2190-
None,
21912191
),
21922192
(
21932193
"Linux",
@@ -2197,9 +2197,9 @@ def test_device_id_from_context_is_used_in_flags_request(self, patch_flags):
21972197
"3.8.10",
21982198
"Linux",
21992199
"20.04",
2200+
"Ubuntu",
22002201
None,
22012202
None,
2202-
{"version": "20.04"},
22032203
),
22042204
]
22052205
)
@@ -2212,9 +2212,9 @@ def test_mock_system_context(
22122212
expected_version,
22132213
expected_os,
22142214
expected_os_version,
2215+
expected_os_distro,
22152216
platform_method,
22162217
platform_return,
2217-
distro_info,
22182218
):
22192219
"""Test that we can mock platform and sys for testing system_context"""
22202220
with mock.patch("posthog.utils.platform") as mock_platform:
@@ -2232,11 +2232,10 @@ def test_mock_system_context(
22322232

22332233
# Special handling for Linux which uses distro module
22342234
if sys_platform == "linux":
2235-
# Directly patch the get_os_info function to return our expected values
2236-
with mock.patch(
2237-
"posthog.utils.get_os_info",
2238-
return_value=(expected_os, expected_os_version),
2239-
):
2235+
with mock.patch("posthog.utils.distro") as mock_distro:
2236+
mock_distro.info.return_value = {"version": expected_os_version}
2237+
mock_distro.name.return_value = expected_os_distro or ""
2238+
22402239
from posthog.utils import system_context
22412240

22422241
context = system_context()
@@ -2254,6 +2253,9 @@ def test_mock_system_context(
22542253
"$os_version": expected_os_version,
22552254
}
22562255

2256+
if sys_platform == "linux":
2257+
expected_context["$os_distro"] = expected_os_distro
2258+
22572259
assert context == expected_context
22582260

22592261
@mock.patch("posthog.client.flags")

posthog/utils.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,12 @@ def str_iequals(value, comparand):
467467

468468
def get_os_info():
469469
"""
470-
Returns standardized OS name and version information.
470+
Returns standardized OS name, version and distro (in case of Linux) information.
471471
Similar to how user agent parsing works in JS.
472472
"""
473473
os_name = ""
474474
os_version = ""
475+
os_distro = ""
475476

476477
platform_name = sys.platform
477478

@@ -494,6 +495,9 @@ def get_os_info():
494495
linux_info = distro.info()
495496
if linux_info["version"]:
496497
os_version = linux_info["version"]
498+
linux_distro = distro.name()
499+
if linux_distro:
500+
os_distro = linux_distro
497501

498502
elif platform_name.startswith("freebsd"):
499503
os_name = "FreeBSD"
@@ -505,15 +509,19 @@ def get_os_info():
505509
if hasattr(platform, "release"):
506510
os_version = platform.release()
507511

508-
return os_name, os_version
512+
info = {
513+
"$os": os_name,
514+
"$os_version": os_version,
515+
}
516+
if os_distro:
517+
info["$os_distro"] = os_distro
509518

519+
return info
510520

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

522+
def system_context() -> dict[str, Any]:
514523
return {
515524
"$python_runtime": platform.python_implementation(),
516525
"$python_version": "%s.%s.%s" % (sys.version_info[:3]),
517-
"$os": os_name,
518-
"$os_version": os_version,
526+
**get_os_info(),
519527
}

0 commit comments

Comments
 (0)