From bc3efc6d4cac3a2fa5b38466364f6a977e2ef878 Mon Sep 17 00:00:00 2001 From: nimrod-up9 <59927337+nimrod-up9@users.noreply.github.com> Date: Tue, 15 Jun 2021 15:51:09 +0300 Subject: [PATCH] TRA-3342 Mizu/tap dump to har directory fails on Linux (#71) * Instead of saving incomplete temp har files in a temp dir, save them in the output dir with a *.har.tmp suffix. * API only loads har from *.har files (by extension). --- api/pkg/api/main.go | 21 +++++++++++++++------ tap/har_writer.go | 11 ++++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/api/pkg/api/main.go b/api/pkg/api/main.go index 2a1e550d4..0907b0146 100644 --- a/api/pkg/api/main.go +++ b/api/pkg/api/main.go @@ -5,9 +5,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/google/martian/har" - "github.com/up9inc/mizu/tap" - "go.mongodb.org/mongo-driver/bson/primitive" "mizuserver/pkg/database" "mizuserver/pkg/models" "mizuserver/pkg/resolver" @@ -16,7 +13,12 @@ import ( "os" "path" "sort" + "strings" "time" + + "github.com/google/martian/har" + "github.com/up9inc/mizu/tap" + "go.mongodb.org/mongo-driver/bson/primitive" ) var k8sResolver *resolver.Resolver @@ -57,14 +59,21 @@ func startReadingFiles(workingDir string) { for true { dir, _ := os.Open(workingDir) dirFiles, _ := dir.Readdir(-1) - sort.Sort(utils.ByModTime(dirFiles)) - if len(dirFiles) == 0 { + var harFiles []os.FileInfo + for _, fileInfo := range dirFiles { + if strings.HasSuffix(fileInfo.Name(), ".har") { + harFiles = append(harFiles, fileInfo) + } + } + sort.Sort(utils.ByModTime(harFiles)) + + if len(harFiles) == 0 { fmt.Printf("Waiting for new files\n") time.Sleep(3 * time.Second) continue } - fileInfo := dirFiles[0] + fileInfo := harFiles[0] inputFilePath := path.Join(workingDir, fileInfo.Name()) file, err := os.Open(inputFilePath) utils.CheckErr(err) diff --git a/tap/har_writer.go b/tap/har_writer.go index 1bd2b5ee5..3126ee4c0 100644 --- a/tap/har_writer.go +++ b/tap/har_writer.go @@ -16,7 +16,8 @@ import ( ) const readPermission = 0644 -const tempFilenamePrefix = "har_writer" +const harFilenameSuffix = ".har" +const tempFilenameSuffix = ".har.tmp" type PairChanItem struct { Request *http.Request @@ -232,7 +233,7 @@ func (hw *HarWriter) Stop() { } func (hw *HarWriter) openNewFile() { - filename := filepath.Join(os.TempDir(), fmt.Sprintf("%s_%d", tempFilenamePrefix, time.Now().UnixNano())) + filename := buildFilename(hw.OutputDirPath, time.Now(), tempFilenameSuffix) hw.currentFile = openNewHarFile(filename) } @@ -241,15 +242,15 @@ func (hw *HarWriter) closeFile() { tmpFilename := hw.currentFile.file.Name() hw.currentFile = nil - filename := buildFilename(hw.OutputDirPath, time.Now()) + filename := buildFilename(hw.OutputDirPath, time.Now(), harFilenameSuffix) err := os.Rename(tmpFilename, filename) if err != nil { SilentError("Rename-file", "cannot rename file: %s (%v,%+v)", err, err, err) } } -func buildFilename(dir string, t time.Time) string { +func buildFilename(dir string, t time.Time, suffix string) string { // (epoch time in nanoseconds)__(YYYY_Month_DD__hh-mm-ss).har - filename := fmt.Sprintf("%d__%s.har", t.UnixNano(), t.Format("2006_Jan_02__15-04-05")) + filename := fmt.Sprintf("%d__%s%s", t.UnixNano(), t.Format("2006_Jan_02__15-04-05"), suffix) return filepath.Join(dir, filename) }