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).
This commit is contained in:
nimrod-up9
2021-06-15 15:51:09 +03:00
committed by GitHub
parent 135b1a5e1e
commit bc3efc6d4c
2 changed files with 21 additions and 11 deletions

View File

@@ -5,9 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/google/martian/har"
"github.com/up9inc/mizu/tap"
"go.mongodb.org/mongo-driver/bson/primitive"
"mizuserver/pkg/database" "mizuserver/pkg/database"
"mizuserver/pkg/models" "mizuserver/pkg/models"
"mizuserver/pkg/resolver" "mizuserver/pkg/resolver"
@@ -16,7 +13,12 @@ import (
"os" "os"
"path" "path"
"sort" "sort"
"strings"
"time" "time"
"github.com/google/martian/har"
"github.com/up9inc/mizu/tap"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
var k8sResolver *resolver.Resolver var k8sResolver *resolver.Resolver
@@ -57,14 +59,21 @@ func startReadingFiles(workingDir string) {
for true { for true {
dir, _ := os.Open(workingDir) dir, _ := os.Open(workingDir)
dirFiles, _ := dir.Readdir(-1) 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") fmt.Printf("Waiting for new files\n")
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
continue continue
} }
fileInfo := dirFiles[0] fileInfo := harFiles[0]
inputFilePath := path.Join(workingDir, fileInfo.Name()) inputFilePath := path.Join(workingDir, fileInfo.Name())
file, err := os.Open(inputFilePath) file, err := os.Open(inputFilePath)
utils.CheckErr(err) utils.CheckErr(err)

View File

@@ -16,7 +16,8 @@ import (
) )
const readPermission = 0644 const readPermission = 0644
const tempFilenamePrefix = "har_writer" const harFilenameSuffix = ".har"
const tempFilenameSuffix = ".har.tmp"
type PairChanItem struct { type PairChanItem struct {
Request *http.Request Request *http.Request
@@ -232,7 +233,7 @@ func (hw *HarWriter) Stop() {
} }
func (hw *HarWriter) openNewFile() { 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) hw.currentFile = openNewHarFile(filename)
} }
@@ -241,15 +242,15 @@ func (hw *HarWriter) closeFile() {
tmpFilename := hw.currentFile.file.Name() tmpFilename := hw.currentFile.file.Name()
hw.currentFile = nil hw.currentFile = nil
filename := buildFilename(hw.OutputDirPath, time.Now()) filename := buildFilename(hw.OutputDirPath, time.Now(), harFilenameSuffix)
err := os.Rename(tmpFilename, filename) err := os.Rename(tmpFilename, filename)
if err != nil { if err != nil {
SilentError("Rename-file", "cannot rename file: %s (%v,%+v)", err, err, err) 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 // (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) return filepath.Join(dir, filename)
} }