Skip to content

Commit 1b4df80

Browse files
authored
feat: make Router.enableTracing configurable (#338)
* feat: make Router.enableTracing configurable * feat: flip bool to keep things unchanged
1 parent f937c78 commit 1b4df80

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

server/server.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ type Server struct {
3131
doneOnce sync.Once
3232
}
3333

34+
type RouterConfig struct {
35+
DisableTracing bool
36+
}
37+
3438
type Config struct {
3539
HealthPath string `split_words:"true"`
3640
Port int
3741
Host string
3842
TLS nconf.TLSConfig
43+
Router RouterConfig
3944
}
4045

4146
// APIDefinition is used to control lifecycle of the API
@@ -67,7 +72,9 @@ func NewOpts(log logrus.FieldLogger, api APIDefinition, opts ...Opt) (*Server, e
6772
WithHostAndPort("", defaultPort),
6873
}
6974

70-
return buildServer(log, api, append(defaultOpts, opts...), defaultHealthPath)
75+
return buildServer(log, api, append(defaultOpts, opts...), Config{
76+
HealthPath: defaultHealthPath,
77+
})
7178
}
7279

7380
// New will build a server with the defaults in place
@@ -85,7 +92,7 @@ func New(log logrus.FieldLogger, config Config, api APIDefinition) (*Server, err
8592
opts = append(opts, WithTLS(tcfg))
8693
}
8794

88-
return buildServer(log, api, opts, config.HealthPath)
95+
return buildServer(log, api, opts, config)
8996
}
9097

9198
func (s *Server) Shutdown(to time.Duration) error {
@@ -173,25 +180,32 @@ func APIFunc(start func(router.Router) error, stop func(), info APIInfo) APIDefi
173180
}
174181
}
175182

176-
func buildRouter(log logrus.FieldLogger, api APIDefinition, healthPath string) router.Router {
183+
func buildRouter(log logrus.FieldLogger, api APIDefinition, config Config) router.Router {
177184
var healthHandler router.APIHandler
178185
if checker, ok := api.(HealthChecker); ok {
179186
healthHandler = checker.Healthy
180187
}
181188

182-
r := router.New(
183-
log,
184-
router.OptHealthCheck(healthPath, healthHandler),
185-
router.OptEnableTracing(api.Info().Name),
189+
opts := []router.Option{
190+
router.OptHealthCheck(config.HealthPath, healthHandler),
186191
router.OptVersionHeader(api.Info().Name, api.Info().Version),
187192
router.OptRecoverer(),
193+
}
194+
195+
if !config.Router.DisableTracing {
196+
opts = append(opts, router.OptEnableTracing(api.Info().Name))
197+
}
198+
199+
r := router.New(
200+
log,
201+
opts...,
188202
)
189203

190204
return r
191205
}
192206

193-
func buildServer(log logrus.FieldLogger, api APIDefinition, opts []Opt, healthPath string) (*Server, error) {
194-
r := buildRouter(log, api, healthPath)
207+
func buildServer(log logrus.FieldLogger, api APIDefinition, opts []Opt, config Config) (*Server, error) {
208+
r := buildRouter(log, api, config)
195209

196210
if err := api.Start(r); err != nil {
197211
return nil, errors.Wrap(err, "Failed to start API")

0 commit comments

Comments
 (0)