Skip to content

Commit

Permalink
Improve Python language detection by linked libraries (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed authored Nov 26, 2024
1 parent b79ec33 commit f448924
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion procdiscovery/pkg/inspectors/langdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inspectors

import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/procdiscovery/pkg/inspectors/dotnet"
Expand Down Expand Up @@ -33,9 +34,9 @@ type VersionInspector interface {
var inspectorsList = []LanguageInspector{
&golang.GolangInspector{},
&java.JavaInspector{},
&python.PythonInspector{},
&dotnet.DotnetInspector{},
&nodejs.NodejsInspector{},
&python.PythonInspector{},
&mysql.MySQLInspector{},
&nginx.NginxInspector{},
}
Expand Down
43 changes: 41 additions & 2 deletions procdiscovery/pkg/inspectors/python/python.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package python

import (
"github.com/hashicorp/go-version"
"debug/elf"
"fmt"
"os"
"strings"

"github.com/hashicorp/go-version"

"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/procdiscovery/pkg/process"
)

type PythonInspector struct{}

const pythonProcessName = "python"
const (
pythonProcessName = "python"
libPythonStr = "libpython3"
)

func (p *PythonInspector) Inspect(proc *process.Details) (common.ProgrammingLanguage, bool) {
if strings.Contains(proc.ExeName, pythonProcessName) || strings.Contains(proc.CmdLine, pythonProcessName) {
return common.PythonProgrammingLanguage, true
}

if p.isLibPythonLinked(proc) {
return common.PythonProgrammingLanguage, true
}

return "", false
}

Expand All @@ -27,3 +38,31 @@ func (p *PythonInspector) GetRuntimeVersion(proc *process.Details, containerURL

return nil
}

func (p *PythonInspector) isLibPythonLinked(proc *process.Details) bool {
f := fmt.Sprintf("/proc/%d/exe", proc.ProcessID)
file, err := os.Open(f)
if err != nil {
return false
}
defer file.Close()

elfFile, err := elf.NewFile(file)
if err != nil {
return false
}
defer elfFile.Close()

dynamicSection, err := elfFile.DynString(elf.DT_NEEDED)
if err != nil {
return false
}

for _, dep := range dynamicSection {
if strings.Contains(dep, libPythonStr) {
return true
}
}

return false
}

0 comments on commit f448924

Please sign in to comment.