Skip to content

Commit

Permalink
signal/grpc: ability to pprof the server (#472)
Browse files Browse the repository at this point in the history
Thanks @gobwas !
  • Loading branch information
gobwas authored Mar 22, 2021
1 parent 191ae90 commit 7be82ab
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cmd/signal/grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"os"

"github.com/pion/ion-sfu/pkg/middlewares/datachannel"
Expand Down Expand Up @@ -37,7 +38,9 @@ var (
addr string
metricsAddr string
verbosityLevel int
logger = log.New()
paddr string

logger = log.New()
)

const (
Expand All @@ -50,6 +53,7 @@ func showHelp() {
fmt.Println(" -a {listen addr}")
fmt.Println(" -h (show help info)")
fmt.Println(" -v {0-10} (verbosity level, default 0)")
fmt.Println(" -paddr {pprof listen addr}")

}

Expand Down Expand Up @@ -97,8 +101,14 @@ func parse() bool {
flag.StringVar(&addr, "a", ":50051", "address to use")
flag.StringVar(&metricsAddr, "m", ":8100", "merics to use")
flag.IntVar(&verbosityLevel, "v", -1, "verbosity level, higher value - more logs")
flag.StringVar(&paddr, "paddr", "", "pprof listening address")
help := flag.Bool("h", false, "help info")
flag.Parse()

if paddr == "" {
paddr = getEnv("paddr")
}

if !load() {
return false
}
Expand All @@ -109,6 +119,14 @@ func parse() bool {
return true
}

func getEnv(key string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}

return ""
}

func startMetrics(addr string) {
// start metrics server
m := http.NewServeMux()
Expand Down Expand Up @@ -151,6 +169,13 @@ func main() {
os.Exit(1)
}

if paddr != "" {
go func() {
logger.Info("PProf Listening", "addr", paddr)
_ = http.ListenAndServe(paddr, http.DefaultServeMux)
}()
}

s := grpc.NewServer(
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
)
Expand Down

0 comments on commit 7be82ab

Please sign in to comment.