Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions internal/processtags/processtags.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ func Reload() {
}
}

// isValidTagValue returns false for values that are not meaningful as process tags.
// This filters out empty strings, filesystem roots, and the "bin" directory.
func isValidTagValue(value string) bool {
switch value {
case "", "/", "\\", "bin":
return false
}
return true
}

func collect() map[string]string {
tags := make(map[string]string)
execPath, err := os.Executable()
Expand All @@ -125,14 +135,19 @@ func collect() map[string]string {
} else {
baseDirName := filepath.Base(filepath.Dir(execPath))
tags[tagEntrypointName] = filepath.Base(execPath)
tags[tagEntrypointBasedir] = baseDirName
if isValidTagValue(baseDirName) {
tags[tagEntrypointBasedir] = baseDirName
}
tags[tagEntrypointType] = entrypointTypeExecutable
}
wd, err := os.Getwd()
if err != nil {
log.Debug("failed to get working directory: %s", err.Error())
} else {
tags[tagEntrypointWorkdir] = filepath.Base(wd)
workdir := filepath.Base(wd)
if isValidTagValue(workdir) {
tags[tagEntrypointWorkdir] = workdir
}
}
return tags
}
Expand Down
24 changes: 24 additions & 0 deletions internal/processtags/processtags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,27 @@ func TestProcessTags(t *testing.T) {
assert.Empty(t, p.Slice())
})
}

func TestIsValidTagValue(t *testing.T) {
tests := []struct {
name string
value string
want bool
}{
{name: "empty string", value: "", want: false},
{name: "forward slash", value: "/", want: false},
{name: "backslash", value: "\\", want: false},
{name: "bin", value: "bin", want: false},
{name: "dot", value: ".", want: true},
{name: "valid app name", value: "myapp", want: true},
{name: "valid usr", value: "usr", want: true},
{name: "valid workspace", value: "workspace", want: true},
{name: "valid hyphenated name", value: "my-service", want: true},
{name: "valid dotted name", value: "app.test", want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, isValidTagValue(tt.value))
})
}
}
Loading