Skip to content

Commit

Permalink
fix(Stats): restore stdout stats option, allow flush interval to be c…
Browse files Browse the repository at this point in the history
…onfigured with env vars (#549)

Signed-off-by: Zak Henry <[email protected]>
  • Loading branch information
zakhenry authored Apr 5, 2024
1 parent 247089f commit 7f35f22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ The rate limit service generates various statistics for each configured rate lim
users both for visibility and for setting alarms. Ratelimit uses [gostats](https://github.com/lyft/gostats) as its statistics library. Please refer
to [gostats' documentation](https://godoc.org/github.com/lyft/gostats) for more information on the library.

Statistics default to using [StatsD](https://github.com/statsd/statsd) and configured via the env vars from [gostats](https://github.com/lyft/gostats).

To output statistics to stdout instead, set env var `USE_STATSD` to `false`

Configure statistics output frequency with `STATS_FLUSH_INTERVAL`, where the type is `time.Duration`, e.g. `10s` is the default value.

To disable statistics entirely, set env var `DISABLE_STATS` to `true`

Rate Limit Statistic Path:

```
Expand Down
15 changes: 11 additions & 4 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ type Runner struct {

func NewRunner(s settings.Settings) Runner {
var store gostats.Store
// use statsd
if s.UseStatsd {

if s.DisableStats {
logger.Info("Stats disabled")
store = gostats.NewStore(gostats.NewNullSink(), false)
} else if s.UseStatsd {
logger.Info("Stats initialized for statsd")
store = gostats.NewStore(gostats.NewTCPStatsdSink(gostats.WithStatsdHost(s.StatsdHost), gostats.WithStatsdPort(s.StatsdPort)), false)
} else {
store = gostats.NewStore(gostats.NewNullSink(), false)
logger.Info("Stats initialized for stdout")
store = gostats.NewStore(gostats.NewLoggingSink(), false)
}

go store.Start(time.NewTicker(10 * time.Second))
logger.Infof("Stats flush interval: %s", s.StatsFlushInterval)

go store.Start(time.NewTicker(s.StatsFlushInterval))

return Runner{
statsManager: stats.NewStatManager(store, s),
Expand Down
10 changes: 6 additions & 4 deletions src/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ type Settings struct {
XdsClientBackoffJitter bool `envconfig:"XDS_CLIENT_BACKOFF_JITTER" default:"true"`

// Stats-related settings
UseStatsd bool `envconfig:"USE_STATSD" default:"true"`
StatsdHost string `envconfig:"STATSD_HOST" default:"localhost"`
StatsdPort int `envconfig:"STATSD_PORT" default:"8125"`
ExtraTags map[string]string `envconfig:"EXTRA_TAGS" default:""`
UseStatsd bool `envconfig:"USE_STATSD" default:"true"`
StatsdHost string `envconfig:"STATSD_HOST" default:"localhost"`
StatsdPort int `envconfig:"STATSD_PORT" default:"8125"`
ExtraTags map[string]string `envconfig:"EXTRA_TAGS" default:""`
StatsFlushInterval time.Duration `envconfig:"STATS_FLUSH_INTERVAL" default:"10s"`
DisableStats bool `envconfig:"DISABLE_STATS" default:"false"`

// Settings for rate limit configuration
RuntimePath string `envconfig:"RUNTIME_ROOT" default:"/srv/runtime_data/current"`
Expand Down

0 comments on commit 7f35f22

Please sign in to comment.