Don’t leak metrics go routines in tests (#8182)

This commit is contained in:
Matt Keeler 2020-06-24 10:15:25 -04:00 committed by GitHub
parent 0db4cb305f
commit e2cfa93f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 10 deletions

View File

@ -323,16 +323,25 @@ type Agent struct {
} }
type agentOptions struct { type agentOptions struct {
logger hclog.InterceptLogger logger hclog.InterceptLogger
builderOpts config.BuilderOpts builderOpts config.BuilderOpts
ui cli.Ui ui cli.Ui
config *config.RuntimeConfig config *config.RuntimeConfig
overrides []config.Source overrides []config.Source
writers []io.Writer writers []io.Writer
initTelemetry bool
} }
type AgentOption func(opt *agentOptions) 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 // WithLogger is used to override any automatic logger creation
// and provide one already built instead. This is mostly useful // and provide one already built instead. This is mostly useful
// for testing. // for testing.
@ -473,11 +482,13 @@ func New(options ...AgentOption) (*Agent, error) {
grpclog.SetLoggerV2(logging.NewGRPCLogger(logConf, a.logger)) grpclog.SetLoggerV2(logging.NewGRPCLogger(logConf, a.logger))
} }
memSink, err := lib.InitTelemetry(config.Telemetry) if flat.initTelemetry {
if err != nil { memSink, err := lib.InitTelemetry(config.Telemetry)
return nil, fmt.Errorf("Failed to initialize telemetry: %w", err) 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 // 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 // right now it gets defaulted if unset so this check actually doesn't do much

View File

@ -171,6 +171,7 @@ func (c *cmd) run(args []string) int {
agent.WithBuilderOpts(c.flagArgs), agent.WithBuilderOpts(c.flagArgs),
agent.WithCLI(c.UI), agent.WithCLI(c.UI),
agent.WithLogWriter(&logGate), agent.WithLogWriter(&logGate),
agent.WithTelemetry(true),
} }
agent, err := agent.New(agentOptions...) agent, err := agent.New(agentOptions...)