diff --git a/dashboard/api/router.go b/dashboard/api/router.go index e12b0f6..71ba453 100644 --- a/dashboard/api/router.go +++ b/dashboard/api/router.go @@ -3,10 +3,10 @@ package api import ( "net/http" - "os" "strconv" "time" + "git.ardenone.com/jedarden/zai-proxy/dashboard/config" "git.ardenone.com/jedarden/zai-proxy/dashboard/model" "git.ardenone.com/jedarden/zai-proxy/dashboard/storage" ) @@ -27,55 +27,13 @@ type Config struct { // DefaultConfig returns the default API configuration. func DefaultConfig() *Config { - addr := os.Getenv("LISTEN_ADDR") - if addr == "" { - addr = ":8080" - } - - interval := 5 * time.Second - if v := os.Getenv("SCRAPE_INTERVAL"); v != "" { - if d, err := time.ParseDuration(v); err == nil { - interval = d - } - } - - targets := []string{"http://zai-proxy.mcp.svc.cluster.local:8080/metrics"} - if v := os.Getenv("SCRAPE_TARGETS"); v != "" { - // Simple split by comma - for i, t := range splitTargets(v) { - if i == 0 { - targets = nil - } - targets = append(targets, t) - } - } - return &Config{ - ListenAddr: addr, - ScrapeInterval: interval, - ScrapeTargets: targets, + ListenAddr: config.GetListenAddr(), + ScrapeInterval: config.GetScrapeInterval(), + ScrapeTargets: config.GetScrapeTargets(), } } -func splitTargets(s string) []string { - var result []string - var current string - for _, c := range s { - if c == ',' { - if current != "" { - result = append(result, current) - current = "" - } - } else { - current += string(c) - } - } - if current != "" { - result = append(result, current) - } - return result -} - // NewRouter creates a new Router. func NewRouter(hub *SSEHub, store *storage.Storage, config *Config) *Router { return &Router{ diff --git a/dashboard/collector/collector.go b/dashboard/collector/collector.go index 36354fb..3428724 100644 --- a/dashboard/collector/collector.go +++ b/dashboard/collector/collector.go @@ -6,12 +6,12 @@ import ( "fmt" "log" "net/http" - "os" "strconv" "strings" "sync" "time" + "git.ardenone.com/jedarden/zai-proxy/dashboard/config" "git.ardenone.com/jedarden/zai-proxy/dashboard/model" ) @@ -38,29 +38,10 @@ type Config struct { // DefaultConfig returns the default configuration. func DefaultConfig() Config { - targets := os.Getenv("SCRAPE_TARGETS") - if targets == "" { - targets = "http://zai-proxy.mcp.svc.cluster.local:8080/metrics" - } - - interval := 5 * time.Second - if v := os.Getenv("SCRAPE_INTERVAL"); v != "" { - if d, err := time.ParseDuration(v); err == nil { - interval = d - } - } - - timeout := 3 * time.Second - if v := os.Getenv("SCRAPE_TIMEOUT"); v != "" { - if d, err := time.ParseDuration(v); err == nil { - timeout = d - } - } - return Config{ - Targets: strings.Split(targets, ","), - Interval: interval, - Timeout: timeout, + Targets: config.GetScrapeTargets(), + Interval: config.GetScrapeInterval(), + Timeout: config.GetScrapeTimeout(), } } diff --git a/dashboard/config/config.go b/dashboard/config/config.go new file mode 100644 index 0000000..3740174 --- /dev/null +++ b/dashboard/config/config.go @@ -0,0 +1,86 @@ +// Package config provides shared configuration constants and helpers for the dashboard. +package config + +import ( + "os" + "strings" + "time" +) + +// DefaultScrapeTarget is the default Prometheus metrics endpoint to scrape. +const DefaultScrapeTarget = "http://zai-proxy.devpod.svc.cluster.local:8080/metrics" + +// DefaultScrapeInterval is the default interval between scrapes. +const DefaultScrapeInterval = 5 * time.Second + +// DefaultScrapeTimeout is the default HTTP timeout for each scrape. +const DefaultScrapeTimeout = 3 * time.Second + +// DefaultListenAddr is the default HTTP listen address for the dashboard API. +const DefaultListenAddr = ":8080" + +// GetEnvOrDefault retrieves an environment variable or returns a default value. +func GetEnvOrDefault(key, defaultValue string) string { + if value := os.Getenv(key); value != "" { + return value + } + return defaultValue +} + +// ParseDurationOrDefault parses a duration from an env var or returns a default. +func ParseDurationOrDefault(key string, defaultValue time.Duration) time.Duration { + if value := os.Getenv(key); value != "" { + if parsed, err := time.ParseDuration(value); err == nil { + return parsed + } + } + return defaultValue +} + +// SplitTargets splits a comma-separated list of scrape targets. +// Empty strings between commas are skipped. +func SplitTargets(s string) []string { + var result []string + var current string + for _, c := range s { + if c == ',' { + if current != "" { + result = append(result, current) + current = "" + } + } else { + current += string(c) + } + } + if current != "" { + result = append(result, current) + } + return result +} + +// GetScrapeTargets returns the list of scrape targets from SCRAPE_TARGETS env var, +// or the default target if not set. +func GetScrapeTargets() []string { + if value := os.Getenv("SCRAPE_TARGETS"); value != "" { + return SplitTargets(value) + } + return []string{DefaultScrapeTarget} +} + +// GetScrapeInterval returns the scrape interval from SCRAPE_INTERVAL env var, +// or the default interval if not set/invalid. +func GetScrapeInterval() time.Duration { + return ParseDurationOrDefault("SCRAPE_INTERVAL", DefaultScrapeInterval) +} + +// GetScrapeTimeout returns the scrape timeout from SCRAPE_TIMEOUT env var, +// or the default timeout if not set/invalid. +func GetScrapeTimeout() time.Duration { + return ParseDurationOrDefault("SCRAPE_TIMEOUT", DefaultScrapeTimeout) +} + +// GetListenAddr returns the HTTP listen address from LISTEN_ADDR env var, +// or the default address if not set. +func GetListenAddr() string { + return GetEnvOrDefault("LISTEN_ADDR", DefaultListenAddr) +}