Skip to content

Commit

Permalink
added sync.Mutex locks to all metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
arussellsaw committed May 3, 2015
1 parent 2e0c57d commit d40c034
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions average.go
@@ -1,22 +1,28 @@
package telemetry

import (
"sync"
"time"
)

//Average running average over a time.Duration
type Average struct {
metric map[string]metric
lock sync.Mutex
}

//New create new averaged metric
func (a *Average) New(name string, duration time.Duration) {
a.lock.Lock()
defer a.lock.Unlock()
average := metric{duration: duration}
a.metric[name] = average
}

//Add add value to averaged metric
func (a *Average) Add(name string, value float32) {
a.lock.Lock()
defer a.lock.Unlock()
point := point{value: value, timestamp: time.Now()}
//this ugly section is because we cannot assign to properties of a
//struct within a map, so have to create the entire struct again
Expand Down
6 changes: 6 additions & 0 deletions counter.go
@@ -1,22 +1,28 @@
package telemetry

import (
"sync"
"time"
)

//Counter metric type for total over a time.Duration
type Counter struct {
metric map[string]metric
lock sync.Mutex
}

//New create new counter metric
func (c *Counter) New(name string, duration time.Duration) {
c.lock.Lock()
defer c.lock.Unlock()
counter := metric{duration: duration}
c.metric[name] = counter
}

//Add add value to counter
func (c *Counter) Add(name string, value float32) {
c.lock.Lock()
defer c.lock.Unlock()
point := point{value: value, timestamp: time.Now()}
//this ugly section is because we cannot assign to properties of a
//struct within a map, so have to create the entire struct again
Expand Down
6 changes: 6 additions & 0 deletions total.go
@@ -1,21 +1,27 @@
package telemetry

import (
"sync"
"time"
)

//Total a total sum of a metric over the lifetime of the process
type Total struct {
metric map[string]float32
lock sync.Mutex
}

//New new total sum metric
func (t *Total) New(name string, _ time.Duration) {
t.lock.Lock()
defer t.lock.Unlock()
t.metric[name] = 0
}

//Add add value to existing metric
func (t *Total) Add(name string, value float32) {
t.lock.Lock()
defer t.lock.Unlock()
t.metric[name] = (t.metric[name] + value)
}

Expand Down

0 comments on commit d40c034

Please sign in to comment.