Skip to content

Commit

Permalink
upd: made timer value min and max configurable for more consistent ra…
Browse files Browse the repository at this point in the history
…nge control
  • Loading branch information
rizkybiz committed Jan 7, 2021
1 parent 697759f commit b5645df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 7 additions & 8 deletions agent.go
Expand Up @@ -19,11 +19,6 @@ import (
statsd "gopkg.in/alexcesaro/statsd.v2"
)

const (
timerSampleMax = 1000
timerSampleMin = 1
)

//AgentController is the main controller of the agents
type AgentController struct {
sig chan os.Signal
Expand All @@ -40,6 +35,8 @@ type Agent struct {
gaugeNames []string
timerNames []string
timerSamples int
timerValueMax int
timerValueMin int
statsdClients []*statsd.Client
}

Expand Down Expand Up @@ -104,7 +101,7 @@ func (ac *AgentController) Start(c config) error {
for i := 0; i < c.agents; i++ {
ac.wg.Add(1)
go func(id int) {
agent, err := CreateAgent(id, c.counters, c.gauges, c.timers, c.flushInterval, statsdClients, c.tags, c.tagFormat)
agent, err := CreateAgent(id, c.counters, c.gauges, c.timers, c.timerValueMax, c.timerValueMin, c.flushInterval, statsdClients, c.tags, c.tagFormat)
if err != nil {
log.Printf("error instantiating agent%d: %s\n", id, err)
ac.ctx.Done()
Expand All @@ -127,7 +124,7 @@ func (ac *AgentController) Start(c config) error {
}

//CreateAgent creates a new instance of an Agent
func CreateAgent(id, counters, gauges, timers int, flush time.Duration, targets []*statsd.Client, tags, tagFormat string) (*Agent, error) {
func CreateAgent(id, counters, gauges, timers, timerMax, timerMin int, flush time.Duration, targets []*statsd.Client, tags, tagFormat string) (*Agent, error) {

//Check the tagformat
if tagFormat != "" {
Expand Down Expand Up @@ -157,6 +154,8 @@ func CreateAgent(id, counters, gauges, timers int, flush time.Duration, targets
counterNames: genMetricsNames("counter", id, counters),
gaugeNames: genMetricsNames("gauge", id, gauges),
timerNames: genMetricsNames("timer", id, timers),
timerValueMax: timerMax,
timerValueMin: timerMin,
statsdClients: targets,
}
return a, nil
Expand Down Expand Up @@ -234,7 +233,7 @@ func (a *Agent) genGauges(ctx context.Context) {
func (a *Agent) genTimers(ctx context.Context) {
for _, name := range a.timerNames {
for i := 0; i < a.timerSamples; i++ {
val := rand.Float64() * (timerSampleMax - timerSampleMin)
val := rand.Float64() * (float64(a.timerValueMax) - float64(a.timerValueMin))
for _, c := range a.statsdClients {
c.Timing(name, val)
if a.done(ctx) {
Expand Down
10 changes: 6 additions & 4 deletions config.go
Expand Up @@ -15,16 +15,17 @@ import (
)

type config struct {
statsdHosts string
prefix string
// network string
statsdHosts string
prefix string
tags string
tagFormat string
flushInterval time.Duration
spawnDrift int
counters int
gauges int
timers int
timerValueMax int
timerValueMin int
tsamples int
agents int
version bool
Expand All @@ -42,10 +43,11 @@ func genConfig() config {
flag.StringVar(&c.prefix, "prefix", filepath.Base(defaultPrefix), "prefix for metrics")
flag.DurationVar(&c.flushInterval, "flush-interval", 10*time.Second, "how often to flush metrics")
flag.IntVar(&c.spawnDrift, "spawn-drift", 10, "spread new agent generation by 0-n seconds")
// flag.StringVar(&c.network, "protocol", "udp", "network protocol to use, tcp or udp")
flag.StringVar(&c.tagFormat, "tag-format", "", "format of the tags to send. accepted values \"datadog\" or \"influx\"")
flag.StringVar(&c.tags, "tags", "", "list of K:V comma separated tags. Example: key1:tag1,key2:tag2")
flag.IntVar(&c.counters, "counters", 50, "number of counters for each agent to hold")
flag.IntVar(&c.timerValueMax, "timer-value-max", 100, "max timer value")
flag.IntVar(&c.timerValueMin, "timer-value-min", 0, "min timer value")
flag.IntVar(&c.gauges, "gauges", 30, "number of gauges for each agent to hold")
flag.IntVar(&c.timers, "timers", 20, "number of timers for each agent to hold")
flag.IntVar(&c.tsamples, "timer-samples", 10, "number of timer samples per iteration")
Expand Down

0 comments on commit b5645df

Please sign in to comment.