Skip to content

Commit

Permalink
Don’t leak metrics go routines in tests (hashicorp#8182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeler authored Jun 24, 2020
1 parent 0db4cb3 commit e2cfa93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
31 changes: 21 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,25 @@ type Agent struct {
}

type agentOptions struct {
logger hclog.InterceptLogger
builderOpts config.BuilderOpts
ui cli.Ui
config *config.RuntimeConfig
overrides []config.Source
writers []io.Writer
logger hclog.InterceptLogger
builderOpts config.BuilderOpts
ui cli.Ui
config *config.RuntimeConfig
overrides []config.Source
writers []io.Writer
initTelemetry bool
}

type AgentOption func(opt *agentOptions)

// WithTelemetry is used to control whether the agent will
// set up metrics.
func WithTelemetry(initTelemetry bool) AgentOption {
return func(opt *agentOptions) {
opt.initTelemetry = initTelemetry
}
}

// WithLogger is used to override any automatic logger creation
// and provide one already built instead. This is mostly useful
// for testing.
Expand Down Expand Up @@ -473,11 +482,13 @@ func New(options ...AgentOption) (*Agent, error) {
grpclog.SetLoggerV2(logging.NewGRPCLogger(logConf, a.logger))
}

memSink, err := lib.InitTelemetry(config.Telemetry)
if err != nil {
return nil, fmt.Errorf("Failed to initialize telemetry: %w", err)
if flat.initTelemetry {
memSink, err := lib.InitTelemetry(config.Telemetry)
if err != nil {
return nil, fmt.Errorf("Failed to initialize telemetry: %w", err)
}
a.MemSink = memSink
}
a.MemSink = memSink

// TODO (autoconf) figure out how to let this setting be pushed down via autoconf
// right now it gets defaulted if unset so this check actually doesn't do much
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (c *cmd) run(args []string) int {
agent.WithBuilderOpts(c.flagArgs),
agent.WithCLI(c.UI),
agent.WithLogWriter(&logGate),
agent.WithTelemetry(true),
}

agent, err := agent.New(agentOptions...)
Expand Down

0 comments on commit e2cfa93

Please sign in to comment.