MCPcopy Index your code
hub / github.com/coder/coder / debugCollectProfile

Method debugCollectProfile

coderd/debug.go:420–630  ·  view source on GitHub ↗

@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)

Source from the content-addressed store, hash-verified

418// @Router /api/v2/debug/profile [post]
419// @x-apidocgen {"skip": true}
420func (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).

Callers

nothing calls this directly

Calls 15

WriteFunction · 0.92
CompareAndSwapMethod · 0.80
ContextMethod · 0.65
GetMethod · 0.65
SetBlockProfileRateMethod · 0.65
StartCPUProfileMethod · 0.65
StartTraceMethod · 0.65
StopMethod · 0.65
WriteMethod · 0.65
LookupProfileMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected