Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: go/metrics2/counter.go

Issue 1722163002: Metrics2: Store a registry of Counters (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/httputils/http.go ('k') | go/metrics2/docs.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package metrics2 1 package metrics2
2 2
3 import "sync" 3 import (
4 » "fmt"
5 » "sync"
6
7 » "github.com/skia-dev/glog"
8 » "go.skia.org/infra/go/util"
9 )
4 10
5 const ( 11 const (
6 MEASUREMENT_COUNTER = "counter" 12 MEASUREMENT_COUNTER = "counter"
7 ) 13 )
8 14
9 // Counter is a struct used for tracking metrics which increment or decrement. 15 // Counter is a struct used for tracking metrics which increment or decrement.
10 type Counter struct { 16 type Counter struct {
11 m *Int64Metric 17 m *Int64Metric
12 mtx sync.Mutex 18 mtx sync.Mutex
13 } 19 }
14 20
15 // NewCounter creates and returns a new Counter. 21 // GetCounter creates or retrieves a Counter with the given name and tag set and
16 func (c *Client) NewCounter(name string, tags map[string]string) *Counter { 22 // returns it.
17 » // Add the name to the tags. 23 func (c *Client) GetCounter(name string, tagsList ...map[string]string) *Counter {
18 » t := make(map[string]string, len(tags)+1) 24 » c.countersMtx.Lock()
19 » for k, v := range tags { 25 » defer c.countersMtx.Unlock()
20 » » t[k] = v 26
27 » // Make a copy of the concatenation of all provided tags.
28 » tags := util.AddParams(map[string]string{}, tagsList...)
29 » tags["name"] = name
30 » md5, err := util.MD5Params(tags)
31 » if err != nil {
32 » » glog.Errorf("Failed to encode measurement tags: %s", err)
21 } 33 }
22 » t["name"] = name 34 » key := fmt.Sprintf("%s_%s", MEASUREMENT_COUNTER, md5)
23 » return &Counter{ 35 » m, ok := c.counters[key]
24 » » m: c.GetInt64Metric(MEASUREMENT_COUNTER, t), 36 » if !ok {
25 » » mtx: sync.Mutex{}, 37 » » m = &Counter{
38 » » » m: c.GetInt64Metric(MEASUREMENT_COUNTER, tags),
39 » » }
40 » » c.counters[key] = m
26 } 41 }
42 return m
27 } 43 }
28 44
29 // NewCounter creates and returns a new Counter using the default client. 45 // GetCounter creates and returns a new Counter using the default client.
30 func NewCounter(name string, tags map[string]string) *Counter { 46 func GetCounter(name string, tags map[string]string) *Counter {
31 » return DefaultClient.NewCounter(name, tags) 47 » return DefaultClient.GetCounter(name, tags)
32 } 48 }
33 49
34 // Inc increments the counter by the given quantity. 50 // Inc increments the counter by the given quantity.
35 func (c *Counter) Inc(i int64) { 51 func (c *Counter) Inc(i int64) {
36 c.mtx.Lock() 52 c.mtx.Lock()
37 defer c.mtx.Unlock() 53 defer c.mtx.Unlock()
38 c.m.Update(c.m.Get() + i) 54 c.m.Update(c.m.Get() + i)
39 } 55 }
40 56
41 // Dec decrements the counter by the given quantity. 57 // Dec decrements the counter by the given quantity.
42 func (c *Counter) Dec(i int64) { 58 func (c *Counter) Dec(i int64) {
43 c.mtx.Lock() 59 c.mtx.Lock()
44 defer c.mtx.Unlock() 60 defer c.mtx.Unlock()
45 c.m.Update(c.m.Get() - i) 61 c.m.Update(c.m.Get() - i)
46 } 62 }
47 63
48 // Reset sets the counter to zero. 64 // Reset sets the counter to zero.
49 func (c *Counter) Reset() { 65 func (c *Counter) Reset() {
50 c.mtx.Lock() 66 c.mtx.Lock()
51 defer c.mtx.Unlock() 67 defer c.mtx.Unlock()
52 c.m.Update(0) 68 c.m.Update(0)
53 } 69 }
54 70
55 // Get returns the current value in the counter. 71 // Get returns the current value in the counter.
56 func (c *Counter) Get() int64 { 72 func (c *Counter) Get() int64 {
57 c.mtx.Lock() 73 c.mtx.Lock()
58 defer c.mtx.Unlock() 74 defer c.mtx.Unlock()
59 return c.m.Get() 75 return c.m.Get()
60 } 76 }
OLDNEW
« no previous file with comments | « go/httputils/http.go ('k') | go/metrics2/docs.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698