|
8 | 8 | "net/http" |
9 | 9 | "os" |
10 | 10 | "strconv" |
| 11 | + "sync" |
11 | 12 | "time" |
12 | 13 |
|
13 | 14 | "github.com/gorilla/mux" |
@@ -338,15 +339,48 @@ func allNodesHandler(w http.ResponseWriter, r *http.Request, kubeClient *kuberne |
338 | 339 | registry := prometheus.NewRegistry() |
339 | 340 | collectors.register(registry) |
340 | 341 |
|
| 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 |
341 | 352 | 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 |
346 | 379 | } |
347 | | - collectSummaryMetrics(summary, collectors) |
| 380 | + collectSummaryMetrics(res.summary, collectors) |
348 | 381 | } |
349 | 382 |
|
| 383 | + // Return all aggregated metrics |
350 | 384 | h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) |
351 | 385 | h.ServeHTTP(w, r) |
352 | 386 | } |
|
0 commit comments