Skip to content

Commit

Permalink
agent: disable metrics server by default
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed Sep 4, 2024
1 parent 685afd2 commit 6216c98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
16 changes: 16 additions & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,34 @@ reconnect.`,
}

type ServerConfig struct {
// Enabled indicates whether to enable the agent metrics server.
Enabled bool `json:"enabled" yaml:"enabled"`

// BindAddr is the address to bind to listen for incoming HTTP connections.
BindAddr string `json:"bind_addr" yaml:"bind_addr"`
}

func (c *ServerConfig) Validate() error {
if !c.Enabled {
return nil
}
if c.BindAddr == "" {
return fmt.Errorf("missing bind addr")
}
return nil
}

func (c *ServerConfig) RegisterFlags(fs *pflag.FlagSet) {
fs.BoolVar(
&c.Enabled,
"server.enabled",
c.Enabled,
`
Whether to enable the agent metrics server.
Disabled by default.`,
)

fs.StringVar(
&c.BindAddr,
"server.bind-addr",
Expand Down
44 changes: 23 additions & 21 deletions cli/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,29 @@ func runAgent(conf *config.Config, logger log.Logger) error {
}

// Agent server.
serverLn, err := net.Listen("tcp", conf.Server.BindAddr)
if err != nil {
return fmt.Errorf("server listen: %s: %w", conf.Server.BindAddr, err)
}
server := server.NewServer(registry, logger)

group.Add(func() error {
if err := server.Serve(serverLn); err != nil {
return fmt.Errorf("agent server: %w", err)
}
return nil
}, func(error) {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), conf.GracePeriod,
)
defer cancel()

if err := server.Shutdown(shutdownCtx); err != nil {
logger.Warn("failed to gracefully shutdown agent server", zap.Error(err))
}
})
if conf.Server.Enabled {

Check failure on line 177 in cli/agent/command.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
serverLn, err := net.Listen("tcp", conf.Server.BindAddr)
if err != nil {
return fmt.Errorf("server listen: %s: %w", conf.Server.BindAddr, err)
}
server := server.NewServer(registry, logger)

group.Add(func() error {

Check failure on line 184 in cli/agent/command.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
if err := server.Serve(serverLn); err != nil {
return fmt.Errorf("agent server: %w", err)
}
return nil
}, func(error) {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), conf.GracePeriod,
)
defer cancel()

if err := server.Shutdown(shutdownCtx); err != nil {
logger.Warn("failed to gracefully shutdown agent server", zap.Error(err))
}
})
}

// Termination handler.
signalCtx, signalCancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 6216c98

Please sign in to comment.