mgots

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2019 License: MIT Imports: 5 Imported by: 0

README

mgots

Build Status Coverage Status GoDoc Release Go Report Card

A wrapper for mgo that turns MongoDB into a time series database.

Example

// get time series collection
coll := Wrap(db.C("metrics"), OneMinuteOf60Seconds)

// ensure indexes
err := coll.EnsureIndexes(0)
if err != nil {
    panic(err)
}

// prepare tags
tags := bson.M{"server": "localhost"}

// add some metrics
from := time.Now()
to := time.Now()
for i := 0; i < 100; i++ {
    coll.Insert(to, map[string]float64{
        "value": float64(i),
    }, tags)

    to = to.Add(time.Second)
}

// get data
ts, err := coll.AggregateSamples(from, to, []string{"value"}, tags)
if err != nil {
    panic(err)
}

// print
fmt.Println(ts.Num("value"))
fmt.Println(ts.Sum("value"))
fmt.Println(ts.Min("value"))
fmt.Println(ts.Max("value"))
fmt.Println(ts.Avg("value"))

// Output:
// 100
// 4950
// 0
// 99
// 49.5

Documentation

Overview

Package mgots is a wrapper for mgo that turns MongoDB into a time series database.

Example
// get time series collection
coll := Wrap(db.C("metrics"), OneMinuteOf60Seconds)

// ensure indexes
err := coll.EnsureIndexes(0)
if err != nil {
	panic(err)
}

// prepare tags
tags := bson.M{"server": "localhost"}

// add some metrics
from := time.Now()
to := time.Now()
for i := 0; i < 100; i++ {
	coll.Insert(to, map[string]float64{
		"value": float64(i),
	}, tags)

	to = to.Add(time.Second)
}

// get data
ts, err := coll.AggregateSamples(from, to, []string{"value"}, tags)
if err != nil {
	panic(err)
}

// print
fmt.Println(ts.Num("value"))
fmt.Println(ts.Sum("value"))
fmt.Println(ts.Min("value"))
fmt.Println(ts.Max("value"))
fmt.Println(ts.Avg("value"))
Output:

100
4950
0
99
49.5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicResolution

type BasicResolution int

BasicResolution defines the granularity of the saved metrics.

const (
	OneMinuteOf60Seconds BasicResolution = iota
	OneHourOf60Minutes
	OneDayOf24Hours
	OneMonthOfUpTo31Days
	OneHourOf3600Seconds
	OneDayOf1440Minutes
)

The following basic resolutions are available:

func (BasicResolution) Join

func (r BasicResolution) Join(t time.Time, key string) time.Time

Join will return the timestamp of a single sample based on the start of a set and the key of the sample.

func (BasicResolution) SampleKey

func (r BasicResolution) SampleKey(t time.Time) string

SampleKey will return the sample key for given time.

func (BasicResolution) SampleTimestamp

func (r BasicResolution) SampleTimestamp(t time.Time) time.Time

SampleTimestamp will return the sample timestamp for the given time.

func (BasicResolution) SampleTimestamps

func (r BasicResolution) SampleTimestamps(first, last time.Time) []time.Time

SampleTimestamps will return a list sample timestamps for the given time range.

func (BasicResolution) SetSize

func (r BasicResolution) SetSize() int

SetSize will return the number of samples per set.

func (BasicResolution) SetTimestamp

func (r BasicResolution) SetTimestamp(t time.Time) time.Time

SetTimestamp will return the set timestamp for the given time.

func (BasicResolution) SetTimestamps

func (r BasicResolution) SetTimestamps(first, last time.Time) []time.Time

SetTimestamps will return a list set timestamps for the given time range.

func (BasicResolution) Split

func (r BasicResolution) Split(t time.Time) (time.Time, string)

Split will return the set timestamp and sample key for the given time.

type Bulk

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

A Bulk represents an operation that can be used to add multiple metrics at once. It is a wrapper around the mgo.Bulk type.

func (*Bulk) Insert

