2019-12-05 00:08:46 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
2019-12-18 18:36:42 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/debug/stats"
|
|
|
|
"github.com/unistack-org/micro/v3/util/ring"
|
2019-12-05 00:08:46 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
type memoryStats struct {
|
2019-12-18 18:36:42 +00:00
|
|
|
// used to store past stats
|
2019-12-17 15:38:03 +00:00
|
|
|
buffer *ring.Buffer
|
2019-12-18 18:36:42 +00:00
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
started int64
|
|
|
|
requests uint64
|
|
|
|
errors uint64
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
func (s *memoryStats) snapshot() *stats.Stat {
|
2019-12-18 18:36:42 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
var mstat runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&mstat)
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
return &stats.Stat{
|
2019-12-18 18:36:42 +00: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 00:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
func (s *memoryStats) Read() ([]*stats.Stat, error) {
|
|
|
|
buf := s.buffer.Get(s.buffer.Size())
|
|
|
|
var buffer []*stats.Stat
|
2019-12-05 00:08:46 +00:00
|
|
|
|
2019-12-18 18:36:42 +00:00
|
|
|
// get a value from the buffer if it exists
|
2019-12-05 00:08:46 +00:00
|
|
|
for _, b := range buf {
|
2020-08-10 15:58:39 +01:00
|
|
|
stat, ok := b.Value.(*stats.Stat)
|
2019-12-05 00:08:46 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-10 15:58:39 +01:00
|
|
|
buffer = append(buffer, stat)
|
2019-12-05 00:08:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:36:42 +00:00
|
|
|
// get a snapshot
|
2020-08-10 15:58:39 +01:00
|
|
|
buffer = append(buffer, s.snapshot())
|
2019-12-18 18:36:42 +00:00
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
return buffer, nil
|
2019-12-05 00:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
func (s *memoryStats) Write(stat *stats.Stat) error {
|
2019-12-05 00:08:46 +00:00
|
|
|
s.buffer.Put(stat)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
func (s *memoryStats) Record(err error) error {
|
2019-12-18 18:36:42 +00: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 00:08:46 +00:00
|
|
|
// NewStats returns a new in memory stats buffer
|
|
|
|
// TODO add options
|
2020-08-10 15:58:39 +01:00
|
|
|
func NewStats() stats.Stats {
|
|
|
|
return &memoryStats{
|
2019-12-18 18:36:42 +00:00
|
|
|
started: time.Now().Unix(),
|
2020-08-10 15:58:39 +01:00
|
|
|
buffer: ring.New(1),
|
2019-12-05 00:08:46 +00:00
|
|
|
}
|
|
|
|
}
|