refactor(bf-j6s): update default SCRAPE_TARGETS from mcp to devpod namespace

- Refactor dashboard/collector/collector.go to use config.GetScrapeTargets()
- Refactor dashboard/api/router.go to use config.GetScrapeTargets()
- Add dashboard/config/config.go with DefaultScrapeTarget set to devpod namespace
- Remove hardcoded mcp namespace defaults from both files

Acceptance: No occurrence of zai-proxy.mcp.svc.cluster.local remains in dashboard Go code, both DefaultConfig structs use devpod default, existing SCRAPE_TARGETS env override unchanged.
This commit is contained in:
jedarden 2026-07-02 17:18:32 -04:00
parent ef301090ce
commit 633df37649
3 changed files with 94 additions and 69 deletions

View file

@ -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{

View file

@ -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(),
}
}

View file

@ -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)
}