gmetric

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2023 License: Apache-2.0 Imports: 10 Imported by: 25

README

Application operation performance metric (gmetric)

Application operation performance metric. GoDoc

This library is compatible with Go 1.12+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

The goal of this project is to provide metrics to measure various aspect of the application behaviours with minimum overhead.
Application metrics can be viewed as cumulative or recent rolling window values. All metrics are locking and memory allocation free to provide best possible performance.

Usage

Single metric counter

package mypackage

import (
	"errors"
	"github.com/viant/gmetric"
	"github.com/viant/gmetric/stat"
	"log"
	"math/rand"
	"net/http"
	"testing"
	"time"
)

func OperationCounterUsage() {

	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)

	err := http.ListenAndServe(":8080", http.DefaultServeMux)
	if err != nil {
		log.Fatal(err)
	}
}

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)

}

open http://127.0.0.1:8080/v1/metrics/counters
## or 
open http://127.0.0.1:8080/v1/metrics/counter/mySingleCounter1
Multi metric counter
package gmetric_test

import (
	"errors"
	"github.com/viant/gmetric"
	"github.com/viant/gmetric/counter/base"
	"github.com/viant/gmetric/stat"
	"log"
	"math/rand"
	"net/http"
	"testing"
	"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 OperationMulriCounterUsage() {
	metrics := gmetric.New()
	handler := gmetric.NewHandler("/v1/metrics", metrics)
	http.Handle("/v1/metrics", handler)
	counter := metrics.MultiOperationCounter("pkg.myapp", "myMultiCounter1", "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)
	//simulate task metrics state
	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"))
	}
}

### all operations counters
open http://127.0.0.1:8080/v1/metrics/operations


## indivual operation multi counter
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1

## cumulative indivual operation multi counter metric value

open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/count
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/min
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/avg
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/max
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/error
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/noSuchKey
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/cacheCollision
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/cumulative/cacheCollision.pct

## recent counters
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/count
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/min
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/avg
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/max
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/counter
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/error
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/noSuchKey
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/noSuchKey.pct
open http://127.0.0.1:8080/v1/metrics/operations/myMultiCounter1/recent/cacheCollision

Example metric:

Example metrics

License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license, all compatible with Apache License, Version 2. Please see individual files for details.

Credits and Acknowledgements

Library Author: Adrian Witas

Documentation

Overview

Package gmetric defines metric service and http handler

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler added in v0.2.0

func NewHandler(URI string, metrics *Service) http.Handler

NewHandler creates a metric http handler

func NewRouter added in v0.2.0

func NewRouter(URI string, service *Service) *toolbox.ServiceRouter

NewRouter crates service router

Types

type Counter added in v0.2.0

type Counter struct {
	Identity
	counter.Counter
}

Counter represents a counter

func NewCounter added in v0.2.0

func NewCounter(location, name, description string) Counter

NewCounter creates a counters

func (*Counter) Reset added in v0.2.0

func (c *Counter) Reset()

Reset resets counters

type Identity added in v0.2.0

type Identity struct {
	Location    string
	Name        string
	Description string
}

Identity represents identity

type Operation added in v0.2.0

type Operation struct {
	Identity
	window.Operation
	// contains filtered or unexported fields
}

Operation represents named counters metrics

func NewOperation added in v0.2.0

func NewOperation(location, name, description string, size int, recentUnit, unit time.Duration, provider counter.Provider) Operation

NewOperation creates a new counters

func (*Operation) Reset added in v0.2.0

func (c *Operation) Reset()

Reset resets counters

type Service added in v0.2.0

type Service struct {
	// contains filtered or unexported fields
}

Service represents operation metrics

func New added in v0.2.0

func New() *Service

New creates a new metric Service

func (*Service) Counter added in v0.2.0

func (s *Service) Counter(location, name, description string) *Counter

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) Counters added in v0.2.0

func (s *Service) Counters() []Counter

Counters returns counters

func (*Service) LookupCounter added in v0.2.0

func (s *Service) LookupCounter(name string) *Counter

LookupCounter returns counters

func (*Service) LookupOperation added in v0.2.0

func (s *Service) LookupOperation(name string) *Operation

LookupOperation returns operation counters

func (*Service) LookupOperationCumulativeMetric added in v0.2.0

func (s *Service) LookupOperationCumulativeMetric(operationName, metric string) int64

LookupOperationCumulativeMetric returns operation metric cumulative value

func (*Service) LookupOperationRecentMetric added in v0.2.0

func (s *Service) LookupOperationRecentMetric(operationName, metric string) int64

LookupOperationRecentMetric returns operation metric current bucket value

func (*Service) LookupOperationRecentMetrics added in v0.2.6

func (s *Service) LookupOperationRecentMetrics(operationName string) counter.Operation

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

func (s *Service) OperationCounters() []Operation

OperationCounters returns operation counters

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL