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
Changes from 1 commit
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
30 changes: 19 additions & 11 deletions libbeat/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand Down Expand Up @@ -97,24 +98,31 @@ func BeforeRun() {

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

// register pprof handler
mux.HandleFunc("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) {
http.DefaultServeMux.ServeHTTP(w, r)
})
// 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 {
log.Fatalf("Failed to start stats listener: %v", err)
}

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