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

Cherry-pick #17028 to 7.x: Ensure pprof endpoint is listening on startup #17081

Merged
merged 3 commits into from
Mar 19, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix `NewContainerMetadataEnricher` to use default config for kubernetes module. {pull}16857[16857]
- Improve some logging messages for add_kubernetes_metadata processor {pull}16866{16866}
- Fix k8s metadata issue regarding node labels not shown up on root level of metadata. {pull}16834[16834]
- Fail to start if httpprof is used and it cannot be initialized. {pull}17028[17028]
- Fix concurrency issues in convert processor when used in the global context. {pull}17032[17032]

*Auditbeat*
Expand Down
66 changes: 42 additions & 24 deletions libbeat/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"expvar"
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand All @@ -41,6 +41,7 @@ import (
// the service shut downs gracefully.
func HandleSignals(stopFunction func(), cancel context.CancelFunc) {
var callback sync.Once
logger := logp.NewLogger("service")

// On termination signals, gracefully stop the Beat
sigc := make(chan os.Signal, 1)
Expand All @@ -50,9 +51,9 @@ func HandleSignals(stopFunction func(), cancel context.CancelFunc) {

switch sig {
case syscall.SIGINT, syscall.SIGTERM:
logp.Debug("service", "Received sigterm/sigint, stopping")
logger.Debug("Received sigterm/sigint, stopping")
case syscall.SIGHUP:
logp.Debug("service", "Received sighup, stopping")
logger.Debug("Received sighup, stopping")
}

cancel()
Expand All @@ -61,7 +62,7 @@ func HandleSignals(stopFunction func(), cancel context.CancelFunc) {

// Handle the Windows service events
go ProcessWindowsControlEvents(func() {
logp.Debug("service", "Received svc stop/shutdown request")
logger.Debug("Received svc stop/shutdown request")
callback.Do(stopFunction)
})
}
Expand All @@ -87,34 +88,46 @@ func withCPUProfile() bool { return *cpuprofile != "" }
// BeforeRun takes care of necessary actions such as creating files
// before the beat should run.
func BeforeRun() {
logger := logp.NewLogger("service")
if withCPUProfile() {
cpuOut, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
logger.Errorf("Failed to create CPU profile: %v", err)
os.Exit(1)
}
pprof.StartCPUProfile(cpuOut)
}

if *httpprof != "" {
logp.Info("start pprof endpoint")
go func() {
mux := http.NewServeMux()
if *httpprof == "" {
return
}

// register pprof handler
mux.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) {
http.DefaultServeMux.ServeHTTP(w, r)
})
logger.Info("Start pprof endpoint")
mux := http.NewServeMux()

// register metrics handler
mux.HandleFunc("/debug/vars", metricsHandler)
// Register pprof handler
mux.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) {
http.DefaultServeMux.ServeHTTP(w, r)
})

endpoint := http.ListenAndServe(*httpprof, mux)
logp.Info("finished pprof endpoint: %v", endpoint)
}()
// Register metrics handler
mux.HandleFunc("/debug/vars", metricsHandler)

// Ensure we are listening before returning
listener, err := net.Listen("tcp", *httpprof)
if err != nil {
logger.Errorf("Failed to start pprof listener: %v", err)
os.Exit(1)
}

go func() {
// Serve returns always a non-nil error
err := http.Serve(listener, mux)
logger.Infof("Finished pprof endpoint: %v", err)
}()
}

// report expvar and all libbeat/monitoring metrics
// metricsHandler reports expvar and all libbeat/monitoring metrics
func metricsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

Expand Down Expand Up @@ -142,6 +155,7 @@ func metricsHandler(w http.ResponseWriter, r *http.Request) {
// Cleanup handles cleaning up the runtime and OS environments. This includes
// tasks such as stopping the CPU profile if it is running.
func Cleanup() {
logger := logp.NewLogger("service")
if withCPUProfile() {
pprof.StopCPUProfile()
cpuOut.Close()
Expand All @@ -152,25 +166,29 @@ func Cleanup() {

writeHeapProfile(*memprofile)

debugMemStats()
debugMemStats(logger.Named("mem"))
}
}

func debugMemStats() {
func debugMemStats(logger *logp.Logger) {
if !logger.IsDebug() {
return
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
logp.Debug("mem", "Memory stats: In use: %d Total (even if freed): %d System: %d",
logger.Debug("Memory stats: In use: %d Total (even if freed): %d System: %d",
m.Alloc, m.TotalAlloc, m.Sys)
}

func writeHeapProfile(filename string) {
logger := logp.NewLogger("service")
f, err := os.Create(filename)
if err != nil {
logp.Err("Failed creating file %s: %s", filename, err)
logger.Errorf("Failed creating file %s: %s", filename, err)
return
}
pprof.WriteHeapProfile(f)
f.Close()

logp.Info("Created memory profile file %s.", filename)
logger.Infof("Created memory profile file %s.", filename)
}