mirror of
https://github.com/fatedier/frp.git
synced 2026-10-20 11:03:27 +08:00
feat: add metricsServer for health checks, metrics and pprof (#5578)
Add a separate HTTP listener for observability, configured through metricsServer in both frps and frpc. It serves /healthz and, when pprofEnable is set, the pprof handlers except /debug/pprof/cmdline. It never serves the dashboard, static assets or the admin API, and it does not touch the global assets filesystem. frps also serves /metrics on this listener with both runtime and business metrics, enabled by metricsServer.port alone. The in-memory collector still starts only with the dashboard, and the Prometheus collector starts either with metricsServer or with enablePrometheus plus webServer, so a configuration that collected nothing before still collects nothing. The /healthz, /metrics and pprof endpoints on webServer, and enablePrometheus, keep working but are deprecated and log a hint at startup. frpc does not serve /metrics yet.
This commit is contained in:
+32
-3
@@ -123,7 +123,8 @@ type Service struct {
|
||||
auth *auth.ClientAuth
|
||||
|
||||
// web server for admin UI and apis
|
||||
webServer *httppkg.Server
|
||||
webServer *httppkg.Server
|
||||
metricsServer *httppkg.Server
|
||||
|
||||
vnetController *vnet.Controller
|
||||
|
||||
@@ -188,8 +189,8 @@ func NewService(options ServiceOptions) (*Service, error) {
|
||||
proxyCfgs = config.CompleteProxyConfigurers(proxyCfgs)
|
||||
visitorCfgs = config.CompleteVisitorConfigurers(visitorCfgs)
|
||||
|
||||
// Create the web server after all fallible steps so its listener is not
|
||||
// leaked when an earlier error causes NewService to return.
|
||||
// Create HTTP servers after all other fallible steps, rolling back the web
|
||||
// listener if the metrics server cannot be created.
|
||||
var webServer *httppkg.Server
|
||||
if options.Common.WebServer.Port > 0 {
|
||||
ws, err := httppkg.NewServer(options.Common.WebServer)
|
||||
@@ -198,11 +199,19 @@ func NewService(options ServiceOptions) (*Service, error) {
|
||||
}
|
||||
webServer = ws
|
||||
}
|
||||
metricsServer, err := httppkg.NewMetricsServer(options.Common.MetricsServer, nil)
|
||||
if err != nil {
|
||||
if webServer != nil {
|
||||
_ = webServer.Close()
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := &Service{
|
||||
ctx: context.Background(),
|
||||
auth: authRuntime,
|
||||
webServer: webServer,
|
||||
metricsServer: metricsServer,
|
||||
common: options.Common,
|
||||
reloadCommon: options.Common,
|
||||
configFilePath: options.ConfigFilePath,
|
||||
@@ -263,6 +272,12 @@ func (svr *Service) Run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if svr.webServer != nil {
|
||||
if svr.common.WebServer.PprofEnable {
|
||||
log.Warnf("webServer pprof endpoints are deprecated; use metricsServer.pprofEnable instead")
|
||||
}
|
||||
if svr.common.MetricsServer.Port == 0 {
|
||||
log.Infof("webServer /healthz is deprecated; health probes can migrate to metricsServer.port")
|
||||
}
|
||||
webServer := svr.webServer
|
||||
go func() {
|
||||
log.Infof("admin server listen on %s", webServer.Address())
|
||||
@@ -272,6 +287,16 @@ func (svr *Service) Run(ctx context.Context) error {
|
||||
}()
|
||||
}
|
||||
|
||||
if svr.metricsServer != nil {
|
||||
metricsServer := svr.metricsServer
|
||||
go func() {
|
||||
log.Infof("metrics server listen on %s", metricsServer.Address())
|
||||
if err := metricsServer.Run(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Warnf("metrics server exit with error: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// first login to frps
|
||||
svr.loopLoginUntilSuccess(10*time.Second, lo.FromPtr(svr.common.LoginFailExit))
|
||||
if svr.ctl == nil {
|
||||
@@ -497,6 +522,10 @@ func (svr *Service) stop() {
|
||||
svr.webServer.Close()
|
||||
svr.webServer = nil
|
||||
}
|
||||
if svr.metricsServer != nil {
|
||||
svr.metricsServer.Close()
|
||||
svr.metricsServer = nil
|
||||
}
|
||||
if svr.vnetController != nil {
|
||||
_ = svr.vnetController.Stop()
|
||||
svr.vnetController = nil
|
||||
|
||||
Reference in New Issue
Block a user