Documentation ¶
Overview ¶
Package gmetric defines metric service and http handler
Index ¶
- func NewHandler(URI string, metrics *Service) http.Handler
- func NewRouter(URI string, service *Service) *toolbox.ServiceRouter
- type Counter
- type Identity
- type Operation
- type Service
- func (s *Service) Counter(location, name, description string) *Counter
- func (s *Service) Counters() []Counter
- func (s *Service) LookupCounter(name string) *Counter
- func (s *Service) LookupOperation(name string) *Operation
- func (s *Service) LookupOperationCumulativeMetric(operationName, metric string) int64
- func (s *Service) LookupOperationRecentMetric(operationName, metric string) int64
- func (s *Service) LookupOperationRecentMetrics(operationName string) counter.Operation
- func (s *Service) MultiOperationCounter(location, name, description string, unit, loopbackUnit time.Duration, ...) *Operation
- func (s *Service) OperationCounter(location, name, description string, unit, loopbackUnit time.Duration, ...) *Operation
- func (s *Service) OperationCounters() []Operation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶ added in v0.2.0
NewHandler creates a metric http handler
Types ¶
type Counter ¶ added in v0.2.0
Counter represents a counter
func NewCounter ¶ added in v0.2.0
NewCounter creates a counters
type Operation ¶ added in v0.2.0
Operation represents named counters metrics
type Service ¶ added in v0.2.0
type Service struct {
// contains filtered or unexported fields
}
Service represents operation metrics
func (*Service) Counter ¶ added in v0.2.0
Counter register counters
Example ¶
package main import ( "github.com/viant/gmetric" "net/http" "time" ) func main() { metrics := gmetric.New() handler := gmetric.NewHandler("/v1/metrics", metrics) http.Handle("/v1/metrics", handler) //basic single counter counter := metrics.OperationCounter("pkg.myapp", "mySingleCounter1", "my description", time.Microsecond, time.Minute, 2) go runBasicTasks(counter) go http.ListenAndServe(":8080", http.DefaultServeMux) } func runBasicTasks(counter *gmetric.Operation) { for i := 0; i < 1000; i++ { runBasicTask(counter) } } func runBasicTask(counter *gmetric.Operation) { onDone := counter.Begin(time.Now()) defer func() { onDone(time.Now()) }() time.Sleep(time.Nanosecond) }
Output:
func (*Service) LookupCounter ¶ added in v0.2.0
LookupCounter returns counters
func (*Service) LookupOperation ¶ added in v0.2.0
LookupOperation returns operation counters
func (*Service) LookupOperationCumulativeMetric ¶ added in v0.2.0
LookupOperationCumulativeMetric returns operation metric cumulative value
func (*Service) LookupOperationRecentMetric ¶ added in v0.2.0
LookupOperationRecentMetric returns operation metric current bucket value
func (*Service) LookupOperationRecentMetrics ¶ added in v0.2.6
LookupOperationRecentMetrics returns operation metrics current bucket values
func (*Service) MultiOperationCounter ¶ added in v0.2.0
func (s *Service) MultiOperationCounter(location, name, description string, unit, loopbackUnit time.Duration, loopbackSize int, provider counter.Provider) *Operation
MultiOperationCounter register multi value operation counters
Example ¶
package main import ( "errors" "github.com/viant/gmetric" "github.com/viant/gmetric/counter/base" "github.com/viant/gmetric/stat" "log" "math/rand" "net/http" "time" ) const ( NoSuchKey = "noSuchKey" MyStatsCacheHit = "cacheHit" MyStatsCacheCollision = "cacheCollision" ) // MultiStateStatTestProvider represents multi stats value provider type MultiStateStatTestProvider struct{ *base.Provider } // Map maps value int slice index func (p *MultiStateStatTestProvider) Map(key interface{}) int { textKey, ok := key.(string) if !ok { return p.Provider.Map(key) } switch textKey { case NoSuchKey: return 1 case MyStatsCacheHit: return 2 case MyStatsCacheCollision: return 3 } return -1 } func main() { metrics := gmetric.New() handler := gmetric.NewHandler("/v1/metrics", metrics) http.Handle("/v1/metrics", handler) counter := metrics.MultiOperationCounter("pkg.myapp", "myMultiCounter", "my description", time.Microsecond, time.Minute, 2, &MultiStateStatTestProvider{}) go runMultiStateTasks(counter) err := http.ListenAndServe(":8080", http.DefaultServeMux) if err != nil { log.Fatal(err) } } func runMultiStateTasks(counter *gmetric.Operation) { for i := 0; i < 1000; i++ { runMultiStateTask(counter) } } func runMultiStateTask(counter *gmetric.Operation) { stats := stat.New() onDone := counter.Begin(time.Now()) defer func() { onDone(time.Now(), stats) }() time.Sleep(time.Nanosecond) rnd := rand.NewSource(time.Now().UnixNano()) state := rnd.Int63() % 3 switch state { case 0: stats.Append(NoSuchKey) case 1: stats.Append(MyStatsCacheHit) case 2: stats.Append(MyStatsCacheHit) stats.Append(MyStatsCacheCollision) } if rnd.Int63()%10 == 0 { stats.Append(errors.New("test error")) } }
Output:
func (*Service) OperationCounter ¶ added in v0.2.0
func (s *Service) OperationCounter(location, name, description string, unit, loopbackUnit time.Duration, RecentBuckets int) *Operation
OperationCounter register operation counters
func (*Service) OperationCounters ¶ added in v0.2.0
OperationCounters returns operation counters
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package counter define single or multi metric counters
|
Package counter define single or multi metric counters |
Package stat define metric stats values
|
Package stat define metric stats values |
Package window define rolling metrics
|
Package window define rolling metrics |