github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/installid.go (about) 1 package storage 2 3 import ( 4 "github.com/dgraph-io/badger/v2" 5 "github.com/google/uuid" 6 ) 7 8 const installID = "installID" 9 10 func (s *Storage) InstallID() string { 11 var id []byte 12 err := s.main.View(func(txn *badger.Txn) error { 13 item, err := txn.Get([]byte(installID)) 14 if err != nil { 15 if err == badger.ErrKeyNotFound { 16 return nil 17 } 18 return err 19 } 20 21 err = item.Value(func(val []byte) error { 22 id = append([]byte{}, val...) 23 return nil 24 }) 25 if err != nil { 26 return err 27 } 28 return nil 29 }) 30 if err != nil { 31 return "id-read-error" 32 } 33 34 if id == nil { 35 id = []byte(newID()) 36 err = s.main.Update(func(txn *badger.Txn) error { 37 return txn.SetEntry(badger.NewEntry([]byte(installID), id)) 38 }) 39 if err != nil { 40 return "id-write-error" 41 } 42 } 43 44 return string(id) 45 } 46 47 func newID() string { 48 return uuid.New().String() 49 }