Skip to content

Commit

Permalink
utility for reporting runtime stats
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Oct 23, 2017
1 parent e37e906 commit 3715ac7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions telemetry/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package telemetry provides monitoring utilities.
package telemetry
47 changes: 47 additions & 0 deletions telemetry/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package telemetry

import (
"runtime"

"github.com/uber-go/tally"
)

// Runtime reports goroutine and memory statistics exposed by the runtime package.
type Runtime struct {
numGoroutines tally.Gauge
heapAlloc tally.Gauge
heapIdle tally.Gauge
heapInuse tally.Gauge
heapObjects tally.Gauge
stackInuse tally.Gauge
}

// NewRuntime constructs runtime metrics from the given scope.
func NewRuntime(scope tally.Scope) *Runtime {
sub := scope.SubScope("runtime")
memStat := func(name string) tally.Gauge {
return sub.Tagged(map[string]string{"stat": name}).Gauge("memory")
}
return &Runtime{
numGoroutines: sub.Gauge("goroutines"),
heapAlloc: memStat("heap_alloc"),
heapIdle: memStat("heap_idle"),
heapInuse: memStat("heap_inuse"),
heapObjects: memStat("heap_objects"),
stackInuse: memStat("stack_inuse"),
}
}

// Update updates runtime stats.
func (r Runtime) Update() {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)

r.heapAlloc.Update(float64(mem.HeapAlloc))
r.heapIdle.Update(float64(mem.HeapIdle))
r.heapInuse.Update(float64(mem.HeapInuse))
r.heapObjects.Update(float64(mem.HeapObjects))
r.stackInuse.Update(float64(mem.StackInuse))

r.numGoroutines.Update(float64(runtime.NumGoroutine()))
}

0 comments on commit 3715ac7

Please sign in to comment.