-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdn): add pprof debug routes (#5531)
Signed-off-by: francois samin <[email protected]>
- Loading branch information
Showing
3 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"runtime" | ||
"runtime/pprof" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func GetAllProfilesHandler() Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
profiles := pprof.Profiles() | ||
profileCounters := map[string]int{} | ||
for _, p := range profiles { | ||
if p != nil { | ||
profileCounters[p.Name()] = p.Count() | ||
} | ||
} | ||
return WriteJSON(w, profileCounters, http.StatusOK) | ||
} | ||
} | ||
|
||
func GetProfileHandler() Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
name := mux.Vars(r)["name"] | ||
if name == "" { | ||
return sdk.ErrWrongRequest | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
debug := FormInt(r, "debug") | ||
p := pprof.Lookup(name) | ||
if p == nil { | ||
w.WriteHeader(404) | ||
fmt.Fprintf(w, "Unknown profile: %s\n", name) | ||
return nil | ||
} | ||
gc := FormInt(r, "gc") | ||
if name == "heap" && gc > 0 { | ||
runtime.GC() | ||
} | ||
p.WriteTo(w, debug) | ||
return nil | ||
} | ||
} |