@@ -26,8 +26,6 @@ import (
2626 "github.com/daeuniverse/outbound/protocol/direct"
2727 "gopkg.in/natefinch/lumberjack.v2"
2828
29- _ "net/http/pprof"
30-
3129 "github.com/daeuniverse/dae/cmd/internal"
3230 "github.com/daeuniverse/dae/common"
3331 "github.com/daeuniverse/dae/common/consts"
@@ -37,6 +35,7 @@ import (
3735 "github.com/daeuniverse/dae/control"
3836 "github.com/daeuniverse/dae/pkg/config_parser"
3937 "github.com/daeuniverse/dae/pkg/logger"
38+ "github.com/daeuniverse/dae/pkg/metrics"
4039 "github.com/mohae/deepcopy"
4140 "github.com/okzk/sdnotify"
4241 "github.com/sirupsen/logrus"
@@ -134,12 +133,25 @@ func Run(log *logrus.Logger, conf *config.Config, externGeoDataDirs []string) (e
134133 return err
135134 }
136135
137- var pprofServer * http.Server
138- if conf .Global .PprofPort != 0 {
139- pprofAddr := fmt .Sprintf ("localhost:%d" , conf .Global .PprofPort )
140- pprofServer = & http.Server {Addr : pprofAddr , Handler : nil }
141- go pprofServer .ListenAndServe ()
136+ metricsState := metrics .NewState ()
137+ metricsState .SetControlPlane (c )
138+ metricsRegistry := metrics .NewRegistry (metricsState )
139+
140+ var endpointServer * http.Server
141+ startEndpointServer := func (cfg metrics.EndpointConfig ) {
142+ if cfg .ListenAddress == "" {
143+ endpointServer = nil
144+ return
145+ }
146+ endpointServer = metrics .NewEndpointServer (cfg , metricsRegistry )
147+ go func (server * http.Server , endpointCfg metrics.EndpointConfig ) {
148+ if e := metrics .StartEndpointServer (server , endpointCfg ); e != nil && ! errors .Is (e , http .ErrServerClosed ) {
149+ log .WithError (e ).Errorln ("Endpoint server stopped with error" )
150+ }
151+ }(endpointServer , cfg )
142152 }
153+ endpointCfg := endpointConfigFromGlobal (conf , log )
154+ startEndpointServer (endpointCfg )
143155
144156 // Serve tproxy TCP/UDP server util signals.
145157 var listener * control.Listener
@@ -263,7 +275,7 @@ loop:
263275 if err := c .StopDNSListener (); err != nil {
264276 log .Warnf ("[Reload] Failed to stop old DNS listener: %v" , err )
265277 }
266-
278+
267279 log .Warnln ("[Reload] Load new control plane" )
268280 newC , err := newControlPlane (log , obj , dnsCache , newConf , externGeoDataDirs )
269281 if err != nil {
@@ -295,21 +307,21 @@ loop:
295307 c = newC
296308 conf = newConf
297309 reloading = true
310+ metricsState .SetControlPlane (newC )
298311
299312 // Ready to close.
300313 if abortConnections {
301314 oldC .AbortConnections ()
302315 }
303316 oldC .Close ()
304317
305- if pprofServer != nil {
306- pprofServer .Shutdown (context .Background ())
307- pprofServer = nil
308- }
309- if newConf .Global .PprofPort != 0 {
310- pprofAddr := fmt .Sprintf ("localhost:%d" , conf .Global .PprofPort )
311- pprofServer = & http.Server {Addr : pprofAddr , Handler : nil }
312- go pprofServer .ListenAndServe ()
318+ newEndpointCfg := endpointConfigFromGlobal (newConf , log )
319+ if endpointConfigChanged (endpointCfg , newEndpointCfg ) {
320+ if endpointServer != nil {
321+ _ = endpointServer .Shutdown (context .Background ())
322+ }
323+ endpointCfg = newEndpointCfg
324+ startEndpointServer (endpointCfg )
313325 }
314326 case syscall .SIGHUP :
315327 // Ignore.
@@ -321,12 +333,37 @@ loop:
321333 }
322334 defer os .Remove (PidFilePath )
323335 defer control .GetDaeNetns ().Close ()
336+ if endpointServer != nil {
337+ _ = endpointServer .Shutdown (context .Background ())
338+ }
324339 if e := c .Close (); e != nil {
325340 return fmt .Errorf ("close control plane: %w" , e )
326341 }
327342 return nil
328343}
329344
345+ func endpointConfigFromGlobal (conf * config.Config , log * logrus.Logger ) metrics.EndpointConfig {
346+ cfg := metrics.EndpointConfig {
347+ ListenAddress : conf .Global .EndpointListenAddress ,
348+ Username : conf .Global .EndpointUsername ,
349+ Password : conf .Global .EndpointPassword ,
350+ TlsCertificate : conf .Global .EndpointTlsCertificate ,
351+ TlsKey : conf .Global .EndpointTlsKey ,
352+ PrometheusEnabled : conf .Global .EndpointPrometheusEnabled ,
353+ PrometheusPath : conf .Global .EndpointPrometheusPath ,
354+ PprofEnabled : conf .Global .PprofPort != 0 ,
355+ }
356+ if cfg .ListenAddress == "" && conf .Global .PprofPort != 0 {
357+ log .Warnln ("pprof_port is deprecated, please use endpoint_listen_address instead" )
358+ cfg .ListenAddress = fmt .Sprintf ("localhost:%d" , conf .Global .PprofPort )
359+ }
360+ return cfg
361+ }
362+
363+ func endpointConfigChanged (a , b metrics.EndpointConfig ) bool {
364+ return a != b
365+ }
366+
330367func newControlPlane (log * logrus.Logger , bpf interface {}, dnsCache map [string ]* control.DnsCache , conf * config.Config , externGeoDataDirs []string ) (c * control.ControlPlane , err error ) {
331368 // Deep copy to prevent modification.
332369 conf = deepcopy .Copy (conf ).(* config.Config )
0 commit comments