From 6251260d14beb84bebc200a795e07adad393672b Mon Sep 17 00:00:00 2001 From: Vincent Whitchurch Date: Thu, 28 Nov 2024 12:28:27 +0100 Subject: [PATCH 1/2] discovery: Tweak Python name heuristics Add a couple of small improvements to the Python name heuristics based on real data seen during testing: - If the directory name is not useful ("/" or "bin"), for example, try falling back to the filename. - Ignore the -u flag while skipping arguments since some cases were seen with "python -u foo" which ended up not generating a name. --- .../corechecks/servicediscovery/usm/python.go | 13 ++++++-- .../servicediscovery/usm/python_test.go | 30 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/pkg/collector/corechecks/servicediscovery/usm/python.go b/pkg/collector/corechecks/servicediscovery/usm/python.go index 16d774c191e01..44b2c02309463 100644 --- a/pkg/collector/corechecks/servicediscovery/usm/python.go +++ b/pkg/collector/corechecks/servicediscovery/usm/python.go @@ -78,14 +78,23 @@ func (p pythonDetector) detect(args []string) (ServiceMetadata, bool) { if value, ok := p.deducePackageName(path.Clean(stripped), filename); ok { return NewServiceMetadata(value), true } - return NewServiceMetadata(p.findNearestTopLevel(stripped)), true + + name := p.findNearestTopLevel(stripped) + // If we have generic/useless directory names, fallback to the filename. + if name == "." || name == "/" || name == "bin" || name == "sbin" { + name = p.findNearestTopLevel(filename) + } + + return NewServiceMetadata(name), true } if hasFlagPrefix && a == "-m" { moduleFlag = true } - prevArgIsFlag = hasFlagPrefix + // The -u (unbuffered) option doesn't take an argument so we should + // consider the next arg even though this one is a flag. + prevArgIsFlag = hasFlagPrefix && a != "-u" } return ServiceMetadata{}, false diff --git a/pkg/collector/corechecks/servicediscovery/usm/python_test.go b/pkg/collector/corechecks/servicediscovery/usm/python_test.go index cfa80ae32413a..d5f0c42acb3fe 100644 --- a/pkg/collector/corechecks/servicediscovery/usm/python_test.go +++ b/pkg/collector/corechecks/servicediscovery/usm/python_test.go @@ -16,8 +16,9 @@ import ( ) func TestPythonDetect(t *testing.T) { - //prepare the in mem fs - memFs := fstest.MapFS{ + // Wrap the MapFS in a SubDirFS since we want to test absolute paths and the + // former doesn't allow them in calls to Open(). + memFs := SubDirFS{FS: fstest.MapFS{ "modules/m1/first/nice/package/__init__.py": &fstest.MapFile{}, "modules/m1/first/nice/__init__.py": &fstest.MapFile{}, "modules/m1/first/nice/something.py": &fstest.MapFile{}, @@ -28,7 +29,9 @@ func TestPythonDetect(t *testing.T) { "apps/app2/cmd/run.py": &fstest.MapFile{}, "apps/app2/setup.py": &fstest.MapFile{}, "example.py": &fstest.MapFile{}, - } + "usr/bin/pytest": &fstest.MapFile{}, + "bin/WALinuxAgent.egg": &fstest.MapFile{}, + }} tests := []struct { name string cmd string @@ -79,6 +82,27 @@ func TestPythonDetect(t *testing.T) { cmd: "python example.py", expected: "example", }, + { + name: "root level script", + cmd: "python /example.py", + expected: "example", + }, + { + name: "root level script with ..", + // This results in a path of "." after findNearestTopLevel is called on the split path. + cmd: "python /../example.py", + expected: "example", + }, + { + name: "script in bin", + cmd: "python /usr/bin/pytest", + expected: "pytest", + }, + { + name: "script in bin with -u", + cmd: "python3 -u bin/WALinuxAgent.egg", + expected: "WALinuxAgent", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From a8dddfe08f5977bc43f829741d9cec7aeb210214 Mon Sep 17 00:00:00 2001 From: Vincent Whitchurch Date: Thu, 28 Nov 2024 15:00:08 +0100 Subject: [PATCH 2/2] Disable Python test on Windows The SubDirFs stuff doesn't work on Windows so just disable the tests there. --- pkg/collector/corechecks/servicediscovery/usm/python_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/collector/corechecks/servicediscovery/usm/python_test.go b/pkg/collector/corechecks/servicediscovery/usm/python_test.go index d5f0c42acb3fe..631c843e8a9d7 100644 --- a/pkg/collector/corechecks/servicediscovery/usm/python_test.go +++ b/pkg/collector/corechecks/servicediscovery/usm/python_test.go @@ -3,6 +3,8 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. +//go:build !windows + package usm import (