monup

package module
v0.0.0-...-6227d99 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2015 License: MIT Imports: 8 Imported by: 0

README

monup

[EXPERIMENT] mongo metric rollups by tailing the oplog

https://godoc.org/github.com/supershabam/monup

usage

#!/usr/bin/env bash
# run the marker service against localhost
# read incoming metrics from the samples collection in the metrics database
# write triggers to the triggers collection in the metrics database
# trigger rollups for datapoints in a 5 minute window, 15 minute window, and 1 hour window

marker -url="mongodb://localhost" -ns="metrics.samples" -tns="metrics.triggers" -over="5m,15m,1h"
#!/usr/bin/env bash
# run the roller service against localhost
# poll for triggers from the triggers collection in the metrics database
# get raw datapoints from the samples database in the metrics collection
# also write datapoints back out to the "samples_#{window}" collection e.g. samples_5m

roller -url="mongodb://localhost" -ns="metrics.samples" -tns="metrics.triggers"

what you get

Now, with the marker and roller services running, whenever you write a datapoint document into the samples collection, you'll get a rollup computed.

// insert datapoint by any application
db.samples.insert({
  name: 'http.request_ms',
  at: new Date(),
  value: 233
})

// rollups are automatically created
db.samples_5m.find({name: 'http.request_ms'})
db.samples_15m.find({name: 'http.request_ms'})
db.samples_1h.find({name: 'http.request_ms'})

MongoDB collection setup

metrics.samples should be a capped collection with enough space to hold all your different metric names for 3x the length of your longest rollup window.

metrics.samples should have an index on {name: 1, at: 1}

metrics.triggers should have an index on {name: 1, at: 1, over: 1}

Documentation

Overview

Package monup is an experiment in augmenting MongoDB data by tailing the oplog. In this case, we're computing statistiacal rollups of data.

There are two main processes to run: `marker` and `roller`. Marker tails the oplog so that new datapoint inserts mark the rollup values they effect to be computed. Roller computes those rollups.

This is an experiment and probably shouldn't be used in production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DatapointsToFloats

func DatapointsToFloats(dps []Datapoint) []float64

DatapointsToFloats lets us work with a natural slice of floats

func OplogCh

func OplogCh(sess *mgo.Session, query bson.M) (<-chan Oplog, <-chan error)

OplogCh returns a channel of oplogs that match the given query as they come into the database. If there is an error along the way, the channel is terminated and there will be a non-nil error ready to take from the error channel

TODO let caller have a way to stop iteration

Types

type Datapoint

type Datapoint struct {
	Value float64 `bson:"value"`
}

Datapoint exists to ease parsing out of MongoDB

type Marker

type Marker struct {
	Sess             *mgo.Session
	Namespace        string
	Over             []time.Duration
	TriggerNamespace string
}

Marker encapsulates the logic for running a marker

func (Marker) Run

func (m Marker) Run() error

Run executes a marker until an error is encountered

type Oplog

type Oplog struct {
	Timestamp    bson.MongoTimestamp `bson:"ts"`
	HistoryID    int64               `bson:"h"`
	MongoVersion int                 `bson:"v"`
	Operation    string              `bson:"op"`
	Namespace    string              `bson:"ns"`
	Object       bson.Raw            `bson:"o"`
	QueryObject  bson.Raw            `bson:"o2"`
}

Oplog is a document returned by tailing the replication log

func LatestOplog

func LatestOplog(sess *mgo.Session) (Oplog, error)

LatestOplog returns the most recent oplog from the database

type Roller

type Roller struct {
	Sess             *mgo.Session
	Namespace        string
	TriggerNamespace string
	Period           time.Duration
}

Roller polls for triggers and calculated rollups

func (Roller) Rollup

func (r Roller) Rollup(trigger Trigger) error

Rollup writes a rollup result based on the current samples in the database

func (Roller) Run

func (r Roller) Run() error

Run executes a roller until an error is encountered

type Rollup

type Rollup struct {
	Name string    `bson:"name"`
	At   time.Time `bson:"at"`
	Min  float64   `bson:"min"`
	P2   float64   `bson:"p2"`
	P9   float64   `bson:"p9"`
	P25  float64   `bson:"p25"`
	P50  float64   `bson:"p50"`
	P75  float64   `bson:"p75"`
	P91  float64   `bson:"p91"`
	P98  float64   `bson:"p98"`
	Max  float64   `bson:"max"`
}

Rollup is an object stored into a rollup collection

type Sample

type Sample struct {
	Name  string    `bson:"name"`
	Value float64   `bson:"value"`
	At    time.Time `bson:"at"`
}

Sample is a single datapoint. Any process can write to MongoDB following this format and the mark/roll process will handle the data.

type Trigger

type Trigger struct {
	Name  string
	At    time.Time
	Over  time.Duration
	After time.Time
}

Trigger is queued into a mongo collection to signal a rollup to create at a later time. It's essentially turning MongoDB into a queue, which isn't the best task for MongoDB.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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