Skip to content

Commit

Permalink
feat: use internal http server not default mux (#13)
Browse files Browse the repository at this point in the history
* use internal http server not default mux

* change config

* rename option

* rename option
  • Loading branch information
Gaojianli authored Jan 22, 2025
1 parent 5297e2a commit 44ddd1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 18 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
useDefaultServerMux bool
}

func defaultConfig() *config {
return &config{
buckets: defaultBuckets,
enableGoCollector: false,
registry: prom.NewRegistry(),
disableServer: false,
buckets: defaultBuckets,
enableGoCollector: false,
registry: prom.NewRegistry(),
disableServer: false,
useDefaultServerMux: true,
}
}

Expand Down Expand Up @@ -89,3 +91,10 @@ func WithRegistry(registry *prom.Registry) Option {
}
})
}

// WithDefaultServerMux use http.DefaultServeMux
func WithDefaultServerMux(useDefault bool) Option {
return option(func(cfg *config) {
cfg.useDefaultServerMux = useDefault
})
}
8 changes: 6 additions & 2 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.useDefaultServerMux {
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())
}
}()
Expand Down

0 comments on commit 44ddd1d

Please sign in to comment.