debug/profile: move to profiler interface

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-14 14:02:51 +03:00
parent e5bf1448f4
commit cc7ebedf22
6 changed files with 0 additions and 123 deletions

View File

@ -1,2 +0,0 @@
// Package debug provides interfaces for service debugging
package debug

View File

@ -1,89 +0,0 @@
package stats
import (
"runtime"
"sync"
"time"
"github.com/unistack-org/micro/v3/debug/stats"
"github.com/unistack-org/micro/v3/util/ring"
)
type memoryStats struct {
// used to store past stats
buffer *ring.Buffer
sync.RWMutex
started int64
requests uint64
errors uint64
}
func (s *memoryStats) snapshot() *stats.Stat {
s.RLock()
defer s.RUnlock()
var mstat runtime.MemStats
runtime.ReadMemStats(&mstat)
now := time.Now().Unix()
return &stats.Stat{
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,
}
}
func (s *memoryStats) Read() ([]*stats.Stat, error) {
buf := s.buffer.Get(s.buffer.Size())
var buffer []*stats.Stat
// get a value from the buffer if it exists
for _, b := range buf {
stat, ok := b.Value.(*stats.Stat)
if !ok {
continue
}
buffer = append(buffer, stat)
}
// get a snapshot
buffer = append(buffer, s.snapshot())
return buffer, nil
}
func (s *memoryStats) Write(stat *stats.Stat) error {
s.buffer.Put(stat)
return nil
}
func (s *memoryStats) Record(err error) error {
s.Lock()
defer s.Unlock()
// increment the total request count
s.requests++
// increment the error count
if err != nil {
s.errors++
}
return nil
}
// NewStats returns a new in memory stats buffer
// TODO add options
func NewStats() stats.Stats {
return &memoryStats{
started: time.Now().Unix(),
buffer: ring.New(1),
}
}

View File

@ -1,32 +0,0 @@
// Package stats provides runtime stats
package stats
// Stats provides stats interface
type Stats interface {
// Read stat snapshot
Read() ([]*Stat, error)
// Write a stat snapshot
Write(*Stat) error
// Record a request
Record(error) error
}
// A runtime stat
type Stat struct {
// Timestamp of recording
Timestamp int64
// Start time as unix timestamp
Started int64
// Uptime in seconds
Uptime int64
// Memory usage in bytes
Memory uint64
// Threads aka go routines
Threads uint64
// Garbage collection in nanoseconds
GC uint64
// Total requests
Requests uint64
// Total errors
Errors uint64
}