Add http pprof_path cfg option

This commit is contained in:
ionutboangiu
2024-08-09 20:23:12 +03:00
committed by Dan Christian Bogos
parent 73fc386036
commit 594e8404dc
10 changed files with 54 additions and 6 deletions

View File

@@ -208,6 +208,7 @@ func startRPC(server *cores.Server, internalRaterChan,
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().PprofPath,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan,

View File

@@ -242,6 +242,7 @@ const CGRATES_CFG_JSON = `
"ws_url": "/ws", // WebSockets relative URL ("" to disable)
"freeswitch_cdrs_url": "/freeswitch_json", // Freeswitch CDRS relative URL ("" to disable)
"http_cdrs": "/cdr_http", // CDRS relative URL ("" to disable)
"pprof_path": "/debug/pprof/", // endpoint for serving runtime profiling data for pprof visualization
"use_basic_auth": false, // use basic authentication
"auth_users": {}, // basic authentication usernames and base64-encoded passwords (eg: { "username1": "cGFzc3dvcmQ=", "username2": "cGFzc3dvcmQy "})
"client_opts":{

View File

@@ -1854,6 +1854,7 @@ func TestDfHttpJsonCfg(t *testing.T) {
Ws_url: utils.StringPointer("/ws"),
Freeswitch_cdrs_url: utils.StringPointer("/freeswitch_json"),
Http_Cdrs: utils.StringPointer("/cdr_http"),
PprofPath: utils.StringPointer("/debug/pprof/"),
Use_basic_auth: utils.BoolPointer(false),
Auth_users: utils.MapStringStringPointer(map[string]string{}),
Client_opts: &HTTPClientOptsJson{

File diff suppressed because one or more lines are too long

View File

@@ -34,6 +34,7 @@ type HTTPCfg struct {
HTTPWSURL string // WebSocket relative URL ("" to disable)
HTTPFreeswitchCDRsURL string // Freeswitch CDRS relative URL ("" to disable)
HTTPCDRsURL string // CDRS relative URL ("" to disable)
PprofPath string // runtime profiling url path ("" to disable)
HTTPUseBasicAuth bool // Use basic auth for HTTP API
HTTPAuthUsers map[string]string // Basic auth user:password map (base64 passwords)
ClientOpts *http.Transport
@@ -135,6 +136,9 @@ func (httpcfg *HTTPCfg) loadFromJSONCfg(jsnHTTPCfg *HTTPJsonCfg) (err error) {
if jsnHTTPCfg.Http_Cdrs != nil {
httpcfg.HTTPCDRsURL = *jsnHTTPCfg.Http_Cdrs
}
if jsnHTTPCfg.PprofPath != nil {
httpcfg.PprofPath = *jsnHTTPCfg.PprofPath
}
if jsnHTTPCfg.Use_basic_auth != nil {
httpcfg.HTTPUseBasicAuth = *jsnHTTPCfg.Use_basic_auth
}
@@ -178,6 +182,7 @@ func (httpcfg *HTTPCfg) AsMapInterface() map[string]any {
utils.HTTPWSURLCfg: httpcfg.HTTPWSURL,
utils.HTTPFreeswitchCDRsURLCfg: httpcfg.HTTPFreeswitchCDRsURL,
utils.HTTPCDRsURLCfg: httpcfg.HTTPCDRsURL,
utils.PprofPathCfg: httpcfg.PprofPath,
utils.HTTPUseBasicAuthCfg: httpcfg.HTTPUseBasicAuth,
utils.HTTPAuthUsersCfg: httpcfg.HTTPAuthUsers,
utils.HTTPClientOptsCfg: clientOpts,
@@ -198,6 +203,7 @@ func (httpcfg HTTPCfg) Clone() (cln *HTTPCfg) {
HTTPWSURL: httpcfg.HTTPWSURL,
HTTPFreeswitchCDRsURL: httpcfg.HTTPFreeswitchCDRsURL,
HTTPCDRsURL: httpcfg.HTTPCDRsURL,
PprofPath: httpcfg.PprofPath,
HTTPUseBasicAuth: httpcfg.HTTPUseBasicAuth,
HTTPAuthUsers: make(map[string]string),
ClientOpts: httpcfg.ClientOpts.Clone(),

View File

@@ -34,6 +34,7 @@ func TestHTTPCfgloadFromJsonCfg(t *testing.T) {
PrometheusURL: utils.StringPointer("/prometheus"),
Ws_url: utils.StringPointer("/ws"),
Registrars_url: utils.StringPointer("/randomUrl"),
PprofPath: utils.StringPointer("/pprof/test"),
Freeswitch_cdrs_url: utils.StringPointer("/freeswitch_json"),
Http_Cdrs: utils.StringPointer("/cdr_http"),
Use_basic_auth: utils.BoolPointer(false),
@@ -46,6 +47,7 @@ func TestHTTPCfgloadFromJsonCfg(t *testing.T) {
RegistrarSURL: "/randomUrl",
HTTPFreeswitchCDRsURL: "/freeswitch_json",
HTTPCDRsURL: "/cdr_http",
PprofPath: "/pprof/test",
HTTPUseBasicAuth: false,
HTTPAuthUsers: map[string]string{},
ClientOpts: &http.Transport{
@@ -91,6 +93,7 @@ func TestHTTPCfgAsMapInterface(t *testing.T) {
utils.HTTPWSURLCfg: "/ws",
utils.HTTPFreeswitchCDRsURLCfg: "/freeswitch_json",
utils.HTTPCDRsURLCfg: "/cdr_http",
utils.PprofPathCfg: "/debug/pprof/",
utils.HTTPUseBasicAuthCfg: false,
utils.HTTPAuthUsersCfg: map[string]string{},
utils.HTTPClientOptsCfg: map[string]any{
@@ -122,7 +125,8 @@ func TestHTTPCfgAsMapInterface1(t *testing.T) {
"http": {
"json_rpc_url": "/rpc",
"ws_url": "",
"prometheus_url": "/metrics",
"prometheus_url": "/metrics",
"pprof_path": "/pprof/test",
"use_basic_auth": true,
"auth_users": {"user1": "authenticated", "user2": "authenticated"},
},
@@ -131,6 +135,7 @@ func TestHTTPCfgAsMapInterface1(t *testing.T) {
utils.HTTPJsonRPCURLCfg: "/rpc",
utils.RegistrarSURLCfg: "/registrar",
utils.PrometheusURLCfg: "/metrics",
utils.PprofPathCfg: "/pprof/test",
utils.HTTPWSURLCfg: "",
utils.HTTPFreeswitchCDRsURLCfg: "/freeswitch_json",
utils.HTTPCDRsURLCfg: "/cdr_http",
@@ -171,6 +176,7 @@ func TestHTTPCfgClone(t *testing.T) {
RegistrarSURL: "/randomUrl",
HTTPFreeswitchCDRsURL: "/freeswitch_json",
HTTPCDRsURL: "/cdr_http",
PprofPath: "/debug/pprof",
HTTPUseBasicAuth: false,
HTTPAuthUsers: map[string]string{
"user": "pass",

View File

@@ -86,6 +86,7 @@ type HTTPJsonCfg struct {
Ws_url *string
Freeswitch_cdrs_url *string
Http_Cdrs *string
PprofPath *string `json:"pprof_path"`
Use_basic_auth *bool
Auth_users *map[string]string
Client_opts *HTTPClientOptsJson

View File

@@ -201,7 +201,7 @@ func (s *Server) RegisterProfiler() {
registerProfiler(s.httpsMux)
}
func (s *Server) ServeHTTP(addr, jsonRPCURL, wsRPCURL, promURL string, useBasicAuth bool,
func (s *Server) ServeHTTP(addr, jsonRPCURL, wsRPCURL, promURL, pprofPath string, useBasicAuth bool,
userList map[string]string, shdChan *utils.SyncedChan) {
s.RLock()
enabled := s.rpcEnabled
@@ -237,7 +237,7 @@ func (s *Server) ServeHTTP(addr, jsonRPCURL, wsRPCURL, promURL string, useBasicA
s.Lock()
s.httpEnabled = true
s.Unlock()
utils.Logger.Info("<HTTP> enabling handler for Prometheus connections")
utils.Logger.Info(fmt.Sprintf("<HTTP> prometheus metrics endpoint registered at %q", promURL))
promHandler := promhttp.Handler()
if useBasicAuth {
s.httpMux.HandleFunc(promURL, use(promHandler.ServeHTTP, basicAuth(userList)))
@@ -245,6 +245,28 @@ func (s *Server) ServeHTTP(addr, jsonRPCURL, wsRPCURL, promURL string, useBasicA
s.httpMux.Handle(promURL, promHandler)
}
}
if pprofPath != "" {
s.Lock()
s.httpEnabled = true
s.Unlock()
if !strings.HasSuffix(pprofPath, "/") {
pprofPath += "/"
}
utils.Logger.Info(fmt.Sprintf("<HTTP> profiling endpoints registered at %q", pprofPath))
if useBasicAuth {
s.httpMux.HandleFunc(pprofPath, use(pprof.Index, basicAuth(userList)))
s.httpMux.HandleFunc(pprofPath+"cmdline", use(pprof.Cmdline, basicAuth(userList)))
s.httpMux.HandleFunc(pprofPath+"profile", use(pprof.Profile, basicAuth(userList)))
s.httpMux.HandleFunc(pprofPath+"symbol", use(pprof.Symbol, basicAuth(userList)))
s.httpMux.HandleFunc(pprofPath+"trace", use(pprof.Trace, basicAuth(userList)))
} else {
s.httpMux.HandleFunc(pprofPath, pprof.Index)
s.httpMux.HandleFunc(pprofPath+"cmdline", pprof.Cmdline)
s.httpMux.HandleFunc(pprofPath+"profile", pprof.Profile)
s.httpMux.HandleFunc(pprofPath+"symbol", pprof.Symbol)
s.httpMux.HandleFunc(pprofPath+"trace", pprof.Trace)
}
}
if !s.httpEnabled {
return
}

View File

@@ -211,6 +211,7 @@ func testServeHHTPPass(t *testing.T) {
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().PprofPath,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -233,6 +234,7 @@ func testServeHHTPPassUseBasicAuth(t *testing.T) {
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().PprofPath,
!cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -255,6 +257,7 @@ func testServeHHTPEnableHttp(t *testing.T) {
utils.EmptyString,
utils.EmptyString,
utils.EmptyString,
utils.EmptyString,
!cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -277,6 +280,7 @@ func testServeHHTPFail(t *testing.T) {
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().PprofPath,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -302,6 +306,7 @@ func testServeHHTPFailEnableRpc(t *testing.T) {
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().PprofPath,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)

View File

@@ -2193,6 +2193,7 @@ const (
HTTPWSURLCfg = "ws_url"
HTTPFreeswitchCDRsURLCfg = "freeswitch_cdrs_url"
HTTPCDRsURLCfg = "http_cdrs"
PprofPathCfg = "pprof_path"
HTTPUseBasicAuthCfg = "use_basic_auth"
HTTPAuthUsersCfg = "auth_users"
HTTPClientOptsCfg = "client_opts"