metrics

package
v0.0.0-...-229c351 Latest Latest
Warning

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

Go to latest
Published: May 15, 2015 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: 0

README

Example API use

// Initialize a metric context
m := metrics.NewMetricContext("system")

// Create a basic counter, all ops are atomic
c := metrics.NewBasicCounter()

c.Add(n)    // increment counter by delta n
c.Set(n)    // Set counter value to n


// Create a new counter; has additional state associated with it
// to calculate rate

c := metrics.NewCounter()

c.Add(n)    // increment counter by delta n
c.Set(n)    // Set counter value to n

r := c.ComputeRate() // compute rate of change/sec

// Create a new gauge
// Set/Get acquire a mutex
c := metrics.NewGauge()
c.Set(12.0) // Set Value
c.Get() // get Value

// StatsTimer - useful for computing statistics on timed operations
s := metrics.NewStatsTimer()

t := s.Start() // returns a timer
s.Stop(t) // stop the timer

// Example
func (* Webapp) ServeRequest(uri string) error {
	t := s.Start()

	// do something
	s.Stop(t)
}
pctile_75th, err := s.Percentile(75)
if err == nil {
	fmt.Println("Percentile latency for 75 pctile: ", pctile_75th)
}


// Launch a goroutine to serve metrics via http json
go func() {
	http.HandleFunc("/metrics.json", m.HttpJsonHandler)
	http.ListenAndServe("localhost:12345", nil)
}

// Get metrics via http json.
resp, err := http.Get("http://localhost:12345/metrics.json")

##Dependencies Package dependency is managed by godep (https://github.com/tools/godep). Follow the docs there when adding/removing/updating package dependencies.

Documentation

Index

Constants

View Source
const NOT_INITIALIZED = -1
View Source
const NS_IN_SEC = float64(time.Second)

nanoseconds in a second represented in float64

Variables

View Source
var PERCENTILES = []float64{50, 75, 95, 99, 99.9, 99.99, 99.999}

default percentiles to compute when serializing statstimer type to stdout/json

View Source
var TICKS int64

Functions

This section is empty.

Types

type BasicCounter

type BasicCounter uint64

func NewBasicCounter

func NewBasicCounter() *BasicCounter

func (*BasicCounter) Add

func (c *BasicCounter) Add(delta uint64)

Add delta to counter value v

func (*BasicCounter) Get

func (c *BasicCounter) Get() uint64

Get value of counter

func (*BasicCounter) MarshalJSON

func (c *BasicCounter) MarshalJSON() ([]byte, error)

MarshalJSON returns a byte slice of JSON representation of basiccounter

func (*BasicCounter) Reset

func (c *BasicCounter) Reset()

Reset counter to zero

func (*BasicCounter) Set

func (c *BasicCounter) Set(v uint64)

Set counter to value v.

type Counter

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

Counters

func NewCounter

func NewCounter() *Counter

Counters differ from BasicCounter by having additional fields for computing rate. Operations on counter hold a mutex. use BasicCounter if you need lock-free counters

func (*Counter) Add

func (c *Counter) Add(delta uint64)

Add value to counter

func (*Counter) ComputeRate

func (c *Counter) ComputeRate() float64

func (*Counter) Get

func (c *Counter) Get() uint64

Get value of counter

func (*Counter) MarshalJSON

func (c *Counter) MarshalJSON() ([]byte, error)

MarshalJSON returns a byte slice of JSON representation of counter

func (*Counter) Reset

func (c *Counter) Reset()

Reset() - resets all internal variables to defaults Usually called from NewCounter but useful if you have to re-use and existing object

func (*Counter) Set

func (c *Counter) Set(v uint64)

Set Counter value. This is useful if you are reading a metric that is already a counter

type Gauge

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

Gauges

func NewGauge

func NewGauge() *Gauge

NewGauge initializes a Gauge and returns it

func (*Gauge) Get

func (g *Gauge) Get() float64

Get value of Gauge

func (*Gauge) MarshalJSON

func (g *Gauge) MarshalJSON() ([]byte, error)

MarshalJSON returns a byte slice of JSON representation of Gauge

func (*Gauge) Reset

func (g *Gauge) Reset()

Reset() all values are reset to defaults Usually called from NewGauge but useful if you have to re-use and existing object

func (*Gauge) Set

func (g *Gauge) Set(v float64)

Set value of Gauge

type Int64Slice

type Int64Slice []int64

func (Int64Slice) Len

func (a Int64Slice) Len() int

func (Int64Slice) Less

func (a Int64Slice) Less(i, j int) bool

func (Int64Slice) Swap

func (a Int64Slice) Swap(i, j int)

type MetricContext

type MetricContext struct {
	Counters      map[string]*Counter
	Gauges        map[string]*Gauge
	BasicCounters map[string]*BasicCounter
	StatsTimers   map[string]*StatsTimer
	OutputFilter  OutputFilterFunc
	// contains filtered or unexported fields
}

func NewMetricContext

func NewMetricContext(namespace string) *MetricContext

func (*MetricContext) EncodeJSON

func (m *MetricContext) EncodeJSON(w io.Writer) error

EncodeJSON is a streaming encoder that writes all metrics passing filter to writer w as JSON

func (*MetricContext) HttpJsonHandler

func (m *MetricContext) HttpJsonHandler(w http.ResponseWriter, r *http.Request)

HttpJsonHandler setups a handler for exposing metrics via JSON over HTTP

func (*MetricContext) Register

func (m *MetricContext) Register(v interface{}, name string)

Register(v Metric) registers a metric with metric context

func (*MetricContext) Unregister

func (m *MetricContext) Unregister(v interface{}, name string)

Unregister(v Metric) unregisters a metric with metric context

type MetricJSON

type MetricJSON struct {
	Type  string
	Name  string
	Value interface{}
}

MetricJSON is a type for serializing any metric type

type OutputFilterFunc

type OutputFilterFunc func(name string, v interface{}) bool

type StatsTimer

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

func NewStatsTimer

func NewStatsTimer(timeUnit time.Duration, nsamples int) *StatsTimer

func (*StatsTimer) MarshalJSON

func (s *StatsTimer) MarshalJSON() ([]byte, error)

MarshalJSON returns a byte slice containing representation of StatsTimer

func (*StatsTimer) Percentile

func (s *StatsTimer) Percentile(percentile float64) (float64, error)

func (*StatsTimer) Reset

func (s *StatsTimer) Reset()

func (*StatsTimer) Start

func (s *StatsTimer) Start() *Timer

func (*StatsTimer) Stop

func (s *StatsTimer) Stop(t *Timer) float64

type Timer

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

func NewTimer

func NewTimer() *Timer

Timer

func (*Timer) Get

func (t *Timer) Get() int64

func (*Timer) Start

func (t *Timer) Start()

func (*Timer) Stop

func (t *Timer) Stop() int64

Jump to

Keyboard shortcuts

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