github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/adhoc/writer/datadir_writer.go (about) 1 package writer 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 8 "github.com/pyroscope-io/pyroscope/pkg/fs" 9 "github.com/pyroscope-io/pyroscope/pkg/structs/flamebearer" 10 ) 11 12 type AdhocDataDirWriter struct { 13 dataDir string 14 } 15 16 func NewAdhocDataDirWriter(dataDir string) *AdhocDataDirWriter { 17 return &AdhocDataDirWriter{ 18 dataDir, 19 } 20 } 21 22 // EnsureExist makes sure the ${dataDir} directory exists in the filesystem 23 func (w *AdhocDataDirWriter) EnsureExists() error { 24 return fs.EnsureDirExists(w.dataDir) 25 } 26 27 // Write writes a flamebearer in json format to its dataDir 28 func (w *AdhocDataDirWriter) Write(filename string, flame flamebearer.FlamebearerProfile) (string, error) { 29 path := filepath.Join(w.dataDir, filename) 30 f, err := os.Create(path) 31 if err != nil { 32 return "", err 33 } 34 defer f.Close() 35 36 if err := json.NewEncoder(f).Encode(flame); err != nil { 37 return "", err 38 } 39 40 if err := f.Close(); err != nil { 41 return "", err 42 } 43 44 return path, nil 45 }