Skip to content

Commit 9cc3795

Browse files
discovery: Improve Erlang service name heuristics (#42406)
### What does this PR do? [DSCVR-237](https://datadoghq.atlassian.net/browse/DSCVR-237) This PR improves the Erlang service name which was implemented in other [PR](#42322). The changes are the following: - Removed concatenated flag support (e.g., -prognameerl). - Return empty string: Changed detectErlangAppName() to return "" instead of "beam" when no name can be extracted. - Added 4 test cases in service_test.go to verify detector is properly hooked up (CouchDB, RabbitMQ, Riak, fallback) - Code cleanup: Removed invalid tests, simplified logic. ### Motivation Improve the heuristics that have already been implemented. ### Describe how you validated your changes Testing locally. ### Additional Notes [DSCVR-237]: https://datadoghq.atlassian.net/browse/DSCVR-237?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Co-authored-by: marta.vicentenavarro <[email protected]>
1 parent c70afaa commit 9cc3795

File tree

3 files changed

+83
-53
lines changed

3 files changed

+83
-53
lines changed

pkg/collector/corechecks/servicediscovery/usm/erlang.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newErlangDetector(ctx DetectionContext) detector {
2020

2121
func (e erlangDetector) detect(args []string) (ServiceMetadata, bool) {
2222
name := detectErlangAppName(args)
23-
if name != "" && name != "beam" {
23+
if name != "" {
2424
return NewServiceMetadata(name, CommandLine), true
2525
}
2626
return ServiceMetadata{}, false
@@ -31,7 +31,7 @@ func detectErlangAppName(cmdline []string) string {
3131
var home string
3232

3333
// Parse command line looking for -progname and -home flags.
34-
// We support both separated (-progname erl) and concatenated (-prognameerl) forms.
34+
// Erlang uses space-separated flag values (e.g., -progname erl).
3535
for i := 0; i < len(cmdline); i++ {
3636
arg := cmdline[i]
3737

@@ -42,41 +42,29 @@ func detectErlangAppName(cmdline []string) string {
4242
continue
4343
}
4444

45-
// Check for -progname with concatenated value (e.g., -prognameerl)
46-
if strings.HasPrefix(arg, "-progname") && len(arg) > 9 {
47-
progname = strings.TrimSpace(arg[9:])
48-
continue
49-
}
50-
5145
// Check for -home flag (with value in next arg)
5246
if arg == "-home" && i+1 < len(cmdline) {
5347
home = strings.TrimSpace(cmdline[i+1])
5448
i++
5549
continue
5650
}
57-
58-
// Check for -home with concatenated value (e.g., -home/var/lib/rabbitmq)
59-
if strings.HasPrefix(arg, "-home") && len(arg) > 5 {
60-
home = strings.TrimSpace(arg[5:])
61-
continue
62-
}
6351
}
6452

6553
// Apply heuristics according to requirements.
66-
// Compare progname case-insensitively to handle edge cases.
67-
if progname != "" && !strings.EqualFold(progname, "erl") {
54+
// Use exact comparison for progname
55+
if progname != "" && progname != "erl" {
6856
return progname
6957
}
7058

7159
// Only use home if progname is explicitly "erl"
72-
if strings.EqualFold(progname, "erl") && home != "" {
60+
if progname == "erl" && home != "" {
7361
// Extract the last component of the home path
7462
base := filepath.Base(home)
7563
if base != "" && base != "." && base != "/" {
7664
return base
7765
}
7866
}
7967

80-
// Fallback to "beam"
81-
return "beam"
68+
// Return empty string to indicate we couldn't extract a name
69+
return ""
8270
}

pkg/collector/corechecks/servicediscovery/usm/erlang_test.go

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func Test_detectErlangAppName(t *testing.T) {
6464
expected: "myapp",
6565
},
6666
{
67-
name: "Fallback to beam when no progname or home",
67+
name: "No progname or home returns empty string",
6868
cmdline: []string{
6969
"-smp", "auto",
7070
"-noinput",
7171
},
72-
expected: "beam",
72+
expected: "",
7373
},
7474
{
7575
name: "Progname without home",
@@ -82,15 +82,15 @@ func Test_detectErlangAppName(t *testing.T) {
8282
{
8383
name: "Empty command line",
8484
cmdline: []string{},
85-
expected: "beam",
85+
expected: "",
8686
},
8787
{
8888
name: "Only home, no progname",
8989
cmdline: []string{
9090
"-home", "/opt/customapp",
9191
"-noshell",
9292
},
93-
expected: "beam",
93+
expected: "",
9494
},
9595
{
9696
name: "Home with trailing slash",
@@ -156,31 +156,6 @@ func Test_detectErlangAppName(t *testing.T) {
156156
},
157157
expected: "couchdb",
158158
},
159-
{
160-
name: "Progname concatenated without space",
161-
cmdline: []string{
162-
"-root", "/usr/lib/erlang",
163-
"-prognamecouchdb",
164-
"-home", "/opt/couchdb",
165-
},
166-
expected: "couchdb",
167-
},
168-
{
169-
name: "Home concatenated without space",
170-
cmdline: []string{
171-
"-progname", "erl",
172-
"-home/var/lib/rabbitmq",
173-
},
174-
expected: "rabbitmq",
175-
},
176-
{
177-
name: "Both flags concatenated",
178-
cmdline: []string{
179-
"-prognameerl",
180-
"-home/var/lib/ejabberd",
181-
},
182-
expected: "ejabberd",
183-
},
184159
{
185160
name: "Progname with spaces in value",
186161
cmdline: []string{
@@ -197,20 +172,38 @@ func Test_detectErlangAppName(t *testing.T) {
197172
expected: "rabbitmq",
198173
},
199174
{
200-
name: "Case insensitive erl - uppercase ERL",
175+
name: "Uppercase ERL is not treated as erl",
201176
cmdline: []string{
202177
"-progname", "ERL",
203178
"-home", "/var/lib/rabbitmq",
204179
},
205-
expected: "rabbitmq",
180+
expected: "ERL",
206181
},
207182
{
208-
name: "Case insensitive erl - mixed case Erl",
183+
name: "Mixed case Erl is not treated as erl",
209184
cmdline: []string{
210185
"-progname", "Erl",
211186
"-home", "/var/lib/ejabberd",
212187
},
213-
expected: "ejabberd",
188+
expected: "Erl",
189+
},
190+
{
191+
name: "progname as last argument without value",
192+
cmdline: []string{
193+
"-root", "/usr/lib/erlang",
194+
"-home", "/var/lib/myapp",
195+
"-progname",
196+
},
197+
expected: "",
198+
},
199+
{
200+
name: "home as last argument without value",
201+
cmdline: []string{
202+
"-root", "/usr/lib/erlang",
203+
"-progname", "erl",
204+
"-home",
205+
},
206+
expected: "",
214207
},
215208
}
216209

@@ -251,7 +244,7 @@ func Test_erlangDetector(t *testing.T) {
251244
expectedSource: CommandLine,
252245
},
253246
{
254-
name: "Fallback to beam returns false",
247+
name: "No name extracted returns false",
255248
args: []string{
256249
"-smp", "auto",
257250
},

pkg/collector/corechecks/servicediscovery/usm/service_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,55 @@ func TestExtractServiceMetadata(t *testing.T) {
607607
expectedGeneratedName: "dotnet",
608608
expectedGeneratedNameSource: CommandLine,
609609
},
610+
{
611+
name: "Erlang beam.smp with CouchDB progname",
612+
cmdline: []string{
613+
"/usr/lib/erlang/erts-12.3/bin/beam.smp",
614+
"--",
615+
"-root", "/usr/lib/erlang",
616+
"-progname", "couchdb",
617+
"-home", "/opt/couchdb",
618+
},
619+
expectedGeneratedName: "couchdb",
620+
expectedGeneratedNameSource: CommandLine,
621+
},
622+
{
623+
name: "Erlang beam with RabbitMQ (erl progname, use home)",
624+
cmdline: []string{
625+
"/usr/lib/erlang/erts-11.2/bin/beam",
626+
"--",
627+
"-W", "w",
628+
"-K", "true",
629+
"-A", "192",
630+
"-progname", "erl",
631+
"-home", "/var/lib/rabbitmq",
632+
},
633+
expectedGeneratedName: "rabbitmq",
634+
expectedGeneratedNameSource: CommandLine,
635+
},
636+
{
637+
name: "Erlang beam.smp with Riak progname",
638+
cmdline: []string{
639+
"beam.smp",
640+
"--",
641+
"-root", "/usr/lib/erlang",
642+
"-progname", "riak",
643+
"-home", "/var/lib/riak",
644+
},
645+
expectedGeneratedName: "riak",
646+
expectedGeneratedNameSource: CommandLine,
647+
},
648+
{
649+
name: "Erlang beam with no useful name",
650+
cmdline: []string{
651+
"beam",
652+
"--",
653+
"-smp", "auto",
654+
"-noinput",
655+
},
656+
expectedGeneratedName: "beam",
657+
expectedGeneratedNameSource: CommandLine,
658+
},
610659
{
611660
name: "PHP Laravel",
612661
cmdline: []string{

0 commit comments

Comments
 (0)