diff --git a/options.go b/options.go index 08c3d13..833ad5b 100644 --- a/options.go +++ b/options.go @@ -35,19 +35,21 @@ func (fn option) apply(cfg *config) { } type config struct { - buckets []float64 - enableGoCollector bool - registry *prom.Registry - runtimeMetricRules []collectors.GoRuntimeMetricsRule - disableServer 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, + buckets: defaultBuckets, + enableGoCollector: false, + registry: prom.NewRegistry(), + disableServer: false, + useDefaultMuxServer: true, } } @@ -89,3 +91,10 @@ func WithRegistry(registry *prom.Registry) Option { } }) } + +// WithDefaultMuxServer use default http mux server +func WithDefaultMuxServer(useDefault bool) Option { + return option(func(cfg *config) { + cfg.useDefaultMuxServer = useDefault + }) +} diff --git a/trace.go b/trace.go index ed6ba64..305a6d4 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.useDefaultMuxServer { + 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()) } }()