Skip to content

Commit

Permalink
Set Namespace and GlobalTags for aggregated metrics too
Browse files Browse the repository at this point in the history
  • Loading branch information
hush-hush committed Mar 18, 2020
1 parent c4733ba commit c1804f4
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,24 +437,12 @@ func (c *Client) FlushTelemetryMetrics() ClientMetrics {
}
}

func (c *Client) globalTags() []string {
if c != nil {
return c.Tags
}
return nil
}

func (c *Client) namespace() string {
if c != nil {
return c.Namespace
}
return ""
}

func (c *Client) addMetric(m metric) error {
if c != nil {
atomic.AddUint64(&c.metrics.TotalMetrics, 1)
if c == nil {
return ErrNoClient
}
atomic.AddUint64(&c.metrics.TotalMetrics, 1)
m.namespace = c.Namespace
return c.send(m)
}

Expand All @@ -463,6 +451,8 @@ func (c *Client) send(m metric) error {
return ErrNoClient
}

m.globalTags = c.Tags

h := hashString32(m.name)
worker := c.bufferShards[h%uint32(len(c.bufferShards))]

Expand All @@ -482,25 +472,25 @@ func (c *Client) Gauge(name string, value float64, tags []string, rate float64)
if c.agg != nil {
return c.agg.gauge(name, value, tags, rate)
}
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: gauge, name: name, fvalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: gauge, name: name, fvalue: value, tags: tags, rate: rate})
}

// Count tracks how many times something happened per second.
func (c *Client) Count(name string, value int64, tags []string, rate float64) error {
if c.agg != nil {
return c.agg.count(name, value, tags, rate)
}
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: count, name: name, ivalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: count, name: name, ivalue: value, tags: tags, rate: rate})
}

// Histogram tracks the statistical distribution of a set of values on each host.
func (c *Client) Histogram(name string, value float64, tags []string, rate float64) error {
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: histogram, name: name, fvalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: histogram, name: name, fvalue: value, tags: tags, rate: rate})
}

// Distribution tracks the statistical distribution of a set of values across your infrastructure.
func (c *Client) Distribution(name string, value float64, tags []string, rate float64) error {
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: distribution, name: name, fvalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: distribution, name: name, fvalue: value, tags: tags, rate: rate})
}

// Decr is just Count of -1
Expand All @@ -518,7 +508,7 @@ func (c *Client) Set(name string, value string, tags []string, rate float64) err
if c.agg != nil {
return c.agg.set(name, value, tags, rate)
}
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: set, name: name, svalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: set, name: name, svalue: value, tags: tags, rate: rate})
}

// Timing sends timing information, it is an alias for TimeInMilliseconds
Expand All @@ -529,15 +519,16 @@ func (c *Client) Timing(name string, value time.Duration, tags []string, rate fl
// TimeInMilliseconds sends timing information in milliseconds.
// It is flushed by statsd with percentiles, mean and other info (https://github.com/etsy/statsd/blob/master/docs/metric_types.md#timing)
func (c *Client) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error {
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: timing, name: name, fvalue: value, tags: tags, rate: rate})
return c.addMetric(metric{metricType: timing, name: name, fvalue: value, tags: tags, rate: rate})
}

// Event sends the provided Event.
func (c *Client) Event(e *Event) error {
if c != nil {
atomic.AddUint64(&c.metrics.TotalEvents, 1)
if c == nil {
return ErrNoClient
}
return c.send(metric{globalTags: c.globalTags(), metricType: event, evalue: e, rate: 1})
atomic.AddUint64(&c.metrics.TotalEvents, 1)
return c.send(metric{metricType: event, evalue: e, rate: 1})
}

// SimpleEvent sends an event with the provided title and text.
Expand All @@ -548,10 +539,11 @@ func (c *Client) SimpleEvent(title, text string) error {

// ServiceCheck sends the provided ServiceCheck.
func (c *Client) ServiceCheck(sc *ServiceCheck) error {
if c != nil {
atomic.AddUint64(&c.metrics.TotalServiceChecks, 1)
if c == nil {
return ErrNoClient
}
return c.send(metric{globalTags: c.globalTags(), metricType: serviceCheck, scvalue: sc, rate: 1})
atomic.AddUint64(&c.metrics.TotalServiceChecks, 1)
return c.send(metric{metricType: serviceCheck, scvalue: sc, rate: 1})
}

// SimpleServiceCheck sends an serviceCheck with the provided name and status.
Expand Down

0 comments on commit c1804f4

Please sign in to comment.