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

@ -329,10 +329,19 @@ type agentOptions struct {
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.
@ -473,11 +482,13 @@ func New(options ...AgentOption) (*Agent, error) {
grpclog.SetLoggerV2(logging.NewGRPCLogger(logConf, a.logger))
}
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
}
// 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

View File

@ -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...)