func (b *Bulk) Insert(timestamp time.Time, metrics map[string]float64, tags bson.M)

Insert will queue the insert in the bulk operation.

func (*Bulk) Run

func (b *Bulk) Run() error

Run will insert all queued insert operations.

type Collection

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

A Collection represents a time series enabled collection. It is a wrapper around the mgo.Collection type.

func Wrap

func Wrap(coll *mgo.Collection, res Resolution) *Collection

Wrap will take a mgo.Collection and return a Collection.

func (*Collection) AggregateSamples

func (c *Collection) AggregateSamples(first, last time.Time, metrics []string, tags bson.M) (*TimeSeries, error)

AggregateSamples will aggregate all samples within sets that match the specified time range and tags.

func (*Collection) AggregateSets

func (c *Collection) AggregateSets(first, last time.Time, metrics []string, tags bson.M) (*TimeSeries, error)

AggregateSets will aggregate only set level metrics matching the specified time range and tags.

func (*Collection) Bulk

func (c *Collection) Bulk() *Bulk

Bulk will return a new bulk operation.

func (*Collection) EnsureIndexes

func (c *Collection) EnsureIndexes(removeAfter time.Duration) error

EnsureIndexes will ensure that the necessary indexes have been created. If removeAfter is specified, sets are automatically removed when their start timestamp falls behind the specified duration.

Note: It is recommended to create custom indexes that support the exact nature of data and access patterns.

func (*Collection) Insert

func (c *Collection) Insert(timestamp time.Time, metrics map[string]float64, tags bson.M) error

Insert will immediately write the specified metrics to the collection.

type Metric

type Metric struct {
	Max float64
	Min float64
	Num int64
	Sum float64
}

A Metric is a single aggregated metric in a sample.

type Resolution

type Resolution interface {
	// Split should return the set timestamp and sample key for the given time.
	Split(t time.Time) (time.Time, string)

	// Join should return the timestamp of a single sample based on the timestamp
	// of a set and the key of the sample.
	Join(t time.Time, key string) time.Time

	// SetSize should return the number of samples per set.
	SetSize() int

	// SetTimestamp should return the set timestamp for the given time.
	SetTimestamp(t time.Time) time.Time

	// SetTimestamps should return a list set timestamps for the given time range.
	SetTimestamps(first time.Time, last time.Time) []time.Time

	// SampleKey should return the sample key for given time.
	SampleKey(t time.Time) string

	// SampleTimestamp should return the sample timestamp for the given time.
	SampleTimestamp(t time.Time) time.Time

	// SampleTimestamps should return a list sample timestamps for the given time
	// range.
	SampleTimestamps(first, last time.Time) []time.Time
}

A Resolution specifies the granularity of saved samples and the organization in sets.

type Sample

type Sample struct {
	Start   time.Time
	Metrics map[string]Metric
}

A Sample is a single aggregated sample in a time series.

type TimeSeries

type TimeSeries struct {
	Samples []Sample
}

A TimeSeries is a list of samples.

func (*TimeSeries) Avg

func (ts *TimeSeries) Avg(metric string) float64

Avg returns the average measured value for the given time series.

func (*TimeSeries) Max

func (ts *TimeSeries) Max(metric string) float64

Max returns the largest measured value for the given time series.

func (*TimeSeries) Min

func (ts *TimeSeries) Min(metric string) float64

Min returns the smallest measured value for the given time series.

func (*TimeSeries) Null

func (ts *TimeSeries) Null(timestamps []time.Time, metrics []string) *TimeSeries

Null will return a new TimeSeries that includes samples for the specified timestamps or a null value if no sample exists in the time series.

func (*TimeSeries) Num

func (ts *TimeSeries) Num(metric string) int64

Num returns the number of measured values for the given time series.

func (*TimeSeries) Sum

func (ts *TimeSeries) Sum(metric string) float64

Sum returns the sum of all measured values for the given time series.

Jump to

Keyboard shortcuts

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