Skip to content

Commit

Permalink
Add setting for disabling logging sink (lyft#94)
Browse files Browse the repository at this point in the history
* Add setting for disabling logging sink

* Default to current behavior

* Update settings.go

Co-Authored-By: Charlie Vieth <[email protected]>

Co-authored-by: Charlie Vieth <[email protected]>
  • Loading branch information
Keyan Pishdadian and charlievieth authored Apr 22, 2020
1 parent 02891db commit cf86465
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
20 changes: 15 additions & 5 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
DefaultStatsdPort = 8125
// DefaultFlushIntervalS is the default flushing interval in seconds.
DefaultFlushIntervalS = 5
// DefaultLoggingSinkDisabled is the default behavior of logging sink suppression, default is false.
DefaultLoggingSinkDisabled = false
)

// The Settings type is used to configure gostats. gostats uses environment
Expand All @@ -32,6 +34,9 @@ type Settings struct {
StatsdPort int `envconfig:"STATSD_PORT" default:"8125"`
// Flushing interval.
FlushIntervalS int `envconfig:"GOSTATS_FLUSH_INTERVAL_SECONDS" default:"5"`
// Disable the LoggingSink when USE_STATSD is false and use the NullSink instead.
// This will cause all stats to be silently dropped.
LoggingSinkDisabled bool `envconfig:"GOSTATS_LOGGING_SINK_DISABLED" default:"false"`
}

// An envError is an error that occured parsing an environment variable
Expand Down Expand Up @@ -91,11 +96,16 @@ func GetSettings() Settings {
if err != nil {
panic(err)
}
loggingSinkDisabled, err := envBool("GOSTATS_LOGGING_SINK_DISABLED", DefaultLoggingSinkDisabled)
if err != nil {
panic(err)
}
return Settings{
UseStatsd: useStatsd,
StatsdHost: envOr("STATSD_HOST", DefaultStatsdHost),
StatsdProtocol: envOr("STATSD_PROTOCOL", DefaultStatsdProtocol),
StatsdPort: statsdPort,
FlushIntervalS: flushIntervalS,
UseStatsd: useStatsd,
StatsdHost: envOr("STATSD_HOST", DefaultStatsdHost),
StatsdProtocol: envOr("STATSD_PROTOCOL", DefaultStatsdProtocol),
StatsdPort: statsdPort,
FlushIntervalS: flushIntervalS,
LoggingSinkDisabled: loggingSinkDisabled,
}
}
26 changes: 16 additions & 10 deletions settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestSettingsCompat(t *testing.T) {
"STATSD_PROTOCOL", "",
"STATSD_PORT", "",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "",
"GOSTATS_LOGGING_SINK_DISABLED", "",
)
defer reset()

Expand All @@ -65,14 +66,16 @@ func TestSettingsDefault(t *testing.T) {
"STATSD_PROTOCOL", "",
"STATSD_PORT", "",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "",
"GOSTATS_LOGGING_SINK_DISABLED", "",
)
defer reset()
exp := Settings{
UseStatsd: DefaultUseStatsd,
StatsdHost: DefaultStatsdHost,
StatsdProtocol: DefaultStatsdProtocol,
StatsdPort: DefaultStatsdPort,
FlushIntervalS: DefaultFlushIntervalS,
UseStatsd: DefaultUseStatsd,
StatsdHost: DefaultStatsdHost,
StatsdProtocol: DefaultStatsdProtocol,
StatsdPort: DefaultStatsdPort,
FlushIntervalS: DefaultFlushIntervalS,
LoggingSinkDisabled: DefaultLoggingSinkDisabled,
}
settings := GetSettings()
if exp != settings {
Expand All @@ -87,14 +90,16 @@ func TestSettingsOverride(t *testing.T) {
"STATSD_PROTOCOL", "udp",
"STATSD_PORT", "1234",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "3",
"GOSTATS_LOGGING_SINK_DISABLED", "true",
)
defer reset()
exp := Settings{
UseStatsd: true,
StatsdHost: "10.0.0.1",
StatsdProtocol: "udp",
StatsdPort: 1234,
FlushIntervalS: 3,
UseStatsd: true,
StatsdHost: "10.0.0.1",
StatsdProtocol: "udp",
StatsdPort: 1234,
FlushIntervalS: 3,
LoggingSinkDisabled: true,
}
settings := GetSettings()
if exp != settings {
Expand All @@ -109,6 +114,7 @@ func TestSettingsErrors(t *testing.T) {
"USE_STATSD": "FOO!",
"STATSD_PORT": "not-an-int",
"GOSTATS_FLUSH_INTERVAL_SECONDS": "true",
"GOSTATS_LOGGING_SINK_DISABLED": "1337",
}
for key, val := range tests {
t.Run(key, func(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ func NewDefaultStore() Store {
settings := GetSettings()
if !settings.UseStatsd {
logger.Warn("statsd is not in use")
newStore = NewStore(NewLoggingSink(), false)
if settings.LoggingSinkDisabled {
newStore = NewStore(NewNullSink(), false)
} else {
newStore = NewStore(NewLoggingSink(), false)
}
go newStore.Start(time.NewTicker(10 * time.Second))
} else {
newStore = NewStore(NewTCPStatsdSink(), false)
Expand Down

0 comments on commit cf86465

Please sign in to comment.