Skip to content

Commit

Permalink
Added memory sytem metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
arnecls committed Apr 4, 2016
1 parent 2157cf0 commit 345442d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const (
MetricGoRoutines = "GoRoutines"
// MetricGoVersion holds the go version as Major*10000+Minor*100+Patch
MetricGoVersion = "GoVersion"
// MetricMemoryAllocated holds the currently active memory in bytes
MetricMemoryAllocated = "GoMemoryAllocated"
// MetricMemoryNumObjects holds the total number of allocated heap objects
MetricMemoryNumObjects = "GoMemoryNumObjects"
// MetricMemoryGCEnabled holds 1 or 0 depending on the state of garbage collection
MetricMemoryGCEnabled = "GoMemoryGCEnabled"
)

// ProcessStartTime stores the time this process has started.
Expand All @@ -47,6 +53,9 @@ func init() {
Metric.New(MetricProcessStart)
Metric.New(MetricGoRoutines)
Metric.New(MetricGoVersion)
Metric.New(MetricMemoryAllocated)
Metric.New(MetricMemoryNumObjects)
Metric.New(MetricMemoryGCEnabled)
Metric.Set(MetricProcessStart, ProcessStartTime.Unix())

version := runtime.Version()
Expand Down Expand Up @@ -101,6 +110,17 @@ func (met *metrics) SetF(name string, value float64) {
atomic.StoreInt64(met.store[name], int64(rounded))
}

// SetB is Set for boolean values (conversion to 0/1)
func (met *metrics) SetB(name string, value bool) {
ivalue := 0
if value {
ivalue = 1
}
met.mutex.RLock()
defer met.mutex.RUnlock()
atomic.StoreInt64(met.store[name], int64(ivalue))
}

// Inc adds 1 to a given metric. This operation is atomic.
func (met *metrics) Inc(name string) {
met.mutex.RLock()
Expand Down Expand Up @@ -175,7 +195,14 @@ func (met *metrics) Get(name string) (int64, error) {
func (met *metrics) UpdateSystemMetrics() {
met.mutex.RLock()
defer met.mutex.RUnlock()

stats := new(runtime.MemStats)
runtime.ReadMemStats(stats)

met.SetI(MetricGoRoutines, runtime.NumGoroutine())
met.Set(MetricMemoryAllocated, int64(stats.Alloc))
met.SetB(MetricMemoryGCEnabled, stats.EnableGC)
met.Set(MetricMemoryNumObjects, int64(stats.HeapObjects))
}

// Dump creates a JSON string from all stored metrics.
Expand Down
11 changes: 11 additions & 0 deletions metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func TestMetricsSet(t *testing.T) {
count, err = mockMetric.Get("MockMetric")
expect.Nil(err)
expect.Equal(int64(4), count)

// test for setting a particular boolean value
mockMetric.SetB("MockMetric", true)
count, err = mockMetric.Get("MockMetric")
expect.Nil(err)
expect.Equal(int64(1), count)

mockMetric.SetB("MockMetric", false)
count, err = mockMetric.Get("MockMetric")
expect.Nil(err)
expect.Equal(int64(0), count)
}

func TestMetricsAddSub(t *testing.T) {
Expand Down

0 comments on commit 345442d

Please sign in to comment.