config: Add redis-url

This commit is contained in:
Thorben Günther 2023-03-08 22:17:32 +01:00
parent 054c163ffb
commit 53bd3b4e58
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
4 changed files with 25 additions and 6 deletions

View file

@ -106,6 +106,8 @@ cache {
cleanup-interval 1h cleanup-interval 1h
# Redis cache settings # Redis cache settings
# URL to connect to redis (default: redis://localhost:6379)
redis-url redis://user:password@localhost:6789/3
} }
``` ```

View file

@ -56,9 +56,13 @@ type labelConfig struct {
} }
type cacheConfig struct { type cacheConfig struct {
// shared settings
Type cacheType Type cacheType
CleanupInterval time.Duration
Duration time.Duration Duration time.Duration
// memory settings
CleanupInterval time.Duration
// redis settings
RedisURL string
} }
type alertmanagerConfig struct { type alertmanagerConfig struct {
@ -86,8 +90,11 @@ func readConfig(path string) (*config, error) {
config.alertMode = single config.alertMode = single
config.cache.Type = memory config.cache.Type = memory
config.cache.CleanupInterval = time.Hour
config.cache.Duration = time.Hour * 24 config.cache.Duration = time.Hour * 24
// memory
config.cache.CleanupInterval = time.Hour
// redis
config.cache.RedisURL = "redis://localhost:6379"
d := cfg.Get("log-level") d := cfg.Get("log-level")
if d != nil { if d != nil {
@ -279,6 +286,7 @@ func readConfig(path string) (*config, error) {
config.cache.Duration = duration config.cache.Duration = duration
} }
// memory
var cleanupIntervalString string var cleanupIntervalString string
d = cacheDir.Children.Get("cleanup-interval") d = cacheDir.Children.Get("cleanup-interval")
if d != nil { if d != nil {
@ -293,6 +301,14 @@ func readConfig(path string) (*config, error) {
config.cache.CleanupInterval = interval config.cache.CleanupInterval = interval
} }
// redis
d = cacheDir.Children.Get("redis-url")
if d != nil {
if err := d.ParseParams(&config.cache.RedisURL); err != nil {
return nil, err
}
}
} }
amDir := cfg.Get("alertmanager") amDir := cfg.Get("alertmanager")

View file

@ -56,6 +56,7 @@ alertmanager {
cache { cache {
type redis type redis
duration 48h duration 48h
redis-url redis://user:password@localhost:6789/3
} }
` `
@ -76,8 +77,9 @@ cache {
}, },
cache: cacheConfig{ cache: cacheConfig{
Type: redis, Type: redis,
CleanupInterval: time.Hour,
Duration: 48 * time.Hour, Duration: 48 * time.Hour,
CleanupInterval: time.Hour,
RedisURL: "redis://user:password@localhost:6789/3",
}, },
am: alertmanagerConfig{ am: alertmanagerConfig{
SilenceDuration: time.Hour * 24, SilenceDuration: time.Hour * 24,

View file

@ -407,8 +407,7 @@ func main() {
c = cache.NewMemoryCache(cfg.cache.Duration) c = cache.NewMemoryCache(cfg.cache.Duration)
case redis: case redis:
var err error var err error
// TODO: Read URL from config c, err = cache.NewRedisCache(cfg.cache.RedisURL, cfg.cache.Duration)
c, err = cache.NewRedisCache("redis://localhost:6379", cfg.cache.Duration)
if err != nil { if err != nil {
logger.Fatalf("Failed to create redis cache: %v", err) logger.Fatalf("Failed to create redis cache: %v", err)
} }