Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: all pprofs #11891

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions diagnostics/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package diagnostics

import (
"fmt"
"net/http"
"runtime/pprof"
"strings"

diaglib "github.com/erigontech/erigon-lib/diagnostics"
)

func SetupProfileAccess(metricsMux *http.ServeMux, diag *diaglib.DiagnosticClient) {
if metricsMux == nil {
return
}

//handle all pprof, supported: goroutine, threadcreate, heap, allocs, block, mutex
metricsMux.HandleFunc("/pprof/", func(w http.ResponseWriter, r *http.Request) {
profile := strings.TrimPrefix(r.URL.Path, "/pprof/")
writePprofProfile(w, profile)
})
}

func writePprofProfile(w http.ResponseWriter, profile string) {
p := pprof.Lookup(profile)
if p == nil {
http.Error(w, "Unknown profile: "+profile, http.StatusNotFound)
return
}

w.Header().Set("Content-Type", "aplication/profile")
err := p.WriteTo(w, 0)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to write profile: %v", err), http.StatusInternalServerError)
return
}
}
1 change: 1 addition & 0 deletions diagnostics/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ func SetupEndpoints(ctx *cli.Context, node *node.ErigonNode, diagMux *http.Serve
SetupHeadersAccess(diagMux, diagnostic)
SetupBodiesAccess(diagMux, diagnostic)
SetupSysInfoAccess(diagMux, diagnostic)
SetupProfileAccess(diagMux, diagnostic)
}
15 changes: 0 additions & 15 deletions diagnostics/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package diagnostics

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

diaglib "github.com/erigontech/erigon-lib/diagnostics"
"github.com/erigontech/erigon-lib/sysutils"
Expand Down Expand Up @@ -50,19 +48,6 @@ 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("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
Loading