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 ¶
- type BasicResolution
- func (r BasicResolution) Join(t time.Time, key string) time.Time
- func (r BasicResolution) SampleKey(t time.Time) string
- func (r BasicResolution) SampleTimestamp(t time.Time) time.Time
- func (r BasicResolution) SampleTimestamps(first, last time.Time) []time.Time
- func (r BasicResolution) SetSize() int
- func (r BasicResolution) SetTimestamp(t time.Time) time.Time
- func (r BasicResolution) SetTimestamps(first, last time.Time) []time.Time
- func (r BasicResolution) Split(t time.Time) (time.Time, string)
- type Bulk
- type Collection
- func (c *Collection) AggregateSamples(first, last time.Time, metrics []string, tags bson.M) (*TimeSeries, error)
- func (c *Collection) AggregateSets(first, last time.Time, metrics []string, tags bson.M) (*TimeSeries, error)
- func (c *Collection) Bulk() *Bulk
- func (c *Collection) EnsureIndexes(removeAfter time.Duration) error
- func (c *Collection) Insert(timestamp time.Time, metrics map[string]float64, tags bson.M) error
- type Metric
- type Resolution
- type Sample
- type TimeSeries
- func (ts *TimeSeries) Avg(metric string) float64
- func (ts *TimeSeries) Max(metric string) float64
- func (ts *TimeSeries) Min(metric string) float64
- func (ts *TimeSeries) Null(timestamps []time.Time, metrics []string) *TimeSeries
- func (ts *TimeSeries) Num(metric string) int64
- func (ts *TimeSeries) Sum(metric string) float64
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 ¶
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.
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.
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) 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.
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 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.