metrics

package module
v0.0.0-...-8887d3f Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2015 License: MIT Imports: 9 Imported by: 0

README

Metrics

Documentation

A go library for printing metrics in an l2met compatible format.

Documentation

Overview

Package metrics is a go library for sampling, counting and timing go code to be output in the l2met format.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ValueInvalidErr      = errors.New("value must be one of [int, uint, int32, uint32, int64, uint64]")
	ValueOverflowErr     = errors.New("value for uint64 too large to be converted to int64")
	InvalidMetricTypeErr = errors.New("metric type must be one of [count, sample, measure]")
)
View Source
var (
	// DefaultDrain is the Drainer that will be used to drain metrics.
	DefaultDrain = Drainer(&LogDrain{})

	// Source is the root source that these metrics are coming from.
	Source string

	// DefaultNamespace is the default namespace to output metrics. By default,
	// no namespace.
	DefaultNamespace Namespace
)
View Source
var DefaultFormatter = Formatter(&l2metFormatter{})

DefaultFormatter is the default Formatter to use.

View Source
var DefaultInterval time.Duration = 30 * time.Second

DefaultInterval sets how often the metrics Runtime will sample and emit stats.

View Source
var DefaultNowFunc = func() time.Time {
	return time.Now()
}

DefaultNowFunc is the default function returning a time.Time for "now".

Functions

func Count

func Count(name string, v interface{}) error

Count logs a count metric in the root namespace.

Example
Count("user.signup", 1)
Output:

count#user.signup=1

func Measure

func Measure(name string, v interface{}, units string) error

Measure logs a measurement metric in the root namespace.

Example
Measure("request.time.2xx", 12.14, "ms")
Output:

measure#request.time.2xx=12.14ms

func Runtime

func Runtime()

Runtime enters into a loop, sampling and outputing the runtime stats periodically.

func Sample

func Sample(name string, v interface{}, units string) error

Sample logs a sample metric in the root namespace.

Example
Sample("goroutine", 1, "")
Output:

sample#goroutine=1

func SampleEvery

func SampleEvery(t time.Duration)

SampleEvery enters a loop, sampling at the specified interval

Types

type Drainer

type Drainer interface {
	Drain(Metric) error
}

Drainer is an interface that can drain a metric to it's output.

type Formatter

type Formatter interface {
	Format(Metric) string
}

Formatter is an interface for formatting a metric into a string.

type LocalStoreDrain

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

func (*LocalStoreDrain) Drain

func (d *LocalStoreDrain) Drain(m Metric) error

Drain records metrics to the local store.

func (*LocalStoreDrain) Flush

func (d *LocalStoreDrain) Flush()

func (*LocalStoreDrain) Store

func (d *LocalStoreDrain) Store() map[string][]Metric

type LogDrain

type LogDrain struct {
	// Formatter to use to format the metric into a string before outputting.
	Formatter Formatter

	DrainFunc func(string)
	Logger    *log.Logger
}

LogDrain is a Drainer implementation that logs the metrics to Stdout in l2met format.

func (*LogDrain) Drain

func (d *LogDrain) Drain(m Metric) error

Drain logs the metric to Stdout.

type Metric

type Metric interface {
	// Name returns the name of the metric (e.g. request.time.2xx)
	Name() string

	// Type returns the type of metric.
	Type() string

	// Value returns the value of the metric.
	Value() interface{}

	// Units returns the units of the metric.
	Units() string
}

Metric represents an individual count/sample/measurement and encapsulates information about it.

type Namespace

type Namespace string

Namespace represents a metric prefix. (e.g. "memcached.")

func (Namespace) Count

func (n Namespace) Count(name string, v interface{}) error

Count creates a count metric and drains it.

func (Namespace) CountMetric

func (n Namespace) CountMetric(name string, v interface{}) Metric

CountMetric returns a new Metric for a count.

func (Namespace) Measure

func (n Namespace) Measure(name string, v interface{}, units string) error

Measure creates a measure metric and drains it.

func (Namespace) MeasureMetric

func (n Namespace) MeasureMetric(name string, v interface{}, units string) Metric

MeasureMetric returns a new Metric for a measure.

func (Namespace) NewMetric

func (n Namespace) NewMetric(t, name string, v interface{}, units string) Metric

NewMetric returns a new Metric.

func (Namespace) Sample

func (n Namespace) Sample(name string, v interface{}, units string) error

Sample creates a sample metric and drains it.

func (Namespace) SampleMetric

func (n Namespace) SampleMetric(name string, v interface{}, units string) Metric

SampleMetric returns a new Metric for a sample.

func (Namespace) Time

func (n Namespace) Time(name string) *Timer

Time starts a timer and returns it.

type NullDrain

type NullDrain struct{}

NullDrain is a Drainer implementation that does nothing.

func (*NullDrain) Drain

func (d *NullDrain) Drain(m Metric) error

Drain implements the Drainer interface.

type RuntimeSample

type RuntimeSample struct {
	*runtime.MemStats
	NumGoroutine int
}

RuntimeSample represents a sampling of the runtime stats.

func NewRuntimeSample

func NewRuntimeSample() *RuntimeSample

NewRuntimeSample samples the current runtime and returns a RuntimeSample.

func (*RuntimeSample) Drain

func (r *RuntimeSample) Drain()

Drain drains all of the metrics.

type StatsdClient

type StatsdClient interface {
	Incr(name string, count int64) error
	Gauge(name string, value int64) error
	Timing(name string, ms int64) error
}

type StatsdDrain

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

StatsdDrain is a Drainer that records metrics to a statsd server.

func NewStatsdDrain

func NewStatsdDrain(c StatsdClient, tmpl string) (*StatsdDrain, error)

NewStatsdDrain takes a statsd client and a template string. The template string is used to construct the metric name with a Metric as its context.

func (*StatsdDrain) Drain

func (d *StatsdDrain) Drain(m Metric) error

Drain records metrics to a statsd server.

type Timer

type Timer struct {
	// NowFunc is a function to return a time.Time representing "now". Zero value
	// is DefaultNowFunc.
	NowFunc func() time.Time
	// contains filtered or unexported fields
}

Timer is an implementation of the Metric interface for timing things.

func NewTimer

func NewTimer(name string) *Timer

NewTimer returns a new Timer metric.

func Time

func Time(name string) *Timer

Time starts a timer and returns it.

Example
t := Time("request.time")
t.NowFunc = func() time.Time {
	return t.start.Add(527 * time.Millisecond)
}
t.Done()
Output:

measure#request.time=527ms

func (*Timer) Done

func (t *Timer) Done()

Done stops the timer and drains it.

func (*Timer) Milliseconds

func (t *Timer) Milliseconds() int64

Milliseconds returns the number of milliseconds elapsed.

func (Timer) Name

func (m Timer) Name() string

Methods to implement the Metric interface

func (*Timer) Start

func (t *Timer) Start()

Start the timer.

func (*Timer) Stop

func (t *Timer) Stop()

Stop stops the timer.

func (Timer) Type

func (m Timer) Type() string

func (Timer) Units

func (m Timer) Units() string

func (*Timer) Value

func (t *Timer) Value() interface{}

Value returns the difference between start and end in milliseconds.

Jump to

Keyboard shortcuts

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