Implement HTTP endpoint for prometheus

configurable via 'prometheus_url' option in the HTTP section
This commit is contained in:
ionutboangiu
2024-07-23 18:18:06 +03:00
committed by Dan Christian Bogos
parent cc25eae687
commit 78e020e2be
13 changed files with 100 additions and 32 deletions

View File

@@ -38,6 +38,7 @@ import (
"github.com/cgrates/cgrates/analyzers"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/websocket"
)
@@ -200,8 +201,8 @@ func (s *Server) RegisterProfiler() {
registerProfiler(s.httpsMux)
}
func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string,
useBasicAuth bool, userList map[string]string, shdChan *utils.SyncedChan) {
func (s *Server) ServeHTTP(addr, jsonRPCURL, wsRPCURL, promURL string, useBasicAuth bool,
userList map[string]string, shdChan *utils.SyncedChan) {
s.RLock()
enabled := s.rpcEnabled
s.RUnlock()
@@ -232,6 +233,18 @@ func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string,
s.httpMux.Handle(wsRPCURL, wsHandler)
}
}
if promURL != "" {
s.Lock()
s.httpEnabled = true
s.Unlock()
utils.Logger.Info("<HTTP> enabling handler for Prometheus connections")
promHandler := promhttp.Handler()
if useBasicAuth {
s.httpMux.HandleFunc(promURL, use(promHandler.ServeHTTP, basicAuth(userList)))
} else {
s.httpMux.Handle(promURL, promHandler)
}
}
if !s.httpEnabled {
return
}

View File

@@ -210,6 +210,7 @@ func testServeHHTPPass(t *testing.T) {
":6555",
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -231,6 +232,7 @@ func testServeHHTPPassUseBasicAuth(t *testing.T) {
":56432",
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
!cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -252,6 +254,7 @@ func testServeHHTPEnableHttp(t *testing.T) {
":45779",
utils.EmptyString,
utils.EmptyString,
utils.EmptyString,
!cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -273,6 +276,7 @@ func testServeHHTPFail(t *testing.T) {
"invalid_port_format",
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)
@@ -297,6 +301,7 @@ func testServeHHTPFailEnableRpc(t *testing.T) {
go server.ServeHTTP(":1000",
cfg.HTTPCfg().HTTPJsonRPCURL,
cfg.HTTPCfg().HTTPWSURL,
cfg.HTTPCfg().PrometheusURL,
cfg.HTTPCfg().HTTPUseBasicAuth,
cfg.HTTPCfg().HTTPAuthUsers,
shdChan)