| OLD | NEW |
| 1 package metrics2 | 1 package metrics2 |
| 2 | 2 |
| 3 import "sync" | 3 import "sync" |
| 4 | 4 |
| 5 const ( | 5 const ( |
| 6 MEASUREMENT_COUNTER = "counter" | 6 MEASUREMENT_COUNTER = "counter" |
| 7 ) | 7 ) |
| 8 | 8 |
| 9 // Counter is a struct used for tracking metrics which increment or decrement. | 9 // Counter is a struct used for tracking metrics which increment or decrement. |
| 10 type Counter struct { | 10 type Counter struct { |
| 11 m *Int64Metric | 11 m *Int64Metric |
| 12 mtx sync.Mutex | 12 mtx sync.Mutex |
| 13 } | 13 } |
| 14 | 14 |
| 15 // NewCounter creates and returns a new Counter. | 15 // GetCounter creates and returns a new Counter using the default client. |
| 16 func (c *Client) NewCounter(name string, tags map[string]string) *Counter { | 16 func GetCounter(name string, tags map[string]string) *Counter { |
| 17 » // Add the name to the tags. | 17 » return DefaultClient.GetCounter(name, tags) |
| 18 » t := make(map[string]string, len(tags)+1) | |
| 19 » for k, v := range tags { | |
| 20 » » t[k] = v | |
| 21 » } | |
| 22 » t["name"] = name | |
| 23 » return &Counter{ | |
| 24 » » m: c.GetInt64Metric(MEASUREMENT_COUNTER, t), | |
| 25 » » mtx: sync.Mutex{}, | |
| 26 » } | |
| 27 } | |
| 28 | |
| 29 // NewCounter creates and returns a new Counter using the default client. | |
| 30 func NewCounter(name string, tags map[string]string) *Counter { | |
| 31 » return DefaultClient.NewCounter(name, tags) | |
| 32 } | 18 } |
| 33 | 19 |
| 34 // Inc increments the counter by the given quantity. | 20 // Inc increments the counter by the given quantity. |
| 35 func (c *Counter) Inc(i int64) { | 21 func (c *Counter) Inc(i int64) { |
| 36 c.mtx.Lock() | 22 c.mtx.Lock() |
| 37 defer c.mtx.Unlock() | 23 defer c.mtx.Unlock() |
| 38 c.m.Update(c.m.Get() + i) | 24 c.m.Update(c.m.Get() + i) |
| 39 } | 25 } |
| 40 | 26 |
| 41 // Dec decrements the counter by the given quantity. | 27 // Dec decrements the counter by the given quantity. |
| 42 func (c *Counter) Dec(i int64) { | 28 func (c *Counter) Dec(i int64) { |
| 43 c.mtx.Lock() | 29 c.mtx.Lock() |
| 44 defer c.mtx.Unlock() | 30 defer c.mtx.Unlock() |
| 45 c.m.Update(c.m.Get() - i) | 31 c.m.Update(c.m.Get() - i) |
| 46 } | 32 } |
| 47 | 33 |
| 48 // Reset sets the counter to zero. | 34 // Reset sets the counter to zero. |
| 49 func (c *Counter) Reset() { | 35 func (c *Counter) Reset() { |
| 50 c.mtx.Lock() | 36 c.mtx.Lock() |
| 51 defer c.mtx.Unlock() | 37 defer c.mtx.Unlock() |
| 52 c.m.Update(0) | 38 c.m.Update(0) |
| 53 } | 39 } |
| 54 | 40 |
| 55 // Get returns the current value in the counter. | 41 // Get returns the current value in the counter. |
| 56 func (c *Counter) Get() int64 { | 42 func (c *Counter) Get() int64 { |
| 57 c.mtx.Lock() | 43 c.mtx.Lock() |
| 58 defer c.mtx.Unlock() | 44 defer c.mtx.Unlock() |
| 59 return c.m.Get() | 45 return c.m.Get() |
| 60 } | 46 } |
| OLD | NEW |