Skip to content

Commit

Permalink
statsd: address review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
remeh committed Jul 29, 2022
1 parent 3529331 commit 87572b0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
3 changes: 0 additions & 3 deletions statsd/aggregator.go
Expand Up @@ -14,9 +14,6 @@ type (
bufferedMetricMap map[string]*bufferedMetric
)

// noTimestamp is used as a value for metric without a given timestamp.
const noTimestamp = int64(0)

type aggregator struct {
nbContextGauge uint64
nbContextCount uint64
Expand Down
8 changes: 2 additions & 6 deletions statsd/buffer.go
Expand Up @@ -45,9 +45,7 @@ func (b *statsdBuffer) writeGauge(namespace string, globalTags []string, name st
}
originalBuffer := b.buffer
b.buffer = appendGauge(b.buffer, namespace, globalTags, name, value, tags, rate)
if timestamp != noTimestamp {
b.buffer = appendTimestamp(b.buffer, timestamp)
}
b.buffer = appendTimestamp(b.buffer, timestamp)
b.writeSeparator()
return b.validateNewElement(originalBuffer)
}
Expand All @@ -58,9 +56,7 @@ func (b *statsdBuffer) writeCount(namespace string, globalTags []string, name st
}
originalBuffer := b.buffer
b.buffer = appendCount(b.buffer, namespace, globalTags, name, value, tags, rate)
if timestamp != noTimestamp {
b.buffer = appendTimestamp(b.buffer, timestamp)
}
b.buffer = appendTimestamp(b.buffer, timestamp)
b.writeSeparator()
return b.validateNewElement(originalBuffer)
}
Expand Down
6 changes: 4 additions & 2 deletions statsd/format.go
Expand Up @@ -272,7 +272,9 @@ func appendContainerID(buffer []byte) []byte {
}

func appendTimestamp(buffer []byte, timestamp int64) []byte {
buffer = append(buffer, "|T"...)
buffer = strconv.AppendInt(buffer, timestamp, 10)
if timestamp != noTimestamp {
buffer = append(buffer, "|T"...)
buffer = strconv.AppendInt(buffer, timestamp, 10)
}
return buffer
}
25 changes: 14 additions & 11 deletions statsd/statsd.go
Expand Up @@ -126,6 +126,9 @@ const (
writerWindowsPipe string = "pipe"
)

// noTimestamp is used as a value for metric without a given timestamp.
const noTimestamp = int64(0)

type metric struct {
metricType metricType
namespace string
Expand Down Expand Up @@ -160,17 +163,17 @@ type ClientInterface interface {
// Gauge measures the value of a metric at a particular time.
Gauge(name string, value float64, tags []string, rate float64) error

// Gauge measures the value of a metric at a given time.
// Even with client side aggregation enabled, there is no aggregation done on a metric
// sent using GaugeWithTimestamp: it is written as is in the serialization buffer.
// GaugeWithTimestamp measures the value of a metric at a given time.
// The value will bypass any aggregation on the client side and agent side.
// This is useful when sending points in the past.
GaugeWithTimestamp(name string, value float64, tags []string, rate float64, timestamp time.Time) error

// Count tracks how many times something happened per second.
Count(name string, value int64, tags []string, rate float64) error

// CountWithTimestamp tracks how many times something happened at the given second.
// Even with client side aggregation enabled, there is no aggregation done on a metric
// sent using CountWithTimestamp: it is written as is in the serialization buffer.
// The value will bypass any aggregation on the client side and agent side.
// This is useful when sending points in the past.
CountWithTimestamp(name string, value int64, tags []string, rate float64, timestamp time.Time) error

// Histogram tracks the statistical distribution of a set of values on each host.
Expand Down Expand Up @@ -563,9 +566,9 @@ func (c *Client) Gauge(name string, value float64, tags []string, rate float64)
return c.send(metric{metricType: gauge, name: name, fvalue: value, tags: tags, rate: rate, globalTags: c.tags, namespace: c.namespace})
}

// Gauge measures the value of a metric at a given time.
// Even with client side aggregation enabled, there is no aggregation done on a metric
// sent using GaugeWithTimestamp: it is written as is in the serialization buffer.
// GaugeWithTimestamp measures the value of a metric at a given time.
// The value will bypass any aggregation on the client side and agent side.
// This is useful when sending points in the past.
func (c *Client) GaugeWithTimestamp(name string, value float64, tags []string, rate float64, timestamp time.Time) error {
if c == nil {
return ErrNoClient
Expand All @@ -586,9 +589,9 @@ func (c *Client) Count(name string, value int64, tags []string, rate float64) er
return c.send(metric{metricType: count, name: name, ivalue: value, tags: tags, rate: rate, globalTags: c.tags, namespace: c.namespace})
}

// Count tracks how many times something happened at the given second.
// Even with client side aggregation enabled, there is no aggregation done on a metric
// sent using CountWithTimestamp: it is written as is in the serialization buffer.
// CountWithTimestamp tracks how many times something happened at the given second.
// The value will bypass any aggregation on the client side and agent side.
// This is useful when sending points in the past.
func (c *Client) CountWithTimestamp(name string, value int64, tags []string, rate float64, timestamp time.Time) error {
if c == nil {
return ErrNoClient
Expand Down

0 comments on commit 87572b0

Please sign in to comment.