Skip to content

Commit

Permalink
Merge pull request #1 from andriyko/tags-support
Browse files Browse the repository at this point in the history
Set tags on func calls
  • Loading branch information
andriyko authored Mar 30, 2018
2 parents fe592d6 + 39db803 commit bb8f7b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ func Tags(tags ...string) Option {
})
}

func joinTagsMap(tf TagFormat, tags map[string]string) string {
statsTags := make([]tag, 0, len(tags)*2)
for k, v := range tags {
t := tag{K: k, V: v}
statsTags = append(statsTags, t)
}
return joinTags(tf, statsTags)
}

type tag struct {
K, V string
}
Expand Down Expand Up @@ -191,6 +200,7 @@ const (
// Datadog tag format.
// See http://docs.datadoghq.com/guides/metrics/#tags
Datadog
// Librato tag format
Librato
)

Expand Down
14 changes: 14 additions & 0 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ func (c *Client) Timing(bucket string, value interface{}) {
c.conn.metric(c.prefix, bucket, value, "ms", c.rate, c.tags)
}

// TimingWithTags ...
func (c *Client) TimingWithTags(bucket string, value interface{}, tags map[string]string) {
if c.skip() {
return
}
strTags := joinTagsMap(c.conn.tagFormat, tags)
c.conn.metric(c.prefix, bucket, value, "ms", c.rate, strTags)
}

// Histogram sends an histogram value to a bucket.
func (c *Client) Histogram(bucket string, value interface{}) {
if c.skip() {
Expand All @@ -132,6 +141,11 @@ func (t Timing) Send(bucket string) {
t.c.Timing(bucket, int(t.Duration()/time.Millisecond))
}

// SendWithTags sends the time elapsed since the creation of the Timing
func (t Timing) SendWithTags(bucket string, tags map[string]string) {
t.c.TimingWithTags(bucket, float64(time.Since(t.start))/float64(time.Millisecond), tags)
}

// Duration returns the time elapsed since the creation of the Timing.
func (t Timing) Duration() time.Duration {
return now().Sub(t.start)
Expand Down

0 comments on commit bb8f7b6

Please sign in to comment.