diff --git a/telemetry/doc.go b/telemetry/doc.go new file mode 100644 index 0000000..c5d0356 --- /dev/null +++ b/telemetry/doc.go @@ -0,0 +1,2 @@ +// Package telemetry provides monitoring utilities. +package telemetry diff --git a/telemetry/runtime.go b/telemetry/runtime.go new file mode 100644 index 0000000..9ccaa32 --- /dev/null +++ b/telemetry/runtime.go @@ -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())) +}