Skip to content

Commit

Permalink
clair: better introspection server defaults
Browse files Browse the repository at this point in the history
This change has the introspection server spin up on a random port if an
address is not provided, instead of the default of ":http".

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Mar 6, 2020
1 parent 9828ed3 commit 8cbddd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
21 changes: 9 additions & 12 deletions cmd/clair/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@ const (

func introspection(ctx context.Context, cfg *config.Config, healthCheck func() bool) (*http.Server, error) {
mux := http.NewServeMux()
srv := http.Server{
Addr: cfg.IntrospectionAddr,
}
srv := http.Server{}
log := zerolog.Ctx(ctx).With().Logger()

// Metrics config
if n := cfg.Metrics.Name; n != "" {
log.Info().Str("sink", n).Msg("configuring metrics sink")
if cfg.Metrics.Name == "" {
cfg.Metrics.Name = "default"
}
log.Info().Str("sink", cfg.Metrics.Name).Msg("configuring metrics sink")
switch cfg.Metrics.Name {
case "":
log.Info().Str("sink", "default").Msg("configuring metrics sink")
fallthrough // will default to prometheus
case "default":
log.Info().Str("sink", "default").Msg("defaulting to prometheus")
fallthrough
case "prometheus":
log.Info().Str("sink", "prometheus").Msg("configuring metrics sink")
endpoint := "/metrics"
if cfg.Metrics.Prometheus.Endpoint != nil {
endpoint = *cfg.Metrics.Prometheus.Endpoint
}
log.Info().
Str("url", srv.Addr+endpoint).
Msg("configuring prometheus")
Str("endpoint", endpoint).
Msg("configuring prometheus on introspection server")
promlog := log.With().
Str("component", "metrics-exporter").
Logger()
Expand All @@ -61,7 +59,6 @@ func introspection(ctx context.Context, cfg *config.Config, healthCheck func() b
srv.RegisterOnShutdown(pipeline.Stop)
mux.HandleFunc(endpoint, hf)
case "dogstatsd":
log.Info().Str("sink", "dogstatsd").Msg("configuring metrics sink")
log.Info().
Str("endpoint", cfg.Metrics.Dogstatsd.URL).
Msg("configuring dogstatsd")
Expand Down
32 changes: 23 additions & 9 deletions cmd/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,34 @@ func main() {

// Make sure to configure our metrics and tracing providers before creating
// any package objects that may close over a provider.
//
// This is structured in a non-idiomatic way because we don't want a failure
// to stop the program.
intro, err := introspection(ctx, &conf, func() bool { return true })
if err != nil {
logger.Error().Err(err).Msg("failed to create introspection server")
} else {
intro.BaseContext = logfunc
logger.Info().
Str("addr", intro.Addr).
Msgf("launching introspection via http")
go func() {
if err := intro.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error().Err(err).Msg("launching introspection via http failed")
}
defer intro.Shutdown(ctx)
}()
addr := conf.IntrospectionAddr
if addr == "" {
addr = ":0"
}
l, err := net.Listen("tcp", addr)
if err != nil {
l.Close()
logger.Error().Err(err).Msg("failed to create introspection server")
} else {
logger.Info().
Str("addr", l.Addr().String()).
Msgf("launching introspection via http")
// The Serve method closes the net.Listener.
go func() {
if err := intro.Serve(l); err != nil && err != http.ErrServerClosed {
logger.Error().Err(err).Msg("launching introspection via http failed")
}
defer intro.Shutdown(ctx)
}()
}
}

// return a http server with the correct handlers given the config's Mode attribute.
Expand Down

0 comments on commit 8cbddd1

Please sign in to comment.