Skip to content

Commit 0412662

Browse files
authored
Detect running Java process after update (#1936)
1 parent c54dfe4 commit 0412662

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

internal/detector/kafkaclient/kafkaclient.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ package kafkaclient
66
import (
77
"context"
88
"log/slog"
9-
"path/filepath"
109
"strings"
1110

1211
"github.com/aws/amazon-cloudwatch-agent/internal/detector"
1312
"github.com/aws/amazon-cloudwatch-agent/internal/detector/common"
13+
"github.com/aws/amazon-cloudwatch-agent/internal/detector/util"
1414
)
1515

1616
const (
@@ -80,11 +80,11 @@ func (d *loadedJarDetector) Detect(ctx context.Context, process detector.Process
8080
return nil, err
8181
}
8282
for _, fd := range fds {
83-
path := strings.TrimSuffix(fd.Path, " (deleted)")
84-
if !strings.HasSuffix(path, common.ExtJAR) {
83+
base := util.BaseName(fd.Path)
84+
if !strings.HasSuffix(base, common.ExtJAR) {
8585
continue
8686
}
87-
if strings.Contains(filepath.Base(path), kafkaClientsLibraryName) {
87+
if strings.Contains(base, kafkaClientsLibraryName) {
8888
return &detector.Metadata{Categories: []detector.Category{detector.CategoryKafkaClient}}, nil
8989
}
9090
}

internal/detector/util/util.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,28 @@ import (
1111
"github.com/aws/amazon-cloudwatch-agent/internal/detector"
1212
)
1313

14+
const (
15+
suffixDeleted = " (deleted)"
16+
)
17+
18+
// BaseName returns the base filename from a path. Removes quotes and deleted file indicators. Preserves file
19+
// extensions and casing.
20+
func BaseName(path string) string {
21+
trimmed := strings.Trim(path, "\"")
22+
if trimmed == "" {
23+
return ""
24+
}
25+
return strings.TrimSuffix(filepath.Base(trimmed), suffixDeleted)
26+
}
27+
28+
// BaseExe returns the executable name from a path. Normalizes the name by removing file extensions and converting to
29+
// lowercase to support cross-platform comparisons.
1430
func BaseExe(exe string) string {
15-
base := strings.Trim(exe, "\"")
16-
if base != "" {
17-
base = filepath.Base(base)
31+
base := BaseName(exe)
32+
if base == "" {
33+
return ""
1834
}
19-
base = strings.TrimSuffix(base, filepath.Ext(base))
20-
return strings.ToLower(base)
35+
return strings.ToLower(strings.TrimSuffix(base, filepath.Ext(base)))
2136
}
2237

2338
func AbsPath(ctx context.Context, process detector.Process, path string) (string, error) {

internal/detector/util/util_test.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,60 @@ import (
1515
"github.com/aws/amazon-cloudwatch-agent/internal/detector/detectortest"
1616
)
1717

18+
func TestBaseName(t *testing.T) {
19+
testCases := map[string]struct {
20+
path string
21+
want string
22+
}{
23+
"WithEmptyPath": {
24+
path: "",
25+
want: "",
26+
},
27+
"WithSimplePath": {
28+
path: filepath.Join("usr", "bin", "test.jar"),
29+
want: "test.jar",
30+
},
31+
"WithQuotedPath": {
32+
path: fmt.Sprintf("%q", filepath.Join("usr", "bin", "TEST.jar")),
33+
want: "TEST.jar",
34+
},
35+
"WithDeleted": {
36+
path: filepath.Join("usr", "bin", "test.jar (deleted)"),
37+
want: "test.jar",
38+
},
39+
}
40+
41+
for name, testCase := range testCases {
42+
t.Run(name, func(t *testing.T) {
43+
got := BaseName(testCase.path)
44+
assert.Equal(t, testCase.want, got)
45+
})
46+
}
47+
}
48+
1849
func TestBaseExe(t *testing.T) {
1950
testCases := map[string]struct {
2051
exe string
2152
want string
2253
}{
23-
"EmptyString": {
54+
"WithEmptyPath": {
2455
exe: "",
2556
want: "",
2657
},
27-
"SimpleExe": {
58+
"WithSimplePath": {
2859
exe: filepath.Join("usr", "bin", "java"),
2960
want: "java",
3061
},
31-
"QuotedPath": {
32-
exe: fmt.Sprintf("%q", filepath.Join("usr", "bin", "java")),
62+
"WithQuotedPath": {
63+
exe: fmt.Sprintf("%q", filepath.Join("usr", "bin", "Java")),
64+
want: "java",
65+
},
66+
"WithExtension": {
67+
exe: filepath.Join("usr", "bin", "java.exe"),
68+
want: "java",
69+
},
70+
"WithDeleted": {
71+
exe: filepath.Join("usr", "bin", "java.exe (deleted)"),
3372
want: "java",
3473
},
3574
}

0 commit comments

Comments
 (0)