From 843ccffdcd325b8da3811c3bc41cb5aa85a7aab9 Mon Sep 17 00:00:00 2001 From: "zhangyibo.114514" Date: Fri, 17 Jan 2025 18:54:58 +0800 Subject: [PATCH 1/3] use internal http server not default mux --- options.go | 9 +++++++++ trace.go | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index 08c3d13..de80a08 100644 --- a/options.go +++ b/options.go @@ -40,6 +40,7 @@ type config struct { registry *prom.Registry runtimeMetricRules []collectors.GoRuntimeMetricsRule disableServer bool + useDefaultServer bool } func defaultConfig() *config { @@ -48,6 +49,7 @@ func defaultConfig() *config { enableGoCollector: false, registry: prom.NewRegistry(), disableServer: false, + useDefaultServer: true, } } @@ -89,3 +91,10 @@ func WithRegistry(registry *prom.Registry) Option { } }) } + +// WithDefaultServer use default http server +func WithDefaultServer() Option { + return option(func(cfg *config) { + cfg.useDefaultServer = true + }) +} diff --git a/trace.go b/trace.go index ed6ba64..cadcf86 100644 --- a/trace.go +++ b/trace.go @@ -84,9 +84,13 @@ func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer { } if !cfg.disableServer { - http.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})) + httpServer := http.DefaultServeMux + if !cfg.useDefaultServer { + httpServer = http.NewServeMux() + } + httpServer.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})) go func() { - if err := http.ListenAndServe(addr, nil); err != nil { + if err := http.ListenAndServe(addr, httpServer); err != nil { hlog.Fatal("HERTZ: Unable to start a promhttp server, err: " + err.Error()) } }() From 931ad4a7859cacd6a8c681815000ad732fc4b63f Mon Sep 17 00:00:00 2001 From: "zhangyibo.114514" Date: Fri, 17 Jan 2025 19:08:19 +0800 Subject: [PATCH 2/3] change config --- options.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index de80a08..b427311 100644 --- a/options.go +++ b/options.go @@ -93,8 +93,8 @@ func WithRegistry(registry *prom.Registry) Option { } // WithDefaultServer use default http server -func WithDefaultServer() Option { +func WithDefaultServer(useDefault bool) Option { return option(func(cfg *config) { - cfg.useDefaultServer = true + cfg.useDefaultServer = useDefault }) } From 8715456099991b59b3da4d02b372760d8e5ffb2e Mon Sep 17 00:00:00 2001 From: "zhangyibo.114514" Date: Mon, 20 Jan 2025 11:19:30 +0800 Subject: [PATCH 3/3] rename option --- options.go | 28 ++++++++++++++-------------- trace.go | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/options.go b/options.go index b427311..833ad5b 100644 --- a/options.go +++ b/options.go @@ -35,21 +35,21 @@ func (fn option) apply(cfg *config) { } type config struct { - buckets []float64 - enableGoCollector bool - registry *prom.Registry - runtimeMetricRules []collectors.GoRuntimeMetricsRule - disableServer bool - useDefaultServer bool + buckets []float64 + enableGoCollector bool + registry *prom.Registry + runtimeMetricRules []collectors.GoRuntimeMetricsRule + disableServer bool + useDefaultMuxServer bool } func defaultConfig() *config { return &config{ - buckets: defaultBuckets, - enableGoCollector: false, - registry: prom.NewRegistry(), - disableServer: false, - useDefaultServer: true, + buckets: defaultBuckets, + enableGoCollector: false, + registry: prom.NewRegistry(), + disableServer: false, + useDefaultMuxServer: true, } } @@ -92,9 +92,9 @@ func WithRegistry(registry *prom.Registry) Option { }) } -// WithDefaultServer use default http server -func WithDefaultServer(useDefault bool) Option { +// WithDefaultMuxServer use default http mux server +func WithDefaultMuxServer(useDefault bool) Option { return option(func(cfg *config) { - cfg.useDefaultServer = useDefault + cfg.useDefaultMuxServer = useDefault }) } diff --git a/trace.go b/trace.go index cadcf86..305a6d4 100644 --- a/trace.go +++ b/trace.go @@ -85,7 +85,7 @@ func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer { if !cfg.disableServer { httpServer := http.DefaultServeMux - if !cfg.useDefaultServer { + if !cfg.useDefaultMuxServer { httpServer = http.NewServeMux() } httpServer.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))