mirror of https://github.com/hashicorp/consul
agent: move telemetry out of the run method
parent
84f5e8c3ab
commit
11245e53a7
|
@ -520,81 +520,37 @@ func (cmd *Command) startupJoinWan(agent *Agent, cfg *Config) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (cmd *Command) Run(args []string) int {
|
||||
cmd.UI = &cli.PrefixedUi{
|
||||
OutputPrefix: "==> ",
|
||||
InfoPrefix: " ",
|
||||
ErrorPrefix: "==> ",
|
||||
Ui: cmd.UI,
|
||||
func statsiteSink(config *Config, hostname string) (metrics.MetricSink, error) {
|
||||
if config.Telemetry.StatsiteAddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return metrics.NewStatsiteSink(config.Telemetry.StatsiteAddr)
|
||||
}
|
||||
|
||||
// Parse our configs
|
||||
cmd.args = args
|
||||
config := cmd.readConfig()
|
||||
if config == nil {
|
||||
return 1
|
||||
func statsdSink(config *Config, hostname string) (metrics.MetricSink, error) {
|
||||
if config.Telemetry.StatsdAddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return metrics.NewStatsdSink(config.Telemetry.StatsdAddr)
|
||||
}
|
||||
|
||||
// Setup the log outputs
|
||||
logConfig := &logger.Config{
|
||||
LogLevel: config.LogLevel,
|
||||
EnableSyslog: config.EnableSyslog,
|
||||
SyslogFacility: config.SyslogFacility,
|
||||
func dogstatdSink(config *Config, hostname string) (metrics.MetricSink, error) {
|
||||
if config.Telemetry.DogStatsdAddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
logFilter, logGate, logWriter, logOutput, ok := logger.Setup(logConfig, cmd.UI)
|
||||
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)
|
||||
sink, err := datadog.NewDogStatsdSink(config.Telemetry.DogStatsdAddr, hostname)
|
||||
if err != nil {
|
||||
cmd.UI.Error(fmt.Sprintf("Failed to start statsite sink. Got: %s", err))
|
||||
return 1
|
||||
return nil, err
|
||||
}
|
||||
fanout = append(fanout, sink)
|
||||
sink.SetTags(config.Telemetry.DogStatsdTags)
|
||||
return sink, nil
|
||||
}
|
||||
|
||||
// Configure the statsd sink
|
||||
if config.Telemetry.StatsdAddr != "" {
|
||||
sink, err := metrics.NewStatsdSink(config.Telemetry.StatsdAddr)
|
||||
if err != nil {
|
||||
cmd.UI.Error(fmt.Sprintf("Failed to start statsd sink. Got: %s", err))
|
||||
return 1
|
||||
}
|
||||
fanout = append(fanout, sink)
|
||||
func circonusSink(config *Config, hostname string) (metrics.MetricSink, error) {
|
||||
if config.Telemetry.CirconusAPIToken == "" && config.Telemetry.CirconusCheckSubmissionURL == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 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.Interval = config.Telemetry.CirconusSubmissionInterval
|
||||
cfg.CheckManager.API.TokenKey = config.Telemetry.CirconusAPIToken
|
||||
|
@ -624,20 +580,87 @@ func (cmd *Command) Run(args []string) int {
|
|||
|
||||
sink, err := circonus.NewCirconusSink(cfg)
|
||||
if err != nil {
|
||||
cmd.UI.Error(fmt.Sprintf("Failed to start Circonus sink. Got: %s", err))
|
||||
return 1
|
||||
return nil, err
|
||||
}
|
||||
sink.Start()
|
||||
fanout = append(fanout, sink)
|
||||
return sink, nil
|
||||
}
|
||||
|
||||
// Initialize the global sink
|
||||
if len(fanout) > 0 {
|
||||
fanout = append(fanout, inm)
|
||||
metrics.NewGlobal(metricsConf, fanout)
|
||||
func startupTelemetry(config *Config) error {
|
||||
// Setup telemetry
|
||||
// Aggregate on 10 second intervals for 1 minute. Expose the
|
||||
// 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 {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue