Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use internal http server not default mux #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,
}
}

Expand Down Expand Up @@ -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
})
}
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.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())
}
}()
Expand Down
Loading