From 53bd3b4e582881cd148d9e44fc432201313a5262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 8 Mar 2023 22:17:32 +0100 Subject: [PATCH] config: Add redis-url --- README.md | 2 ++ config.go | 22 +++++++++++++++++++--- config_test.go | 4 +++- main.go | 3 +-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6c41925..3240451 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ cache { cleanup-interval 1h # Redis cache settings + # URL to connect to redis (default: redis://localhost:6379) + redis-url redis://user:password@localhost:6789/3 } ``` diff --git a/config.go b/config.go index ab8bce6..9bb10fb 100644 --- a/config.go +++ b/config.go @@ -56,9 +56,13 @@ type labelConfig struct { } type cacheConfig struct { - Type cacheType + // shared settings + Type cacheType + Duration time.Duration + // memory settings CleanupInterval time.Duration - Duration time.Duration + // redis settings + RedisURL string } type alertmanagerConfig struct { @@ -86,8 +90,11 @@ func readConfig(path string) (*config, error) { config.alertMode = single config.cache.Type = memory - config.cache.CleanupInterval = time.Hour config.cache.Duration = time.Hour * 24 + // memory + config.cache.CleanupInterval = time.Hour + // redis + config.cache.RedisURL = "redis://localhost:6379" d := cfg.Get("log-level") if d != nil { @@ -279,6 +286,7 @@ func readConfig(path string) (*config, error) { config.cache.Duration = duration } + // memory var cleanupIntervalString string d = cacheDir.Children.Get("cleanup-interval") if d != nil { @@ -293,6 +301,14 @@ func readConfig(path string) (*config, error) { 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") diff --git a/config_test.go b/config_test.go index c0e3e6e..e5a7ad3 100644 --- a/config_test.go +++ b/config_test.go @@ -56,6 +56,7 @@ alertmanager { cache { type redis duration 48h + redis-url redis://user:password@localhost:6789/3 } ` @@ -76,8 +77,9 @@ cache { }, cache: cacheConfig{ Type: redis, - CleanupInterval: time.Hour, Duration: 48 * time.Hour, + CleanupInterval: time.Hour, + RedisURL: "redis://user:password@localhost:6789/3", }, am: alertmanagerConfig{ SilenceDuration: time.Hour * 24, diff --git a/main.go b/main.go index 69ddf74..57f0577 100644 --- a/main.go +++ b/main.go @@ -407,8 +407,7 @@ func main() { c = cache.NewMemoryCache(cfg.cache.Duration) case redis: var err error - // TODO: Read URL from config - c, err = cache.NewRedisCache("redis://localhost:6379", cfg.cache.Duration) + c, err = cache.NewRedisCache(cfg.cache.RedisURL, cfg.cache.Duration) if err != nil { logger.Fatalf("Failed to create redis cache: %v", err) }