metrics

package module
v0.0.0-...-5ed4e60 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2017 License: MIT Imports: 9 Imported by: 6

README

Easy-metrics GoDoc Go Report Card Build Status Coverage Status

A Go library that provides easy to use, standalone metrics and exposes it via HTTP. It provides metrics snapshots on defined interval and stores it in a pool. That can be used to track performance and other application indicators without necessity of export into external applications.

Features

Metrics can be stored in a TrackRegistry that provides snapshots over defined interval and stores it in a pool. Snapshots like other metrics available via HTTP. So you don't need to collect metrics in some external applications like statsd, graphana, elk etc.

Installation

Just go get:

go get github.com/admobi/easy-metrics

Or to update:

go get -u github.com/admobi/easy-metrics

Usage

At the core of metrics is two subjects, metric which stores a single numerical value and registry which stores pool of metrics.

Add import to project:

import "github.com/admobi/easy-metrics"

Create and update metrics:

// Create metrics
c := metrics.NewCounter("requests")
g := metrics.NewGauge("rates")
// Create registry
r, err := metrics.NewRegistry("Statistics")
// Register metrics
r.AddMetrics(c, g)

// Change metric. Increase by 1 
c.Inc()
// or the same
c.Add(1)

// Add delta to gauge
g.Add(3.14)

All operations are thread safe.

Snapshots

r := metrics.NewTrackRegistry("Stat", 30, time.Second, false)
c := metrics.NewCounter("requests")
r.AddMetrics(c)

In this case TrackRegistry will take metric snapshots every second and stores last 30 results. For largest interval you may align timer by setting align to true . For example:

r := metrics.NewTrackRegistry("Stat", 30, time.Hour, true)

If application starts at 11:15, snapshots will be created at 12:00, 13:00 etc. (not 12:15, 13:15)

Monitoring

Add

go func() {
		log.Println(http.ListenAndServe(":9911", nil))
}()

And then go to http://localhost:9911/easy-metrics. You'll see the list of registries. Chose one and you should see something like that:

It uses Plotly library for charts.

Contribution

Contributions are welcome. Feel free to create issue or better PR :)

License

MIT License

Documentation

Overview

Package metrics provide easy to use, stand alone metrics and exposes it via HTTP. It can create metric's snapshots each defined interval and store it in a pool. That useful for fast monitoring of current performance without necessity of data export to external applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRegistries

func GetRegistries() map[string]Registry

GetRegistries returns all registries

Types

type Counter

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

Counter is a cumulative metric that represents a single numerical value that only ever goes up. Satsfies Metric interface.

func NewCounter

func NewCounter(name string) *Counter

NewCounter returns new counter that satsfies Metric interface.

func (*Counter) Add

func (c *Counter) Add(delta uint64)

Add adds delta to counter value.

func (*Counter) Get

func (c *Counter) Get() interface{}

Get returns counter value.

func (*Counter) Inc

func (c *Counter) Inc()

Inc increases counter value by 1

func (*Counter) Name

func (c *Counter) Name() string

Name returns metric name.

func (*Counter) String

func (c *Counter) String() string

String returns formated representation of counter value.

type DefaultRegistry

type DefaultRegistry struct {
	sync.Mutex
	// contains filtered or unexported fields
}

DefaultRegistry its a plain container for metrics. It just keeps metric

func (*DefaultRegistry) AddMetrics

func (r *DefaultRegistry) AddMetrics(metrics ...Metric) error

AddMetrics adds one or more metrics into registry

func (*DefaultRegistry) GetMetricByName

func (r *DefaultRegistry) GetMetricByName(name string) (Metric, error)

GetMetricByName returns metric by given name

func (*DefaultRegistry) GetMetrics

func (r *DefaultRegistry) GetMetrics() map[string]Metric

GetMetrics returns map of registred metrics

type ErrEmptyMetricName

type ErrEmptyMetricName struct{}

ErrEmptyMetricName error type on empty metric name.

func (ErrEmptyMetricName) Error

func (e ErrEmptyMetricName) Error() string

type ErrEmptyRegistryName

type ErrEmptyRegistryName struct{}

ErrEmptyRegistryName error type on empty registry name.

func (ErrEmptyRegistryName) Error

func (e ErrEmptyRegistryName) Error() string

