micro/debug/stats/default.go

39 lines
601 B
Go
Raw Normal View History

2019-12-05 00:08:46 +00:00
package stats
import (
2019-12-17 15:38:03 +00:00
"github.com/micro/go-micro/util/ring"
2019-12-05 00:08:46 +00:00
)
type stats struct {
2019-12-17 15:38:03 +00:00
buffer *ring.Buffer
2019-12-05 00:08:46 +00:00
}
func (s *stats) Read() ([]*Stat, error) {
// TODO adjustable size and optional read values
buf := s.buffer.Get(1)
var stats []*Stat
for _, b := range buf {
stat, ok := b.Value.(*Stat)
if !ok {
continue
}
stats = append(stats, stat)
}
return stats, nil
}
func (s *stats) Write(stat *Stat) error {
s.buffer.Put(stat)
return nil
}
// NewStats returns a new in memory stats buffer
// TODO add options
func NewStats() Stats {
return &stats{
2019-12-17 15:38:03 +00:00
buffer: ring.New(1024),
2019-12-05 00:08:46 +00:00
}
}