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

Ensure pprof endpoint is listening on startup #17028

Merged
merged 6 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -68,6 +68,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]

*Auditbeat*

Expand Down
43 changes: 27 additions & 16 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 Down Expand Up @@ -90,31 +90,42 @@ func BeforeRun() {
if withCPUProfile() {
cpuOut, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
logp.Err("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)
})
logp.Info("Start pprof endpoint")
mux := http.NewServeMux()

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

// register metrics handler
mux.HandleFunc("/debug/vars", metricsHandler)
// Register metrics handler
mux.HandleFunc("/debug/vars", metricsHandler)

endpoint := http.ListenAndServe(*httpprof, mux)
logp.Info("finished pprof endpoint: %v", endpoint)
}()
// Ensure we are listening before returning
listener, err := net.Listen("tcp", *httpprof)
if err != nil {
logp.Err("Failed to start stats listener: %v", err)
os.Exit(1)
}

go func() {
// Serve returns always a non-nil error
err := http.Serve(listener, mux)
logp.Info("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