@Summary Collect debug profiles @ID collect-debug-profiles @Security CoderSessionToken @Tags Debug @Success 200 @Router /api/v2/debug/profile [post] @x-apidocgen {"skip": true}
(rw http.ResponseWriter, r *http.Request)
| 418 | // @Router /api/v2/debug/profile [post] |
| 419 | // @x-apidocgen {"skip": true} |
| 420 | func (api *API) debugCollectProfile(rw http.ResponseWriter, r *http.Request) { |
| 421 | ctx := r.Context() |
| 422 | |
| 423 | // Parse duration. |
| 424 | duration := profileDurationDefault |
| 425 | if v := r.URL.Query().Get("duration"); v != "" { |
| 426 | d, err := time.ParseDuration(v) |
| 427 | if err != nil { |
| 428 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 429 | Message: "Invalid duration parameter.", |
| 430 | Detail: err.Error(), |
| 431 | }) |
| 432 | return |
| 433 | } |
| 434 | if d <= 0 { |
| 435 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 436 | Message: "Duration must be positive.", |
| 437 | }) |
| 438 | return |
| 439 | } |
| 440 | if d > profileDurationMax { |
| 441 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 442 | Message: fmt.Sprintf("Duration cannot exceed %s.", profileDurationMax), |
| 443 | }) |
| 444 | return |
| 445 | } |
| 446 | duration = d |
| 447 | } |
| 448 | |
| 449 | // Parse requested profiles. |
| 450 | profiles := defaultProfiles |
| 451 | if v := r.URL.Query().Get("profiles"); v != "" { |
| 452 | profiles = strings.Split(v, ",") |
| 453 | for _, p := range profiles { |
| 454 | if !allValidProfiles[p] { |
| 455 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 456 | Message: fmt.Sprintf("Unknown profile type: %q.", p), |
| 457 | Detail: "Valid types: cpu, heap, allocs, block, mutex, goroutine, threadcreate, trace", |
| 458 | }) |
| 459 | return |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Only one profile collection can run at a time because the CPU |
| 465 | // profiler is process-global. |
| 466 | if !api.ProfileCollecting.CompareAndSwap(false, true) { |
| 467 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 468 | Message: "A profile collection is already in progress. Try again later.", |
| 469 | }) |
| 470 | return |
| 471 | } |
| 472 | defer api.ProfileCollecting.Store(false) |
| 473 | |
| 474 | // Temporarily enable block and mutex profiling so those profiles are |
| 475 | // actually populated. Restore previous values when we are done. |
| 476 | // SetBlockProfileRate does not return the previous value, so we |
| 477 | // simply disable it again after collection (the default is 0). |
nothing calls this directly
no test coverage detected