Skip to content

Commit

Permalink
diagnostics: added func to grab heap profile (#11643) (#11649)
Browse files Browse the repository at this point in the history
- Added functionality to grab the heap profile by calling the
diagnostics endpoint
- Added support to pass heap profile file to diagnostics UI through
WebSocket
  • Loading branch information
dvovk authored Aug 24, 2024
1 parent 3d7c090 commit f30c4c4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions diagnostics/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package diagnostics

import (
"encoding/json"
"fmt"
"net/http"
"runtime/pprof"

diaglib "github.com/ledgerwatch/erigon-lib/diagnostics"
"github.com/ledgerwatch/erigon-lib/sysutils"
Expand Down Expand Up @@ -52,6 +54,20 @@ func SetupSysInfoAccess(metricsMux *http.ServeMux, diag *diaglib.DiagnosticClien
w.Header().Set("Content-Type", "application/json")
writeMemoryInfo(w)
})

metricsMux.HandleFunc("/heap-profile", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "aplication/profile")
writeHeapProfile(w)
})
}

func writeHeapProfile(w http.ResponseWriter) {
err := pprof.Lookup("heap").WriteTo(w, 0)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to write profile: %v", err), http.StatusInternalServerError)
return
}
}

func writeHardwareInfo(w http.ResponseWriter, diag *diaglib.DiagnosticClient) {
Expand Down
31 changes: 31 additions & 0 deletions turbo/app/support_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,37 @@ func tunnel(ctx context.Context, cancel context.CancelFunc, sigs chan os.Signal,
})
}

case "aplication/profile":
if _, err := io.Copy(buffer, debugResponse.Body); err != nil {
return codec.WriteJSON(ctx1, &nodeResponse{
Id: requestId,
Error: &responseError{
Code: http.StatusInternalServerError,
Message: fmt.Sprintf("Request for metrics method [%s] failed: %v", debugURL, err),
},
Last: true,
})
}

data, err := json.Marshal(struct {
Data []byte `json:"chunk"`
}{
Data: buffer.Bytes(),
})

buffer = bytes.NewBuffer(data)

if err != nil {
return codec.WriteJSON(ctx1, &nodeResponse{
Id: requestId,
Error: &responseError{
Code: int64(http.StatusInternalServerError),
Message: fmt.Sprintf("Can't copy metrics response for [%s]: %s", debugURL, err),
},
Last: true,
})
}

default:
return codec.WriteJSON(ctx1, &nodeResponse{
Id: requestId,
Expand Down

0 comments on commit f30c4c4

Please sign in to comment.