Skip to content
Merged
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
14 changes: 12 additions & 2 deletions comp/core/flare/helpers/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"regexp"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/DataDog/datadog-agent/comp/core/flare/types"
Expand All @@ -31,6 +32,8 @@ const (
filePerm = 0644
)

var archiveNameCounter atomic.Uint64

func newBuilder(root string, hostname string, localFlare bool, flareArgs types.FlareArgs) (*builder, error) {
fb := &builder{
tmpDir: root,
Expand Down Expand Up @@ -144,7 +147,10 @@ type builder struct {
}

func getArchiveName() string {
t := time.Now().UTC()
return getArchiveNameForTime(time.Now().UTC(), newArchiveNameID())
}

func getArchiveNameForTime(t time.Time, uniqueSuffix string) string {
timeString := strings.ReplaceAll(t.Format(time.RFC3339), ":", "-")

logLevel, err := log.GetLogLevel()
Expand All @@ -153,7 +159,11 @@ func getArchiveName() string {
logLevelString = "-" + logLevel.String()
}

return fmt.Sprintf("datadog-agent-%s%s.zip", timeString, logLevelString)
return fmt.Sprintf("datadog-agent-%s-%s%s.zip", timeString, uniqueSuffix, logLevelString)
}

func newArchiveNameID() string {
return fmt.Sprintf("%d-%d", os.Getpid(), archiveNameCounter.Add(1))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is production code, os.Getpid is a safe os wrapper with no error code, no failure mode

}

func (fb *builder) Save() (string, error) {
Expand Down
13 changes: 13 additions & 0 deletions comp/core/flare/helpers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ func TestSave(t *testing.T) {
}
}

func TestGetArchiveNameIsUniqueWithinSameSecond(t *testing.T) {
timestamp := time.Date(2026, time.August, 14, 12, 0, 0, 0, time.UTC)
names := make(map[string]struct{})

for range 100 {
name := getArchiveNameForTime(timestamp, newArchiveNameID())
if _, found := names[name]; found {
t.Fatalf("archive name %q was generated more than once", name)
}
names[name] = struct{}{}
}
}

func TestAddFileFromFunc(t *testing.T) {
fb := getNewBuilder(t)
defer fb.clean()
Expand Down
Loading