Skip to content

Commit 817ea3e

Browse files
authored
Make allNodesHandler concurrent (#141)
1 parent 89f40e6 commit 817ea3e

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

main.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"strconv"
11+
"sync"
1112
"time"
1213

1314
"github.com/gorilla/mux"
@@ -338,15 +339,48 @@ func allNodesHandler(w http.ResponseWriter, r *http.Request, kubeClient *kuberne
338339
registry := prometheus.NewRegistry()
339340
collectors.register(registry)
340341

342+
type result struct {
343+
summary *stats.Summary
344+
node string
345+
err error
346+
}
347+
348+
results := make(chan result, len(nodes.Items))
349+
var wg sync.WaitGroup
350+
351+
// Process each node concurrently
341352
for _, node := range nodes.Items {
342-
summary, err := nodeSummary(ctx, kubeClient, node.Name)
343-
if err != nil {
344-
http.Error(w, fmt.Sprintf("Error querying /stats/summary for %s: %v", node.Name, err), http.StatusInternalServerError)
345-
return
353+
wg.Add(1)
354+
go func(n string) {
355+
defer wg.Done()
356+
357+
// Each nodeSummary call gets the shared context (with timeout)
358+
summary, err := nodeSummary(ctx, kubeClient, n)
359+
results <- result{
360+
summary: summary,
361+
node: n,
362+
err: err,
363+
}
364+
}(node.Name)
365+
}
366+
367+
// Close channel when all node scrapes finish
368+
go func() {
369+
wg.Wait()
370+
close(results)
371+
}()
372+
373+
// Consume results
374+
for res := range results {
375+
if res.err != nil {
376+
// Log error but DO NOT fail the whole scrape
377+
fmt.Printf("Error scraping %s: %v\n", res.node, res.err)
378+
continue
346379
}
347-
collectSummaryMetrics(summary, collectors)
380+
collectSummaryMetrics(res.summary, collectors)
348381
}
349382

383+
// Return all aggregated metrics
350384
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
351385
h.ServeHTTP(w, r)
352386
}

0 commit comments

Comments
 (0)