Skip to content

Commit

Permalink
fix: Correctly group Prometheus scrape targets by service and exporte…
Browse files Browse the repository at this point in the history
…r type
  • Loading branch information
pcfreak30 committed Dec 29, 2024
1 parent 7c91e76 commit 532df8c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cmd/promster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func getScrapeTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry)
return nil, fmt.Errorf("failed to get service groups: %w", err)
}

// Map to hold nodes by job name
// Map to hold nodes by job name and exporter type
groupedNodes := make(map[string]*ServiceGroup)

// For each service, get its group and nodes
Expand All @@ -190,6 +190,9 @@ func getScrapeTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry)

// Process nodes for this service
for _, node := range nodes {
// Create a composite key using service name and exporter type
groupKey := fmt.Sprintf("%s_%s", serviceName, node.ExporterType)

// Use ingress_host from labels if available, fallback to node ID
hostname := node.ID
if ingressHost, ok := node.Labels["ingress_host"]; ok && ingressHost != "" {
Expand All @@ -208,7 +211,7 @@ func getScrapeTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry)
}

// Get or create service group
sg, exists := groupedNodes[serviceName]
sg, exists := groupedNodes[groupKey]
if !exists {
// Merge group common labels with node labels
labels := make(map[string]string)
Expand All @@ -219,8 +222,11 @@ func getScrapeTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry)
labels[k] = v
}

// Include exporter type in the job name
jobName := fmt.Sprintf("%s-%s", serviceName, node.ExporterType)

sg = &ServiceGroup{
Name: serviceName,
Name: jobName,
Targets: []string{},
MetricsPath: metricsPath,
Labels: labels,
Expand All @@ -234,7 +240,7 @@ func getScrapeTargets(ctx context.Context, registry *etcdregistry.EtcdRegistry)
}
}

groupedNodes[serviceName] = sg
groupedNodes[groupKey] = sg
}

// Add target to existing group
Expand Down

0 comments on commit 532df8c

Please sign in to comment.