micro/debug/stats/memory/memory.go

90 lines
1.5 KiB
Go
Raw Normal View History

2019-12-05 03:08:46 +03:00
package stats
import (
2019-12-18 21:36:42 +03:00
"runtime"
"sync"
"time"
2020-08-10 17:58:39 +03:00
"github.com/micro/go-micro/v3/debug/stats"
"github.com/micro/go-micro/v3/util/ring"
2019-12-05 03:08:46 +03:00
)
2020-08-10 17:58:39 +03:00
type memoryStats struct {
2019-12-18 21:36:42 +03:00
// used to store past stats
2019-12-17 18:38:03 +03:00
buffer *ring.Buffer
2019-12-18 21:36:42 +03:00
sync.RWMutex
started int64
requests uint64
errors uint64
}
2020-08-10 17:58:39 +03:00
func (s *memoryStats) snapshot() *stats.Stat {
2019-12-18 21:36:42 +03:00
s.RLock()
defer s.RUnlock()
var mstat runtime.MemStats
runtime.ReadMemStats(&mstat)
now := time.Now().Unix()
2020-08-10 17:58:39 +03:00
return &stats.Stat{
2019-12-18 21:36:42 +03:00
Timestamp: now,
Started: s.started,
Uptime: now - s.started,
Memory: mstat.Alloc,
GC: mstat.PauseTotalNs,
Threads: uint64(runtime.NumGoroutine()),
Requests: s.requests,
Errors: s.errors,
}
2019-12-05 03:08:46 +03:00
}
2020-08-10 17:58:39 +03:00
func (s *memoryStats) Read() ([]*stats.Stat, error) {
buf := s.buffer.Get(s.buffer.Size())
var buffer []*stats.Stat
2019-12-05 03:08:46 +03:00
2019-12-18 21:36:42 +03:00
// get a value from the buffer if it exists
2019-12-05 03:08:46 +03:00
for _, b := range buf {
2020-08-10 17:58:39 +03:00
stat, ok := b.Value.(*stats.Stat)
2019-12-05 03:08:46 +03:00
if !ok {
continue
}
2020-08-10 17:58:39 +03:00
buffer = append(buffer, stat)
2019-12-05 03:08:46 +03:00
}
2019-12-18 21:36:42 +03:00
// get a snapshot
2020-08-10 17:58:39 +03:00
buffer = append(buffer, s.snapshot())
2019-12-18 21:36:42 +03:00
2020-08-10 17:58:39 +03:00
return buffer, nil
2019-12-05 03:08:46 +03:00
}
2020-08-10 17:58:39 +03:00
func (s *memoryStats) Write(stat *stats.Stat) error {
2019-12-05 03:08:46 +03:00
s.buffer.Put(stat)
return nil
}
2020-08-10 17:58:39 +03:00
func (s *memoryStats) Record(err error) error {
2019-12-18 21:36:42 +03:00
s.Lock()
defer s.Unlock()
// increment the total request count
s.requests++
// increment the error count
if err != nil {
s.errors++
}
return nil
}
2019-12-05 03:08:46 +03:00
// NewStats returns a new in memory stats buffer
// TODO add options
2020-08-10 17:58:39 +03:00
func NewStats() stats.Stats {
return &memoryStats{
2019-12-18 21:36:42 +03:00
started: time.Now().Unix(),
2020-08-10 17:58:39 +03:00
buffer: ring.New(1),
2019-12-05 03:08:46 +03:00
}
}