agent: move telemetry out of the run method

pull/3108/head
Frank Schroeder 2017-06-02 15:30:03 +02:00 committed by Frank Schröder
parent 84f5e8c3ab
commit 11245e53a7
1 changed files with 113 additions and 90 deletions

View File

@ -520,81 +520,37 @@ func (cmd *Command) startupJoinWan(agent *Agent, cfg *Config) error {
return nil return nil
} }
func (cmd *Command) Run(args []string) int { func statsiteSink(config *Config, hostname string) (metrics.MetricSink, error) {
cmd.UI = &cli.PrefixedUi{ if config.Telemetry.StatsiteAddr == "" {
OutputPrefix: "==> ", return nil, nil
InfoPrefix: " ", }
ErrorPrefix: "==> ", return metrics.NewStatsiteSink(config.Telemetry.StatsiteAddr)
Ui: cmd.UI,
} }
// Parse our configs func statsdSink(config *Config, hostname string) (metrics.MetricSink, error) {
cmd.args = args if config.Telemetry.StatsdAddr == "" {
config := cmd.readConfig() return nil, nil
if config == nil { }
return 1 return metrics.NewStatsdSink(config.Telemetry.StatsdAddr)
} }
// Setup the log outputs func dogstatdSink(config *Config, hostname string) (metrics.MetricSink, error) {
logConfig := &logger.Config{ if config.Telemetry.DogStatsdAddr == "" {
LogLevel: config.LogLevel, return nil, nil
EnableSyslog: config.EnableSyslog,
SyslogFacility: config.SyslogFacility,
} }
logFilter, logGate, logWriter, logOutput, ok := logger.Setup(logConfig, cmd.UI) sink, err := datadog.NewDogStatsdSink(config.Telemetry.DogStatsdAddr, hostname)
if !ok {
return 1
}
cmd.logFilter = logFilter
cmd.logOutput = logOutput
// Setup telemetry
// Aggregate on 10 second intervals for 1 minute. Expose the
// metrics over stderr when there is a SIGUSR1 received.
inm := metrics.NewInmemSink(10*time.Second, time.Minute)
metrics.DefaultInmemSignal(inm)
metricsConf := metrics.DefaultConfig(config.Telemetry.StatsitePrefix)
metricsConf.EnableHostname = !config.Telemetry.DisableHostname
// Configure the statsite sink
var fanout metrics.FanoutSink
if config.Telemetry.StatsiteAddr != "" {
sink, err := metrics.NewStatsiteSink(config.Telemetry.StatsiteAddr)
if err != nil { if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to start statsite sink. Got: %s", err)) return nil, err
return 1
} }
fanout = append(fanout, sink) sink.SetTags(config.Telemetry.DogStatsdTags)
return sink, nil
} }
// Configure the statsd sink func circonusSink(config *Config, hostname string) (metrics.MetricSink, error) {
if config.Telemetry.StatsdAddr != "" { if config.Telemetry.CirconusAPIToken == "" && config.Telemetry.CirconusCheckSubmissionURL == "" {
sink, err := metrics.NewStatsdSink(config.Telemetry.StatsdAddr) return nil, nil
if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to start statsd sink. Got: %s", err))
return 1
}
fanout = append(fanout, sink)
} }
// Configure the DogStatsd sink
if config.Telemetry.DogStatsdAddr != "" {
var tags []string
if config.Telemetry.DogStatsdTags != nil {
tags = config.Telemetry.DogStatsdTags
}
sink, err := datadog.NewDogStatsdSink(config.Telemetry.DogStatsdAddr, metricsConf.HostName)
if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to start DogStatsd sink. Got: %s", err))
return 1
}
sink.SetTags(tags)
fanout = append(fanout, sink)
}
if config.Telemetry.CirconusAPIToken != "" || config.Telemetry.CirconusCheckSubmissionURL != "" {
cfg := &circonus.Config{} cfg := &circonus.Config{}
cfg.Interval = config.Telemetry.CirconusSubmissionInterval cfg.Interval = config.Telemetry.CirconusSubmissionInterval
cfg.CheckManager.API.TokenKey = config.Telemetry.CirconusAPIToken cfg.CheckManager.API.TokenKey = config.Telemetry.CirconusAPIToken
@ -624,20 +580,87 @@ func (cmd *Command) Run(args []string) int {
sink, err := circonus.NewCirconusSink(cfg) sink, err := circonus.NewCirconusSink(cfg)
if err != nil { if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to start Circonus sink. Got: %s", err)) return nil, err
return 1
} }
sink.Start() sink.Start()
fanout = append(fanout, sink) return sink, nil
} }
// Initialize the global sink func startupTelemetry(config *Config) error {
if len(fanout) > 0 { // Setup telemetry
fanout = append(fanout, inm) // Aggregate on 10 second intervals for 1 minute. Expose the
metrics.NewGlobal(metricsConf, fanout) // metrics over stderr when there is a SIGUSR1 received.
memSink := metrics.NewInmemSink(10*time.Second, time.Minute)
metrics.DefaultInmemSignal(memSink)
metricsConf := metrics.DefaultConfig(config.Telemetry.StatsitePrefix)
metricsConf.EnableHostname = !config.Telemetry.DisableHostname
var sinks metrics.FanoutSink
addSink := func(name string, fn func(*Config, string) (metrics.MetricSink, error)) error {
s, err := fn(config, metricsConf.HostName)
if err != nil {
return err
}
if s != nil {
sinks = append(sinks, s)
}
return nil
}
if err := addSink("statsite", statsiteSink); err != nil {
return err
}
if err := addSink("statsd", statsdSink); err != nil {
return err
}
if err := addSink("dogstatd", dogstatdSink); err != nil {
return err
}
if err := addSink("circonus", circonusSink); err != nil {
return err
}
if len(sinks) > 0 {
sinks = append(sinks, memSink)
metrics.NewGlobal(metricsConf, sinks)
} else { } else {
metricsConf.EnableHostname = false metricsConf.EnableHostname = false
metrics.NewGlobal(metricsConf, inm) metrics.NewGlobal(metricsConf, memSink)
}
return nil
}
func (cmd *Command) Run(args []string) int {
cmd.UI = &cli.PrefixedUi{
OutputPrefix: "==> ",
InfoPrefix: " ",
ErrorPrefix: "==> ",
Ui: cmd.UI,
}
// Parse our configs
cmd.args = args
config := cmd.readConfig()
if config == nil {
return 1
}
// Setup the log outputs
logConfig := &logger.Config{
LogLevel: config.LogLevel,
EnableSyslog: config.EnableSyslog,
SyslogFacility: config.SyslogFacility,
}
logFilter, logGate, logWriter, logOutput, ok := logger.Setup(logConfig, cmd.UI)
if !ok {
return 1
}
cmd.logFilter = logFilter
cmd.logOutput = logOutput
if err := startupTelemetry(config); err != nil {
cmd.UI.Error(err.Error())
return 1
} }
// Create the agent // Create the agent