Skip to content

Commit

Permalink
Add detection for PHP language (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
marctc authored Jun 26, 2024
1 parent b3ed5a5 commit 7c41a6e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/internal/discover/typer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (t *typer) asInstrumentable(execElf *exec.FileInfo) Instrumentable {
parent, ok = t.currentPids[parent.Ppid]
}

detectedType := exec.FindProcLanguage(execElf.Pid, execElf.ELF)
detectedType := exec.FindProcLanguage(execElf.Pid, execElf.ELF, execElf.CmdExePath)

log.Debug("instrumented", "comm", execElf.CmdExePath, "pid", execElf.Pid,
"child", child, "language", detectedType.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/common/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func serviceInfo(pid uint32) svc.ID {
}

name := commName(pid)
lang := exec.FindProcLanguage(int32(pid), nil)
lang := exec.FindProcLanguage(int32(pid), nil, name)
result := svc.ID{Name: name, SDKLanguage: lang, ProcPID: int32(pid)}

activePids.Add(pid, result)
Expand Down
7 changes: 7 additions & 0 deletions pkg/internal/exec/proclang.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ func instrumentableFromSymbolName(symbol string) svc.InstrumentableType {

return svc.InstrumentableGeneric
}

func instrumentableFromPath(path string) svc.InstrumentableType {
if strings.Contains(path, "php") {
return svc.InstrumentablePHP
}
return svc.InstrumentableGeneric
}
7 changes: 6 additions & 1 deletion pkg/internal/exec/proclang_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/beyla/pkg/internal/svc"
)

func FindProcLanguage(pid int32, elfF *elf.File) svc.InstrumentableType {
func FindProcLanguage(pid int32, elfF *elf.File, path string) svc.InstrumentableType {
maps, err := FindLibMaps(pid)

if err != nil {
Expand Down Expand Up @@ -37,6 +37,11 @@ func FindProcLanguage(pid int32, elfF *elf.File) svc.InstrumentableType {
return t
}

t = instrumentableFromPath(path)
if t != svc.InstrumentableGeneric {
return t
}

bytes, err := os.ReadFile(fmt.Sprintf("/proc/%d/environ", pid))
if err != nil {
return svc.InstrumentableGeneric
Expand Down
6 changes: 6 additions & 0 deletions pkg/internal/exec/proclang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ func TestSymbolDetection(t *testing.T) {
assert.Equal(t, svc.InstrumentableGeneric, instrumentableFromSymbolName("graal"))
assert.Equal(t, svc.InstrumentableGeneric, instrumentableFromSymbolName("rust"))
}

func TestEnvironDetection(t *testing.T) {
assert.Equal(t, svc.InstrumentableDotnet, instrumentableFromEnviron("ASPNETCORE_HTTP_PORTS=8080"))
assert.Equal(t, svc.InstrumentableDotnet, instrumentableFromEnviron("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true"))
assert.Equal(t, svc.InstrumentableGeneric, instrumentableFromEnviron("SOME_ENV_VAR=123"))
assert.Equal(t, svc.InstrumentableGeneric, instrumentableFromEnviron("DOT=1"))
}

func TestPathDetection(t *testing.T) {
assert.Equal(t, svc.InstrumentablePHP, instrumentableFromPath("php"))
assert.Equal(t, svc.InstrumentableGeneric, instrumentableFromPath("python"))
}
7 changes: 5 additions & 2 deletions pkg/internal/svc/svc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package svc

import (
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"

attr "github.com/grafana/beyla/pkg/internal/export/attributes/names"
)
Expand All @@ -17,6 +17,7 @@ const (
InstrumentableNodejs
InstrumentableRust
InstrumentableGeneric
InstrumentablePHP
)

func (it InstrumentableType) String() string {
Expand All @@ -34,7 +35,9 @@ func (it InstrumentableType) String() string {
case InstrumentableNodejs:
return semconv.TelemetrySDKLanguageNodejs.Value.AsString()
case InstrumentableRust:
return "rust"
return semconv.TelemetrySDKLanguageRust.Value.AsString()
case InstrumentablePHP:
return semconv.TelemetrySDKLanguagePHP.Value.AsString()
case InstrumentableGeneric:
return "generic"
default:
Expand Down

0 comments on commit 7c41a6e

Please sign in to comment.