Add ability to run pprof on dedicated HTTP server (#4584)
This allows exposing the pprof/debug endpoints in a production environment more easily, where it shouldn't be exposed publicly.
This commit is contained in:
@@ -170,6 +170,11 @@ rtc:
|
|||||||
# when enabled, LiveKit will expose prometheus metrics on :6789/metrics
|
# when enabled, LiveKit will expose prometheus metrics on :6789/metrics
|
||||||
# prometheus_port: 6789
|
# prometheus_port: 6789
|
||||||
|
|
||||||
|
# expose /debug/pprof (and /debug/goroutine, /debug/rooms) on a dedicated port,
|
||||||
|
# separate from the public signalling port. only enabled when port is set.
|
||||||
|
# debug_handler_port:
|
||||||
|
# port: 7070
|
||||||
|
|
||||||
# API key / secret pairs.
|
# API key / secret pairs.
|
||||||
# Keys are used for JWT authentication, server APIs would require a keypair in order to generate access tokens
|
# Keys are used for JWT authentication, server APIs would require a keypair in order to generate access tokens
|
||||||
# and make calls to the server
|
# and make calls to the server
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ type Config struct {
|
|||||||
// PrometheusPort is deprecated
|
// PrometheusPort is deprecated
|
||||||
PrometheusPort uint32 `yaml:"prometheus_port,omitempty"`
|
PrometheusPort uint32 `yaml:"prometheus_port,omitempty"`
|
||||||
Prometheus PrometheusConfig `yaml:"prometheus,omitempty"`
|
Prometheus PrometheusConfig `yaml:"prometheus,omitempty"`
|
||||||
|
DebugHandler DebugHandlerConfig `yaml:"debug_handler,omitempty"`
|
||||||
RTC RTCConfig `yaml:"rtc,omitempty"`
|
RTC RTCConfig `yaml:"rtc,omitempty"`
|
||||||
Redis redisLiveKit.RedisConfig `yaml:"redis,omitempty"`
|
Redis redisLiveKit.RedisConfig `yaml:"redis,omitempty"`
|
||||||
Audio sfu.AudioConfig `yaml:"audio,omitempty"`
|
Audio sfu.AudioConfig `yaml:"audio,omitempty"`
|
||||||
@@ -348,6 +349,10 @@ type PrometheusConfig struct {
|
|||||||
Password string `yaml:"password,omitempty"`
|
Password string `yaml:"password,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DebugHandlerConfig struct {
|
||||||
|
Port uint32 `yaml:"port,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type ForwardStatsConfig struct {
|
type ForwardStatsConfig struct {
|
||||||
SummaryInterval time.Duration `yaml:"summary_interval,omitempty"`
|
SummaryInterval time.Duration `yaml:"summary_interval,omitempty"`
|
||||||
ReportInterval time.Duration `yaml:"report_interval,omitempty"`
|
ReportInterval time.Duration `yaml:"report_interval,omitempty"`
|
||||||
|
|||||||
+35
-1
@@ -21,7 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
httppprof "net/http/pprof"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -53,6 +53,7 @@ type LivekitServer struct {
|
|||||||
agentService *AgentService
|
agentService *AgentService
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
promServer *http.Server
|
promServer *http.Server
|
||||||
|
debugServer *http.Server
|
||||||
router routing.Router
|
router routing.Router
|
||||||
roomManager *RoomManager
|
roomManager *RoomManager
|
||||||
signalServer *SignalServer
|
signalServer *SignalServer
|
||||||
@@ -171,6 +172,20 @@ func NewLivekitServer(conf *config.Config,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if conf.DebugHandler.Port > 0 {
|
||||||
|
debugMux := http.NewServeMux()
|
||||||
|
debugMux.HandleFunc("/debug/pprof/", httppprof.Index)
|
||||||
|
debugMux.HandleFunc("/debug/pprof/cmdline", httppprof.Cmdline)
|
||||||
|
debugMux.HandleFunc("/debug/pprof/profile", httppprof.Profile)
|
||||||
|
debugMux.HandleFunc("/debug/pprof/symbol", httppprof.Symbol)
|
||||||
|
debugMux.HandleFunc("/debug/pprof/trace", httppprof.Trace)
|
||||||
|
debugMux.HandleFunc("/debug/goroutine", s.debugGoroutines)
|
||||||
|
debugMux.HandleFunc("/debug/rooms", s.debugInfo)
|
||||||
|
s.debugServer = &http.Server{
|
||||||
|
Handler: http.Handler(debugMux),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err = router.RemoveDeadNodes(); err != nil {
|
if err = router.RemoveDeadNodes(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -221,6 +236,7 @@ func (s *LivekitServer) Start() error {
|
|||||||
// ensure we could listen
|
// ensure we could listen
|
||||||
listeners := make([]net.Listener, 0)
|
listeners := make([]net.Listener, 0)
|
||||||
promListeners := make([]net.Listener, 0)
|
promListeners := make([]net.Listener, 0)
|
||||||
|
debugListeners := make([]net.Listener, 0)
|
||||||
for _, addr := range addresses {
|
for _, addr := range addresses {
|
||||||
ln, err := net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.Port))))
|
ln, err := net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.Port))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -235,6 +251,14 @@ func (s *LivekitServer) Start() error {
|
|||||||
}
|
}
|
||||||
promListeners = append(promListeners, ln)
|
promListeners = append(promListeners, ln)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.debugServer != nil {
|
||||||
|
ln, err = net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.DebugHandler.Port))))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
debugListeners = append(debugListeners, ln)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
values := []any{
|
values := []any{
|
||||||
@@ -259,6 +283,9 @@ func (s *LivekitServer) Start() error {
|
|||||||
if s.config.Prometheus.Port != 0 {
|
if s.config.Prometheus.Port != 0 {
|
||||||
values = append(values, "portPrometheus", s.config.Prometheus.Port)
|
values = append(values, "portPrometheus", s.config.Prometheus.Port)
|
||||||
}
|
}
|
||||||
|
if s.config.DebugHandler.Port != 0 {
|
||||||
|
values = append(values, "portDebugHandler", s.config.DebugHandler.Port)
|
||||||
|
}
|
||||||
if s.config.Region != "" {
|
if s.config.Region != "" {
|
||||||
values = append(values, "region", s.config.Region)
|
values = append(values, "region", s.config.Region)
|
||||||
}
|
}
|
||||||
@@ -271,6 +298,10 @@ func (s *LivekitServer) Start() error {
|
|||||||
go s.promServer.Serve(promLn)
|
go s.promServer.Serve(promLn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, debugLn := range debugListeners {
|
||||||
|
go s.debugServer.Serve(debugLn)
|
||||||
|
}
|
||||||
|
|
||||||
if err := s.signalServer.Start(); err != nil {
|
if err := s.signalServer.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -302,6 +333,9 @@ func (s *LivekitServer) Start() error {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_ = s.httpServer.Shutdown(ctx)
|
_ = s.httpServer.Shutdown(ctx)
|
||||||
|
if s.debugServer != nil {
|
||||||
|
_ = s.debugServer.Shutdown(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
if s.turnServer != nil {
|
if s.turnServer != nil {
|
||||||
_ = s.turnServer.Close()
|
_ = s.turnServer.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user