| 332 | } |
| 333 | |
| 334 | func fetchPrometheusMetrics(ctx context.Context, client *codersdk.Client, log slog.Logger) ([]byte, error) { |
| 335 | if client == nil { |
| 336 | return nil, xerrors.New("nil client") |
| 337 | } |
| 338 | |
| 339 | reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 340 | defer cancel() |
| 341 | |
| 342 | resp, err := client.Request(reqCtx, http.MethodGet, "/api/v2/debug/metrics", nil) |
| 343 | if err != nil { |
| 344 | return nil, xerrors.Errorf("request metrics: %w", err) |
| 345 | } |
| 346 | defer resp.Body.Close() |
| 347 | |
| 348 | body, err := io.ReadAll(resp.Body) |
| 349 | if err != nil { |
| 350 | return nil, xerrors.Errorf("read metrics body: %w", err) |
| 351 | } |
| 352 | |
| 353 | if resp.StatusCode != http.StatusOK { |
| 354 | log.Debug(ctx, "coderd prometheus metrics fetch non-200", |
| 355 | slog.F("status", resp.StatusCode), slog.F("body_len", len(body))) |
| 356 | return nil, xerrors.Errorf("unexpected status code %d", resp.StatusCode) |
| 357 | } |
| 358 | |
| 359 | trimmed := bytes.TrimSpace(body) |
| 360 | if len(trimmed) == 0 { |
| 361 | return nil, xerrors.New("empty prometheus metrics response") |
| 362 | } |
| 363 | return append([]byte(nil), trimmed...), nil |
| 364 | } |
| 365 | |
| 366 | func NetworkInfo(ctx context.Context, client *codersdk.Client, log slog.Logger) Network { |
| 367 | var ( |