Skip to content

Commit

Permalink
feat(cdn): add pprof debug routes (#5531)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Nov 3, 2020
1 parent b5f7303 commit 3094e35
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
8 changes: 4 additions & 4 deletions engine/cdn/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *Service) Serve(c context.Context) error {

if s.Cfg.EnableLogProcessing {
log.Info(ctx, "Initializing database connection...")
//Intialize database
// Intialize database
s.DBConnectionFactory, err = database.Init(
ctx,
s.Cfg.Database.User,
Expand Down Expand Up @@ -184,14 +184,14 @@ func (s *Service) Serve(c context.Context) error {
MaxHeaderBytes: 1 << 20,
}

//Gracefully shutdown the http server
s.GoRoutines.Run(ctx, "service.httpserver-shutdown", func(ctx context.Context) {
// Gracefully shutdown the http server
s.GoRoutines.Exec(ctx, "service.httpserver-shutdown", func(ctx context.Context) {
<-ctx.Done()
log.Info(ctx, "CDN> Shutdown HTTP Server")
_ = server.Shutdown(ctx)
})

//Start the http server
// Start the http server
log.Info(ctx, "CDN> Starting HTTP Server on port %d", s.Cfg.HTTP.Port)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("CDN> Cannot start cds-cdn: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions engine/cdn/cdn_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func (s *Service) initRouter(ctx context.Context) {
r.Handle("/mon/status", nil, r.GET(s.statusHandler, service.OverrideAuth(service.NoAuthMiddleware)))
r.Handle("/mon/metrics", nil, r.GET(service.GetPrometheustMetricsHandler(s), service.OverrideAuth(service.NoAuthMiddleware)))
r.Handle("/mon/metrics/all", nil, r.GET(service.GetMetricsHandler, service.OverrideAuth(service.NoAuthMiddleware)))
r.Handle("/mon/profile", nil, r.GET(service.GetAllProfilesHandler, service.OverrideAuth(service.NoAuthMiddleware)))
r.Handle("/mon/profile/{name}", nil, r.GET(service.GetProfileHandler, service.OverrideAuth(service.NoAuthMiddleware)))

r.Handle("/cache", nil, r.DELETE(s.deleteCacheHandler))
r.Handle("/cache/status", nil, r.GET(s.getStatusCacheHandler))
Expand Down
49 changes: 49 additions & 0 deletions engine/service/debug.go
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
}
}

0 comments on commit 3094e35

Please sign in to comment.