-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #489 from JiaweiGithub/feat/pprof
add pprof for agent and gateway
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 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
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,56 @@ | ||
package profile | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/pprof" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubeedge/edgemesh/pkg/apis/config/v1alpha1" | ||
) | ||
|
||
const ( | ||
// ReadHeaderTimeout is the amount of time allowed to read | ||
// request headers. | ||
// HTTP timeouts are necessary to expire inactive connections | ||
// and failing to do so might make the application vulnerable | ||
// to attacks like slowloris which work by sending data very slow, | ||
// which in case of no timeout will keep the connection active | ||
// eventually leading to a denial-of-service (DoS) attack. | ||
// References: | ||
// - https://en.wikipedia.org/wiki/Slowloris_(computer_security) | ||
ReadHeaderTimeout = 32 * time.Second | ||
) | ||
|
||
func installHandlerForPProf(mux *http.ServeMux) { | ||
mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | ||
} | ||
|
||
// ListenAndServer start a http server to enable pprof. | ||
func ListenAndServer(pprofconfig *v1alpha1.PprofConfig) { | ||
if pprofconfig != nil && pprofconfig.EnableProfiling { | ||
mux := http.NewServeMux() | ||
installHandlerForPProf(mux) | ||
addr := net.JoinHostPort(pprofconfig.ProfilingAddress, strconv.Itoa(pprofconfig.ProfilingPort)) | ||
klog.Infof("Starting profiling on address %s", addr) | ||
go func() { | ||
httpServer := http.Server{ | ||
Addr: addr, | ||
Handler: mux, | ||
ReadHeaderTimeout: ReadHeaderTimeout, | ||
} | ||
if err := httpServer.ListenAndServe(); err != nil { | ||
klog.Errorf("Failed to start profiling server: %v", err) | ||
os.Exit(1) | ||
} | ||
}() | ||
} | ||
} |