type ErrMetricExists

type ErrMetricExists string

ErrMetricExists error type - metric with provided name exists.

func (ErrMetricExists) Error

func (e ErrMetricExists) Error() string

type ErrMetricUnknown

type ErrMetricUnknown string

ErrMetricUnknown error type on provided metric name is unknown.

func (ErrMetricUnknown) Error

func (e ErrMetricUnknown) Error() string

type ErrRegistryExists

type ErrRegistryExists string

ErrRegistryExists error type - registry with provided is exists.

func (ErrRegistryExists) Error

func (e ErrRegistryExists) Error() string

type ErrRegistryUnknown

type ErrRegistryUnknown string

ErrRegistryUnknown error type on provided registry name is unknown.

func (ErrRegistryUnknown) Error

func (e ErrRegistryUnknown) Error() string

type Gauge

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

Gauge is a metric that represents a single float64 value that can arbitrarily go up and down. Satsfies Metric interface.

func NewGauge

func NewGauge(name string) *Gauge

NewGauge returns new gauge metric that satsfies Metric interface.

func (*Gauge) Add

func (g *Gauge) Add(delta float64)

Add adds delta to gauge value.

func (*Gauge) Get

func (g *Gauge) Get() interface{}

Get returns gauge value.

func (*Gauge) Name

func (g *Gauge) Name() string

Name returns metric name.

func (*Gauge) Set

func (g *Gauge) Set(value float64)

Set sets gauge value to value.

func (*Gauge) String

func (g *Gauge) String() string

String returns formated representation of gauge value.

func (*Gauge) Sub

func (g *Gauge) Sub(delta float64)

Sub substarcts delta from gauge value.

type Metric

type Metric interface {
	// Get returns a metric value.
	Get() interface{}
	// String returns formatted value of metric.
	String() string
	// Name returns metric name.
	Name() string
	// contains filtered or unexported methods
}

Metric is an abstract type of metric.

type Registry

type Registry interface {
	AddMetrics(metrics ...Metric) error
	GetMetricByName(name string) (Metric, error)
	GetMetrics() map[string]Metric
}

Registry is an abstract type for metric countainer

func GetRegistryByName

func GetRegistryByName(name string) (Registry, error)

GetRegistryByName returns Registry by given name

func NewRegistry

func NewRegistry(name string) (Registry, error)

NewRegistry creates a new registry and adds it into the registry map

type Snapshot

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

Snapshot is a struct for snapshoted metrics It stores snapshot timestamp and map of metrics

func (*Snapshot) GetMetricByName

func (am *Snapshot) GetMetricByName(name string) (Metric, error)

GetMetricByName returns Metric by given name

func (*Snapshot) GetMetrics

func (am *Snapshot) GetMetrics() map[string]Metric

GetMetrics returns all metrics from snapshot

func (*Snapshot) GetTimestamp

func (am *Snapshot) GetTimestamp() time.Time

GetTimestamp returns timestamp of metrics snapshot

type TrackRegistry

type TrackRegistry struct {
	DefaultRegistry
	// contains filtered or unexported fields
}

TrackRegistry is a registry that can stores the pool of snapshoted metrics.

func (*TrackRegistry) GetSnapshots

func (r *TrackRegistry) GetSnapshots() []Snapshot

GetSnapshots returns slice of swaped metrics

type Tracker

type Tracker interface {
	Registry
	GetSnapshots() []Snapshot
}

Tracker is an abstract type for countainer with metrics snaphshot Implements Registry interface

func NewTrackRegistry

func NewTrackRegistry(name string, capacity int, interval time.Duration, align bool) (Tracker, error)

NewTrackRegistry creates a new TrackRegistry and adds it into the registry map. It makes the snapshots of metric on each interval and keeps it in pool with specified capacity. If align is set to true, metric's archiving will be align by interval duration. For examaple:

// If application will be started at 12:10:13

// this registry will create snaphosts at 13:10:13, 14:10:13, 15:10:13 etc.
NewTrackRegistry("stat per minute", 10, time.Hour, false)

// but this will create snaphosts at 13:00:00, 14:00:00, 15:00:00 etc.
NewTrackRegistry("stat per minute", 10, time.Hour, true)

Jump to

Keyboard shortcuts

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