Skip to content

Commit

Permalink
refactor!: simplify etcd path handling to single base path
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pcfreak30 committed Dec 27, 2024
1 parent 7feca2f commit 9a594f3
Showing 1 changed file with 48 additions and 56 deletions.
104 changes: 48 additions & 56 deletions cmd/promster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -346,15 +344,15 @@ 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)
}

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

return monitorTargets(ctx, registry, scrapeEtcdPaths)
return monitorTargets(ctx, registry, etcdBasePath)
}

func main() {
Expand All @@ -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",
Expand Down

0 comments on commit 9a594f3

Please sign in to comment.