Chromium Code Reviews| Index: go/metrics2/metrics.go |
| diff --git a/go/metrics2/metrics.go b/go/metrics2/metrics.go |
| index 1636283923d60f4aeb36f43af48414c6cd62f4f6..7302be3872f8071e07d5f0c5f0a986b26c4bbe78 100644 |
| --- a/go/metrics2/metrics.go |
| +++ b/go/metrics2/metrics.go |
| @@ -23,6 +23,7 @@ const ( |
| var ( |
| DefaultClient *Client = &Client{ |
| aggMetrics: map[string]*aggregateMetric{}, |
| + counters: map[string]*Counter{}, |
| metrics: map[string]*rawMetric{}, |
| } |
| ) |
| @@ -44,6 +45,7 @@ func Init(appName string, influxClient *influxdb.Client) error { |
| // Some metrics may already be registered with DefaultClient. Copy them |
| // over. |
| c.aggMetrics = DefaultClient.aggMetrics |
| + c.counters = DefaultClient.counters |
| c.metrics = DefaultClient.metrics |
| // Set the default client. |
| @@ -53,12 +55,18 @@ func Init(appName string, influxClient *influxdb.Client) error { |
| // Client is a struct used for communicating with an InfluxDB instance. |
| type Client struct { |
| - aggMetrics map[string]*aggregateMetric |
| - aggMetricsMtx sync.Mutex |
| - influxClient *influxdb.Client |
| - defaultTags map[string]string |
| - metrics map[string]*rawMetric |
| - metricsMtx sync.Mutex |
| + aggMetrics map[string]*aggregateMetric |
| + aggMetricsMtx sync.Mutex |
| + |
| + counters map[string]*Counter |
| + countersMtx sync.Mutex |
| + |
| + influxClient *influxdb.Client |
| + defaultTags map[string]string |
| + |
| + metrics map[string]*rawMetric |
| + metricsMtx sync.Mutex |
| + |
| reportFrequency time.Duration |
| values *influxdb.BatchPoints |
| valuesMtx sync.Mutex |
| @@ -76,6 +84,8 @@ func NewClient(influxClient *influxdb.Client, defaultTags map[string]string, rep |
| c := &Client{ |
| aggMetrics: map[string]*aggregateMetric{}, |
| aggMetricsMtx: sync.Mutex{}, |
| + counters: map[string]*Counter{}, |
| + countersMtx: sync.Mutex{}, |
| influxClient: influxClient, |
| defaultTags: defaultTags, |
| metrics: map[string]*rawMetric{}, |
| @@ -264,3 +274,29 @@ func (c *Client) getAggregateMetric(measurement string, tagsList []map[string]st |
| } |
| return m |
| } |
| + |
| +// getCounter creates or retrieves a Counter with the given name and tag set and |
|
stephana
2016/02/24 14:48:16
This should be in counter.go file, all the other G
borenet
2016/02/24 15:09:35
Done.
|
| +// returns it. |
| +func (c *Client) GetCounter(name string, tagsList ...map[string]string) *Counter { |
| + c.countersMtx.Lock() |
| + defer c.countersMtx.Unlock() |
| + |
| + // Make a copy of the concatenation of all provided tags. |
| + tags := util.AddParams(map[string]string{}, tagsList...) |
| + tags["name"] = name |
| + md5, err := util.MD5Params(tags) |
| + if err != nil { |
| + glog.Errorf("Failed to encode measurement tags: %s", err) |
| + } |
| + key := fmt.Sprintf("%s_%s", MEASUREMENT_COUNTER, md5) |
| + m, ok := c.counters[key] |
| + if !ok { |
| + m = &Counter{ |
| + m: c.GetInt64Metric(MEASUREMENT_COUNTER, tags), |
| + mtx: sync.Mutex{}, |
|
stephana
2016/02/24 14:48:16
nit: Mutex doesn't need to be initialized, it's un
borenet
2016/02/24 15:09:35
Done, here and elsewhere.
|
| + } |
| + c.counters[key] = m |
| + } |
| + return m |
| + |
| +} |