From 9a594f390e4ae7ea33c6613b71f077b1afc81dad Mon Sep 17 00:00:00 2001 From: "Derrick Hammer (aider)" Date: Fri, 27 Dec 2024 07:28:57 -0500 Subject: [PATCH] refactor!: simplify etcd path handling to single base path - Remove support for multiple etcd paths via scrape-etcd-paths flag - Refactor getScrapeTargets to use single etcd path - Extract service name from last segment of etcd path - Clean up whitespace and formatting --- cmd/promster/main.go | 104 ++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/cmd/promster/main.go b/cmd/promster/main.go index ba56321..114f0da 100644 --- a/cmd/promster/main.go +++ b/cmd/promster/main.go @@ -126,17 +126,16 @@ func reloadPrometheus() error { } } - func parseAddress(address string) (host string, metricsPath string) { // Default metrics path metricsPath = "/metrics" - + // Remove scheme (http:// or https://) if strings.Contains(address, "://") { parts := strings.SplitN(address, "://", 2) address = parts[1] } - + // Extract path if exists if strings.Contains(address, "/") { parts := strings.SplitN(address, "/", 2) @@ -147,64 +146,64 @@ func parseAddress(address string) (host string, metricsPath string) { } else { host = address } - + return host, metricsPath } -func getScrapeTargets(registry *etcdregistry.EtcdRegistry, scrapeEtcdPaths []string) []ServiceGroup { +func getScrapeTargets(registry *etcdregistry.EtcdRegistry, etcdPath string) []ServiceGroup { // Map to group targets by service name and auth groupMap := make(map[string]*ServiceGroup) - for _, path := range scrapeEtcdPaths { - nodes, err := registry.GetServiceNodes(path) - if err != nil { - logrus.Warnf("Failed to get service nodes for path %s: %v", path, err) - continue + nodes, err := registry.GetServiceNodes(etcdPath) + if err != nil { + logrus.Warnf("Failed to get service nodes for path %s: %v", etcdPath, err) + return nil + } + + for _, node := range nodes { + // Use the last part of the path as the service name + pathParts := strings.Split(etcdPath, "/") + serviceName := pathParts[len(pathParts)-1] + address, metricsPath := parseAddress(node.Info["address"]) + + // Create auth key for grouping + authKey := "" + if password, ok := node.Info["password"]; ok { + username := node.Info["username"] + if username == "" { + username = "prometheus" // default username + } + authKey = username + ":" + password } - for _, node := range nodes { - serviceName := path - address, metricsPath := parseAddress(node.Info["address"]) + // Create group key + groupKey := serviceName + "|" + authKey - // Create auth key for grouping - authKey := "" - if password, ok := node.Info["password"]; ok { - username := node.Info["username"] - if username == "" { - username = "prometheus" // default username - } - authKey = username + ":" + password + group, exists := groupMap[groupKey] + if !exists { + group = &ServiceGroup{ + Name: serviceName, + Targets: make([]string, 0), + MetricsPath: metricsPath, + NodeID: node.Name, } - // Create group key - groupKey := serviceName + "|" + authKey - - group, exists := groupMap[groupKey] - if !exists { - group = &ServiceGroup{ - Name: serviceName, - Targets: make([]string, 0), - MetricsPath: metricsPath, - NodeID: node.Name, + // Only set auth if credentials exist + if authKey != "" { + username := node.Info["username"] + if username == "" { + username = "prometheus" } - - // Only set auth if credentials exist - if authKey != "" { - username := node.Info["username"] - if username == "" { - username = "prometheus" - } - group.BasicAuth = &BasicAuth{ - Username: username, - Password: node.Info["password"], - } + group.BasicAuth = &BasicAuth{ + Username: username, + Password: node.Info["password"], } - - groupMap[groupKey] = group } - group.Targets = append(group.Targets, address) + groupMap[groupKey] = group } + + group.Targets = append(group.Targets, address) } // Convert map to slice @@ -252,7 +251,7 @@ var ( configFile string ) -func monitorTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry, scrapeEtcdPaths []string) error { +func monitorTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry, etcdPath string) error { ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() @@ -264,7 +263,7 @@ func monitorTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry, sc for { select { case <-ticker.C: - serviceGroups := getScrapeTargets(registry, scrapeEtcdPaths) + serviceGroups := getScrapeTargets(registry, etcdPath) targetsMu.Lock() if !reflect.DeepEqual(serviceGroups, previousGroups) { @@ -304,7 +303,6 @@ func run(ctx context.Context, cmd *cli.Command) error { // Get flag values etcdURLScrape := cmd.String("scrape-etcd-url") etcdBasePath := cmd.String("etcd-base-path") - scrapeEtcdPaths := strings.Split(cmd.String("scrape-etcd-paths"), ",") scrapeInterval = cmd.String("scrape-interval") scrapeTimeout = cmd.String("scrape-timeout") evaluationInterval = cmd.String("evaluation-interval") @@ -346,7 +344,7 @@ func run(ctx context.Context, cmd *cli.Command) error { if rulesFile == "" { rulesFile = "/rules.yml" } - + if err := createRulesFromENV(rulesFile); err != nil { return fmt.Errorf("failed to create rules: %w", err) } @@ -354,7 +352,7 @@ func run(ctx context.Context, cmd *cli.Command) error { ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) defer cancel() - return monitorTargets(ctx, registry, scrapeEtcdPaths) + return monitorTargets(ctx, registry, etcdBasePath) } func main() { @@ -380,12 +378,6 @@ func main() { Required: true, Sources: cli.EnvVars("PROMSTER_ETCD_BASE_PATH"), }, - &cli.StringFlag{ - Name: "scrape-etcd-paths", - Usage: "Comma-separated list of base ETCD paths for getting servers to be scrapped", - Required: true, - Sources: cli.EnvVars("PROMSTER_SCRAPE_ETCD_PATHS"), - }, &cli.StringFlag{ Name: "scrape-interval", Value: "30s",