Update JSON nesting, fix file permissions

Signed-off-by: Mustafain Ali Khan <mustalik@amazon.com>
This commit is contained in:
Mustafain Ali Khan 2024-12-10 09:44:57 -08:00
parent 60c2a5b448
commit 0736877480

View file

@ -11,22 +11,24 @@ import (
// FileStore implements the AlertStore interface. // FileStore implements the AlertStore interface.
type FileStore struct { type FileStore struct {
logger *slog.Logger
alertsByRule map[uint64][]*Alert alertsByRule map[uint64][]*Alert
logger *slog.Logger
// protects the `alertsByRule` map. // protects the `alertsByRule` map.
stateMtx sync.RWMutex stateMtx sync.RWMutex
path string path string
registerer prometheus.Registerer
storeInitErrors prometheus.Counter storeInitErrors prometheus.Counter
alertStoreErrors *prometheus.CounterVec alertStoreErrors *prometheus.CounterVec
} }
type FileData struct {
Alerts map[uint64][]*Alert `json:"alerts"`
}
func NewFileStore(l *slog.Logger, storagePath string, registerer prometheus.Registerer) *FileStore { func NewFileStore(l *slog.Logger, storagePath string, registerer prometheus.Registerer) *FileStore {
s := &FileStore{ s := &FileStore{
logger: l, logger: l,
alertsByRule: make(map[uint64][]*Alert), alertsByRule: make(map[uint64][]*Alert),
path: storagePath, path: storagePath,
registerer: registerer,
} }
s.storeInitErrors = prometheus.NewCounter( s.storeInitErrors = prometheus.NewCounter(
prometheus.CounterOpts{ prometheus.CounterOpts{
@ -43,16 +45,16 @@ func NewFileStore(l *slog.Logger, storagePath string, registerer prometheus.Regi
}, },
[]string{"rule_group"}, []string{"rule_group"},
) )
s.initState() s.initState(registerer)
return s return s
} }
// initState reads the state from file storage into the alertsByRule map. // initState reads the state from file storage into the alertsByRule map.
func (s *FileStore) initState() { func (s *FileStore) initState(registerer prometheus.Registerer) {
if s.registerer != nil { if registerer != nil {
s.registerer.MustRegister(s.alertStoreErrors, s.storeInitErrors) registerer.MustRegister(s.alertStoreErrors, s.storeInitErrors)
} }
file, err := os.OpenFile(s.path, os.O_RDWR|os.O_CREATE, 0o666) file, err := os.OpenFile(s.path, os.O_RDWR|os.O_CREATE, 0o644)
if err != nil { if err != nil {
s.logger.Error("Failed reading alerts state from file", "err", err) s.logger.Error("Failed reading alerts state from file", "err", err)
s.storeInitErrors.Inc() s.storeInitErrors.Inc()
@ -60,14 +62,16 @@ func (s *FileStore) initState() {
} }
defer file.Close() defer file.Close()
var alertsByRule map[uint64][]*Alert var data *FileData
err = json.NewDecoder(file).Decode(&alertsByRule) err = json.NewDecoder(file).Decode(&data)
if err != nil { if err != nil {
data = nil
s.logger.Error("Failed reading alerts state from file", "err", err) s.logger.Error("Failed reading alerts state from file", "err", err)
s.storeInitErrors.Inc() s.storeInitErrors.Inc()
} }
if alertsByRule == nil { alertsByRule := make(map[uint64][]*Alert)
alertsByRule = make(map[uint64][]*Alert) if data != nil && data.Alerts != nil {
alertsByRule = data.Alerts
} }
s.alertsByRule = alertsByRule s.alertsByRule = alertsByRule
} }
@ -101,6 +105,8 @@ func (s *FileStore) SetAlerts(key uint64, groupKey string, alerts []*Alert) erro
// Update in memory // Update in memory
if alerts != nil { if alerts != nil {
s.alertsByRule[key] = alerts s.alertsByRule[key] = alerts
} else {
return nil
} }
// flush in memory state to file storage // flush in memory state to file storage
file, err := os.Create(s.path) file, err := os.Create(s.path)
@ -111,7 +117,10 @@ func (s *FileStore) SetAlerts(key uint64, groupKey string, alerts []*Alert) erro
defer file.Close() defer file.Close()
encoder := json.NewEncoder(file) encoder := json.NewEncoder(file)
err = encoder.Encode(s.alertsByRule) data := FileData{
Alerts: s.alertsByRule,
}
err = encoder.Encode(data)
if err != nil { if err != nil {
s.alertStoreErrors.WithLabelValues(groupKey).Inc() s.alertStoreErrors.WithLabelValues(groupKey).Inc()
return err return err