| 1 |
package global |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log/slog" |
| 6 |
"net/http" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func overload(ctx context.Context, addr string) { |
| 11 |
client := http.Client{Timeout: 3 * time.Second} |
| 12 |
|
| 13 |
ticker := time.NewTicker(2 * time.Second) |
| 14 |
url := "http://" + addr + "/health" |
| 15 |
|
| 16 |
for { |
| 17 |
select { |
| 18 |
case <-ticker.C: |
| 19 |
start := time.Now() |
| 20 |
resp, err := client.Get(url) |
| 21 |
if err != nil { |
| 22 |
HealthDuration.Observe(time.Since(start).Seconds()) |
| 23 |
HealthFailures.Inc() |
| 24 |
log().Error("Failed health", Err(err)) |
| 25 |
continue |
| 26 |
} |
| 27 |
resp.Body.Close() |
| 28 |
elapsed := time.Since(start) |
| 29 |
HealthDuration.Observe(elapsed.Seconds()) |
| 30 |
if elapsed > time.Second { // 1s is pretty random, but a *local* scrape taking that long isn't good |
| 31 |
log().Warn("Slow health", slog.Duration("elapsed", elapsed)) |
| 32 |
} |
| 33 |
|
| 34 |
case <-ctx.Done(): |
| 35 |
return |
| 36 |
} |
| 37 |
} |
| 38 |
} |