stats

package
v0.0.0-...-725c2af Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2019 License: MPL-2.0 Imports: 3 Imported by: 5

Documentation

Overview

Package stats provides a tool to collect and track statistics Example usage:

c := NewStatCollector("myapp")
c.Child("performance").Child("net-latency").Value(200)
c.Ch("performance").Ch("iops").V(100)
c.Ch("hits") //defaults to int(0)
c.Ch("hits").Incr() // add 1 hit
fmt.Println(c.JSON())

Outputs

{"init": "2015-10-05T13:01:42.722509063-04:00",
"changed": "0001-01-01T00:00:00Z",
"value": 0,
"children": {
	"hits": {
		"init": "2015-10-05T13:01:42.722511672-04:00",
		"changed": "0001-01-01T00:00:00Z",
		"value": 1
	},
	"performance": {
		"init": "2015-10-05T13:01:42.72250972-04:00",
		"changed": "0001-01-01T00:00:00Z",
		"value": 0,
		"children": {
			"iops": {
				"init": "2015-10-05T13:01:42.722511208-04:00",
				"changed": "2015-10-05T13:01:42.722511477-04:00",
				"value": 100
			},
			"net-latency": {
				"init": "2015-10-05T13:01:42.7225103-04:00",
				"changed": "2015-10-05T13:01:42.722510848-04:00",
				"value": 200
			}
		}
	}
}}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector struct {
	StatName    string                `json:"-"`
	InitDate    *time.Time            `json:"init,omitempty"`
	ChangeDate  *time.Time            `json:"changed,omitempty"`
	StatValue   interface{}           `json:"value"`
	Children    map[string]*Collector `json:"children,omitempty"`
	DisableTime bool                  `json:"-"`
	// contains filtered or unexported fields
}

Collector stores a stat name, value, and its children. DisableTime is inherited by children automatically

func NewStatCollector

func NewStatCollector(name string) *Collector

NewStatCollector Creates an empty new collector with default stat value of int64(0) Enables timestamp collection by default

func (*Collector) Avg

func (c *Collector) Avg(nextval float32) *Collector

Avg takes the next value in a sequence to average and store for this stat If AvgLen hasn't been called yet to init this stat, default length of 10 is used.

func (*Collector) AvgLen

func (c *Collector) AvgLen(init float32, length int) *Collector

AvgLen sets the value as an array where index 0 is the computed average. It sets the length of items to store to compute that average, if it has not been set. If it has been set already, then it will ignore this call.

func (*Collector) Ch

func (c *Collector) Ch(name string) *Collector

Ch is the short function alias for Child()

func (*Collector) Child

func (c *Collector) Child(name string) *Collector

Child retrives (and creates, if does not exist) a child stat by name

func (*Collector) Decr

func (c *Collector) Decr() *Collector

Decr subtracts from the current stat value by 1 (or 1.0 for float) Supported types: int, int32, int64, float32, float64, others are ignored

func (*Collector) DisableTimes

func (c *Collector) DisableTimes()

DisableTimes sets init to 0, changed to 0, and disables the timestamp collection for this item

func (*Collector) EnableTimes

func (c *Collector) EnableTimes()

EnableTimes sets init to time.Now(), leaves changed alone, and enables the timestamp collection for this item

func (*Collector) GetV

func (c *Collector) GetV() interface{}

GetV returns the collectors stat value

func (*Collector) Has

func (c *Collector) Has(name string) bool

Has checks if a child stat name exists on the collector, returns true if exists

func (*Collector) Incr

func (c *Collector) Incr() *Collector

Incr adds to the current stat value by 1 (or 1.0 for float) Supported types: int, int32, int64, float32, float64, others are ignored

func (*Collector) JSON

func (c *Collector) JSON() (string, error)

JSON Returns the collector and its children in serialized JSON format

{
	"init": "1999-10-05T12:25:08.793401767-04:00",
	"changed": "0001-01-01T00:00:00Z",
	"value": 0,
	"children": {
		"c1": {
			"init": "1999-10-05T12:25:08.793402159-04:00",
			"changed": "2001-10-05T12:25:08.79340264-04:00",
			"value": 1
		}
	}
}

func (*Collector) Name

func (c *Collector) Name() string

Name returns the collectors stat name

func (*Collector) V

func (c *Collector) V(v interface{}) *Collector

V is the short function alias for Value()

func (*Collector) Value

func (c *Collector) Value(v interface{}) *Collector

Value sets the collectors stat value

func (*Collector) Vf

func (c *Collector) Vf(f float64) *Collector

Vf is the short function alias for Value() which accepts only a float64

func (*Collector) Vi

func (c *Collector) Vi(i int64) *Collector

Vi is the short function alias for Value() which accepts only an int64

func (*Collector) Vs

func (c *Collector) Vs(s string) *Collector

Vs is the short function alias for Value() which accepts only a string

Jump to

Keyboard shortcuts

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