Repository: scoutapp/scout_apm_python
Pull request #585 (2020-09-17 07:59:24)

Default to musl core agent on Linux
As discussed on Slack.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 335968a..1433464 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
 # Changelog
 
+## Pending
+
+- Moved core agent on Linux to default to the musl version, rather than try
+  detect whether to use the musl or GNU libc version.
+  ([PR #585](https://github.com/scoutapp/scout_apm_python/pull/585))
+
 ## [2.16.1] 2020-09-09
 
 ### Fixed
diff --git a/src/scout_apm/core/platform_detection.py b/src/scout_apm/core/platform_detection.py
index a841772..fea8a6c 100644
--- a/src/scout_apm/core/platform_detection.py
+++ b/src/scout_apm/core/platform_detection.py
@@ -2,7 +2,6 @@
 from __future__ import absolute_import, division, print_function, unicode_literals
 
 import platform
-import subprocess
 
 
 def is_valid_triple(triple):
@@ -38,35 +37,11 @@ def get_platform():
     """
     system_name = platform.system()
     if system_name == "Linux":
-        libc = get_libc()
-        return "unknown-linux-{libc}".format(libc=libc)
+        # Previously we'd use either "-gnu" or "-musl" indicate which version
+        # of libc we were built against. We now default to musl since it
+        # reliably works on all platforms.
+        return "unknown-linux-musl"
     elif system_name == "Darwin":
         return "apple-darwin"
     else:
         return "unknown"
-
-
-_libc = None
-
-
-def get_libc():
-    """
-    Alpine linux uses a non glibc version of the standard library, it uses
-    the stripped down musl instead. The core agent can be built against it,
-    but which one is running must be detected. Shelling out to `ldd`
-    appears to be the most reliable way to do this.
-    """
-    global _libc
-    if _libc is None:
-        try:
-            output = subprocess.check_output(
-                ["ldd", "--version"], stderr=subprocess.STDOUT, close_fds=True
-            )
-        except (OSError, subprocess.CalledProcessError):
-            _libc = "gnu"
-        else:
-            if b"musl" in output:
-                _libc = "musl"
-            else:
-                _libc = "gnu"
-    return _libc


Test patch:
diff --git a/tests/unit/core/test_platform_detection.py b/tests/unit/core/test_platform_detection.py
index b88c248..141a825 100644
--- a/tests/unit/core/test_platform_detection.py
+++ b/tests/unit/core/test_platform_detection.py
@@ -50,7 +50,7 @@ def test_get_arch(platform_machine, machine, arch):
     "system, platform",
     [
         ("Darwin", "apple-darwin"),
-        ("Linux", "unknown-linux-gnu"),
+        ("Linux", "unknown-linux-musl"),
         ("Windows", "unknown"),
         ("", "unknown"),
     ],
@@ -59,25 +59,3 @@ def test_get_arch(platform_machine, machine, arch):
 def test_get_platform(platform_system, system, platform):
     platform_system.return_value = system
     assert platform_detection.get_platform() == platform
-
-
-@pytest.mark.parametrize(
-    "output, libc",
-    [
-        (b"ldd (GNU libc) 2.17\n", "gnu"),
-        (b"musl libc (x86_64)\nVersion 1.1.18\n", "musl"),
-        (b"", "gnu"),
-    ],
-)
-@mock.patch("subprocess.check_output")
-def test_get_libc(check_output, output, libc):
-    platform_detection._libc = None  # reset cache
-    check_output.return_value = output
-    assert platform_detection.get_libc() == libc
-
-
-@mock.patch("subprocess.check_output")
-def test_get_libc_no_ldd(check_output):
-    platform_detection._libc = None  # reset cache
-    check_output.side_effect = OSError
-    assert platform_detection.get_libc() == "gnu"