feat: implement log-rotation for local sqlite storage
This commit is contained in:
parent
4d3782902a
commit
1f07632ae2
5 changed files with 322 additions and 28 deletions
57
config.go
57
config.go
|
|
@ -57,8 +57,9 @@ type ElasticsearchConfig struct {
|
|||
}
|
||||
|
||||
type LocalStorage struct {
|
||||
Enable bool `mapstructure:"enabled"`
|
||||
DBPath string `mapstructure:"db_path"`
|
||||
Enable bool `mapstructure:"enabled"`
|
||||
DBPath string `mapstructure:"db_path"`
|
||||
RotationConfig StorageRotationConfig `mapstructure:"rotation"`
|
||||
}
|
||||
|
||||
type SystemMetrics struct {
|
||||
|
|
@ -97,6 +98,33 @@ type Config struct {
|
|||
} `mapstructure:"logging"`
|
||||
}
|
||||
|
||||
type StorageRotationConfig struct {
|
||||
// MaxSizeBytes is the maximum size of the database in bytes (0 = deactivated)
|
||||
MaxSizeBytes int64 `mapstructure:"max_size_bytes"`
|
||||
// MaxAgeHours is the maximum age of the database (0 = deaactivated)
|
||||
MaxAgeHours time.Duration `mapstructure:"max_age_hours"`
|
||||
// MaxFiles is the maximum count of old files, to keep
|
||||
MaxFiles int `mapstructure:"max_files"`
|
||||
// CheckIntervalMinutes is the intervall for checking rotation conditions
|
||||
CheckIntervalMinutes time.Duration `mapstructure:"check_interval_minutes"`
|
||||
// ArchiveDir is the dir to store archived files (empty = same dir as db)
|
||||
ArchiveDir string `mapstructure:"archive_dir"`
|
||||
}
|
||||
|
||||
func (src StorageRotationConfig) GetMaxAge() time.Duration {
|
||||
if src.MaxAgeHours <= 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(src.MaxAgeHours) * time.Hour
|
||||
}
|
||||
|
||||
func (src StorageRotationConfig) GetCheckInterval() time.Duration {
|
||||
if src.CheckIntervalMinutes <= 0 {
|
||||
return 5 * time.Minute
|
||||
}
|
||||
return time.Duration(src.CheckIntervalMinutes) * time.Minute
|
||||
}
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(".")
|
||||
|
|
@ -157,6 +185,11 @@ func setConfigDefaultsV2() {
|
|||
viper.SetDefault("export.health_check_interval", "60s")
|
||||
viper.SetDefault("localstorage.enabled", true)
|
||||
viper.SetDefault("localstorage.db_path", "./tixel_watch.db")
|
||||
viper.SetDefault("localstorage.rotation.max_size_bytes", int64(100*1024*1024))
|
||||
viper.SetDefault("localstorage.rotation.max_age_hours", 24)
|
||||
viper.SetDefault("localstorage.rotation.max_files", 7)
|
||||
viper.SetDefault("localstorage.rotation.check_interval_minutes", 5)
|
||||
viper.SetDefault("localstorage.rotation.archive_dir", "")
|
||||
}
|
||||
|
||||
func validateConfig(cfg *Config) error {
|
||||
|
|
@ -253,5 +286,25 @@ func validateConfigV2(cfg *Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
if cfg.LocalStorage.RotationConfig.MaxSizeBytes < 0 {
|
||||
slog.Warn("Invalid rotation max_size_bytes, setting to 100MB", "value", cfg.LocalStorage.RotationConfig.MaxSizeBytes)
|
||||
cfg.LocalStorage.RotationConfig.MaxSizeBytes = 100 * 1024 * 1024
|
||||
}
|
||||
|
||||
if cfg.LocalStorage.RotationConfig.MaxAgeHours < 0 {
|
||||
slog.Warn("Invalid rotation max_age_hours, setting to 24", "value", cfg.LocalStorage.RotationConfig.MaxAgeHours)
|
||||
cfg.LocalStorage.RotationConfig.MaxAgeHours = 24
|
||||
}
|
||||
|
||||
if cfg.LocalStorage.RotationConfig.MaxFiles < 0 {
|
||||
slog.Warn("Invalid rotation max_files, setting to 7", "value", cfg.LocalStorage.RotationConfig.MaxFiles)
|
||||
cfg.LocalStorage.RotationConfig.MaxFiles = 7
|
||||
}
|
||||
|
||||
if cfg.LocalStorage.RotationConfig.CheckIntervalMinutes < 1 {
|
||||
slog.Warn("Invalid rotation check_interval_minutes, setting to 5", "value", cfg.LocalStorage.RotationConfig.CheckIntervalMinutes)
|
||||
cfg.LocalStorage.RotationConfig.CheckIntervalMinutes = 5
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue