metric

package
v0.0.0-...-f99c5bb Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2016 License: Apache-2.0, HPND, MIT Imports: 8 Imported by: 0

README

gonelog metrics

Fast Golang metrics library

Package gonelog/metric is an expandable library for metrics. Initally only sending data to statsd

The design goals:

  • Basic statsd metric types (gauge, counter, timer, set)
  • Client side buffered
  • Fast

Timer and Histogram is basically the same except for the argument type.

Counter is reset to zero on each flush. Gauges are not.

Still somewhat experimental.

Example

import "github.com/One-com/gonelog/metric"

var flushPeriod = 2*time.Second

func main() {

	sink, err := metric.NewStatsdSink("1.2.3.4:8125", "prefix", 1432)
	if err != nil {
		log.Fatal(err)
	}
	flushPeriod := metric.FlushInterval(flushPeriod)
	c := metric.NewClient(sink,flushPeriod)
	
	gauge   := c.NewGauge("gauge",flushPeriod)
	timer   := c.NewTimer("timer")
	histo   := c.NewHistogram("histo",flushPeriod)
	counter := c.NewCounter("counter",flushPeriod)
	set     := c.NewSet("set", flushPeriod)

	g := 100
	for g != 0 {
		counter.Inc(1)
		gauge.Update(uint64(g))
		timer.Update(time.Duration(g)*time.Millisecond)
		histo.Update(int64(g))
		set.Update(strconv.FormatInt(int64(g), 10))
		
		time.Sleep(time.Second)
		g--
	}
	c.Stop()
	
}

Documentation

Index

Examples

Constants

View Source
const (
	MeterGauge     = iota // A client side maintained value
	MeterCounter          // A server side maintained value
	MeterHistogram        // A general distribution of measurements events.
	MeterTimer            // ... when those measurements are milliseconds
	MeterSet              // free form string events
)

Conceptual meter types Gauge: A client side maintained counter Counter: A server side maintained counter Historam: A series of events analyzed on the server Timer: A series of time.Duration events analyzed on the server Set: Discrete strings added to a set maintained on the server

View Source
const (
	Uint64 = iota
	Int64
	Float64
)

Data types for Numeric64

Variables

This section is empty.

Functions

func NewStatsdSink

func NewStatsdSink(addr string, prefix string, maxDatagramsize int) (sink *statsdSink, err error)

A Sink sending data to a UDP statsd server Provide a UDP address, a prefix and a maximum UDP datagram size. 1432 should be a safe size for most nets.

func NewTestSink

func NewTestSink(prefix string, maxDatagramsize int) (sink *testSink, err error)

func SetDefaultOptions

func SetDefaultOptions(opts ...MOption)

Set options on the default metric client

func SetDefaultSink

func SetDefaultSink(sink Sink)

Set the Sink factory of the default client

func Start

func Start()

Start the default client if stopped.

func Stop

func Stop()

Stops the global default metrics client

Types

type AutoFlusher

type AutoFlusher interface {
	SetFlusher(*Flusher)
}

An AutoFlusher can initiate a Flush throught the flusher at any time and needs to know the Flusher to call FlushMeter() on it

type Client

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

func NewClient

func NewClient(sink Sink, opts ...MOption) (client *Client)

NewClient returns you a client handle directly if you do not want to use the global default client. Create a new metric client with a factory object for the sink. If sink == nil, the client will not emit metrics until a Sink is set.

Example
package main

import (
	"github.com/One-com/gonelog/metric"
	"log"
	"strconv"
	"time"
)

func main() {

	var _flushPeriod = 4 * time.Second

	sink, err := metric.NewTestSink("prefix", 1432)
	if err != nil {
		log.Fatal(err)
	}
	flushPeriod := metric.FlushInterval(_flushPeriod)

	c := metric.NewClient(sink, flushPeriod)

	gauge := c.NewGauge("gauge", flushPeriod)
	timer := c.NewTimer("timer")
	histo := c.NewHistogram("histo", flushPeriod)
	counter := c.NewCounter("counter", flushPeriod)
	set := c.NewSet("set", flushPeriod)

	g := 100
	for g != 0 {
		counter.Inc(1)
		gauge.Update(uint64(g))
		timer.Update(time.Duration(g) * time.Millisecond)
		histo.Update(int64(g))
		set.Update(strconv.FormatInt(int64(g), 10))

		time.Sleep(time.Second)
		g--
	}
	c.Stop()

}
Output:

func (*Client) NewCounter

func (c *Client) NewCounter(name string, opts ...MOption) *Counter

func (*Client) NewGauge

func (c *Client) NewGauge(name string, opts ...MOption) *GaugeUint64

NewGauge is alias for NewGaugeUint64

func (*Client) NewGaugeFloat64

func (c *Client) NewGaugeFloat64(name string, opts ...MOption) *GaugeFloat64

================================================================== An Float64 Gauge.

func (*Client) NewGaugeInt64

func (c *Client) NewGaugeInt64(name string, opts ...MOption) *GaugeInt64

An Int64 Gauge. Can be used as a go-metric client side gauge or counter

func (*Client) NewGaugeUint64

func (c *Client) NewGaugeUint64(name string, opts ...MOption) *GaugeUint64

NewGaugeUint64 returns a standard gauge.

func (*Client) NewHistogram

func (c *Client) NewHistogram(name string, opts ...MOption) *Histogram

func (*Client) NewSet

func (c *Client) NewSet(name string, opts ...MOption) *Set

func (*Client) NewTimer

func (c *Client) NewTimer(name string, opts ...MOption) *Timer

func (*Client) SetOptions

func (c *Client) SetOptions(opts ...MOption)

Set options on a client - like the flush interval for metrics which haven't them selves a fixed flush interval

func (*Client) SetSink

func (c *Client) SetSink(sink Sink)

The the Sink factory of the client

func (*Client) Start

func (c *Client) Start()

Start a stopped client

func (*Client) Stop

func (c *Client) Stop()

Stop a Client from flushing data. If any AutoFlusher meters are still in use they will still flush when overflown.

type Counter

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

Counter is different from a GaugeInt64 in that it is reset to zero every time its flushed - and thus being server-side maintained.

func NewCounter

func NewCounter(name string, opts ...MOption) *Counter

A server side maintained counter. "Server side" meaning that it's reset to 0 every time it's sent to the server and the tally is kept on the server This poses the risk of the server-side absolute value to drift in case of increments lost in transit. However, it also allows several processes to update the same counter. If you want to have a client side counter, use GaugeInt64

func (*Counter) Dec

func (c *Counter) Dec(i int64)

func (*Counter) Flush

func (c *Counter) Flush(f FlusherSink)

func (*Counter) Inc

func (c *Counter) Inc(val int64)

func (*Counter) Mtype

func (c *Counter) Mtype() int

func (*Counter) Name

func (c *Counter) Name() string

type Flusher

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

func (*Flusher) Flush

func (f *Flusher) Flush()

flush all meters

func (*Flusher) FlushMeter

func (f *Flusher) FlushMeter(m Meter)

flush a single Meter

type FlusherSink

type FlusherSink interface {
	Emit(name string, mtype int, value interface{})
	EmitNumeric64(name string, mtype int, value Numeric64)
	Flush()
}

FlusherSink is a Sink for metrics data which methods are guaranteed to be called synchronized. This it can keep state like buffers without locking

type GaugeFloat64

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

A go-metric compatible float64 gauge which stores its value as a uint64 to implement Flush() fast

func (*GaugeFloat64) Flush

func (g *GaugeFloat64) Flush(f FlusherSink)

func (*GaugeFloat64) Mtype

func (g *GaugeFloat64) Mtype() int

func (*GaugeFloat64) Name

func (g *GaugeFloat64) Name() string

func (*GaugeFloat64) Update

func (g *GaugeFloat64) Update(v float64)

Update updates the gauge's value.

func (*GaugeFloat64) Value

func (g *GaugeFloat64) Value() float64

Value returns the gauge's current value.

type GaugeInt64

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

A gauge using an int64 - meaning it can be decremented to negaive values

func (*GaugeInt64) Clear

func (g *GaugeInt64) Clear()

Clear sets the counter to zero.

func (*GaugeInt64) Count

func (g *GaugeInt64) Count() int64

Count returns the current count.

func (*GaugeInt64) Dec

func (g *GaugeInt64) Dec(i int64)

Dec decrements the counter by the given amount.

func (*GaugeInt64) Flush

func (g *GaugeInt64) Flush(f FlusherSink)

func (*GaugeInt64) Inc

func (g *GaugeInt64) Inc(i int64)

Inc increments the counter by the given amount.

func (*GaugeInt64) Mtype

func (g *GaugeInt64) Mtype() int

func (*GaugeInt64) Name

func (g *GaugeInt64) Name() string

func (*GaugeInt64) Update

func (g *GaugeInt64) Update(val int64)

func (*GaugeInt64) Value

func (g *GaugeInt64) Value() int64

type GaugeUint64

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

The default gauge type using a uint64

func NewGauge

func NewGauge(name string, opts ...MOption) *GaugeUint64

================================================================== NewGauge is alias for NewGaugeUint64

func (*GaugeUint64) Dec

func (g *GaugeUint64) Dec(i int64)

Decrement the gauge value

func (*GaugeUint64) Flush

func (g *GaugeUint64) Flush(f FlusherSink)

Flush to implement Meter interface

func (*GaugeUint64) Inc

func (g *GaugeUint64) Inc(i uint64)

Increment the gauge value

func (*GaugeUint64) Mtype

func (g *GaugeUint64) Mtype() int

Mtype to implement Meter interface

func (*GaugeUint64) Name

func (g *GaugeUint64) Name() string

Name to implement Meter interface

func (*GaugeUint64) Update

func (g *GaugeUint64) Update(val uint64)

Update the gauge value

func (*GaugeUint64) Value

func (g *GaugeUint64) Value() uint64

Get the gauge value

type Histogram

type Histogram eventStream

A histogram is a series of int64 events all sent to the server

func NewHistogram

func NewHistogram(name string, opts ...MOption) *Histogram

func (*Histogram) Update

func (h *Histogram) Update(d int64)

Update the Histogram by registering a new event

type MConfig

type MConfig map[string]interface{}

type MOption

type MOption func(MConfig)

func FlushInterval

func FlushInterval(d time.Duration) MOption

type Meter

type Meter interface {
	Mtype() int
	Name() string
	Flush(FlusherSink) // Read the meter, by flushing all non-read values.
}

A Meter measures stuff and can be registered with a client to be periodically reported to the Sink

type Numeric64

type Numeric64 struct {
	Type int
	// contains filtered or unexported fields
}

Numeric64 is an internal datatype used to generalize an eventStream to hold any 64 bit numeric value. This is a performance optimation. An interface{} would require additional dynamic allocation of heap objects.

func (Numeric64) Float64

func (n Numeric64) Float64() float64

func (Numeric64) Int64

func (n Numeric64) Int64() int64

func (Numeric64) Uint64

func (n Numeric64) Uint64() uint64

type Option

type Option func(*config)

type Set

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

func NewSet

func NewSet(name string, opts ...MOption) *Set

func (*Set) Flush

func (s *Set) Flush(f FlusherSink)

func (*Set) Mtype

func (s *Set) Mtype() int

func (*Set) Name

func (s *Set) Name() string

func (*Set) SetFlusher

func (s *Set) SetFlusher(f *Flusher)

func (*Set) Update

func (s *Set) Update(val string)

type Sink

type Sink interface {
	FlusherSink() FlusherSink
}

A factory for FlusherSinks. Metric clients are given a Sink to output to.

type Timer

type Timer eventStream

Timer is like Histogram, but the event is a time.Duration. values are remembered as milliseconds

func NewTimer

func NewTimer(name string, opts ...MOption) *Timer

func (*Timer) Update

func (t *Timer) Update(d time.Duration)

Register a new duration event.

Jump to

Keyboard shortcuts

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