metric

package
v1.3.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package metric implements an abstraction for safely managing metrics in concurrent environments.

Index

Constants

View Source
const (
	// AVG_METRIC_AGE By default, we average over a one-minute period, which means the average
	// age of the metrics in the period is 30 seconds.
	AVG_METRIC_AGE float64 = 30.0

	// DECAY The formula for computing the decay factor from the average age comes
	// from "Production and Operations Analysis" by Steven Nahmias.
	DECAY = 2 / (AVG_METRIC_AGE + 1)

	// WARMUP_SAMPLES For best results, the moving average should not be initialized to the
	// samples it sees immediately. The book "Production and Operations
	// Analysis" by Steven Nahmias suggests initializing the moving average to
	// the mean of the first 10 samples. Until the VariableEwma has seen this
	// many samples, it is not "ready" to be queried for the value of the
	// moving average. This adds some memory cost.
	WARMUP_SAMPLES uint8 = 10
)
View Source
const (
	JOBSDB_PENDING_EVENTS_COUNT = "jobsdb_%s_pending_events_count"
	ALL                         = "ALL"
)
View Source
const (
	PUBLISHED_METRICS string = "published_metrics"
)

Variables

This section is empty.

Functions

func DecreasePendingEvents

func DecreasePendingEvents(tablePrefix, workspace, destType string, value float64)

DecreasePendingEvents increments three gauges, the dest & workspace-specific gauge, plus two aggregate (global) gauges

func IncreasePendingEvents

func IncreasePendingEvents(tablePrefix, workspace, destType string, value float64)

IncreasePendingEvents increments three gauges, the dest & workspace-specific gauge, plus two aggregate (global) gauges

Types

type Counter

type Counter interface {
	// Inc increments the counter by 1. Use Add to increment it by arbitrary
	// non-negative values.
	Inc()
	// Add adds the given value to the counter. It panics if the value is <
	// 0.
	Add(float64)
	// Value gets the current value of the counter.
	Value() float64
}

Counter counts monotonically increasing values

func NewCounter

func NewCounter() Counter

NewCounter creates a new counter

type Gauge

type Gauge interface {
	// Set sets the given value to the gauge.
	Set(float64)
	// Inc increments the gauge by 1. Use Add to increment it by arbitrary
	// values.
	Inc()
	// Dec decrements the gauge by 1. Use Sub to decrement it by arbitrary
	// values.
	Dec()
	// Add adds the given value to the counter.
	Add(val float64)
	// Sub subtracts the given value from the counter.
	Sub(float64)
	// SetToCurrentTime sets the current UNIX time as the gauge's value
	SetToCurrentTime()
	// Value gets the current value of the counter.
	Value() float64
	// IntValue gets the current value of the counter as an int.
	IntValue() int
	// ValueAsTime gets the current value of the counter as time.
	ValueAsTime() time.Time
}

Gauge keeps track of increasing/decreasing or generally arbitrary values. You can even use a gauge to keep track of time, e.g. when was the last time an event happened!

func NewGauge

func NewGauge() Gauge

func PendingEvents

func PendingEvents(tablePrefix, workspace, destType string) Gauge

PendingEvents gets the measurement for pending events metric

type Manager

type Manager interface {
	// GetRegistry gets a registry by its key
	GetRegistry(key string) Registry
	// Reset cleans all registries
	Reset()
}

Manager is the entry-point for retrieving metric registries

var Instance Manager = NewManager()

func NewManager added in v1.2.0

func NewManager() Manager

type Measurement

type Measurement interface {
	// GetName gets the name of the measurement
	GetName() string
	// GetTags gets the tags of the measurement
	GetTags() map[string]string
}

Measurement acts as a key in a Registry.

func PendingEventsMeasurement added in v1.2.0

func PendingEventsMeasurement(tablePrefix, workspace, destType string) Measurement

type MovingAverage

type MovingAverage interface {
	Add(float64)
	Value() float64
	Set(float64)
}

MovingAverage is the interface that computes a moving average over a time- series stream of numbers. The average may be over a window or exponentially decaying.

func NewMovingAverage

func NewMovingAverage(age ...float64) MovingAverage

NewMovingAverage constructs a MovingAverage that computes an average with the desired characteristics in the moving window or exponential decay. If no age is given, it constructs a default exponentially weighted implementation that consumes minimal memory. The age is related to the decay factor alpha by the formula given for the DECAY constant. It signifies the average age of the samples as time goes to infinity.

type Registry

type Registry interface {
	// GetCounter gets a counter by key. If a value for this key
	// already exists but corresponds to another measurement type,
	// e.g. a Gauge, an error is returned
	GetCounter(Measurement) (Counter, error)

	// MustGetCounter gets a counter by key. If a value for this key
	// already exists but corresponds to another measurement type,
	// e.g. a Gauge, it panics
	MustGetCounter(Measurement) Counter

	// GetGauge gets a gauge by key. If a value for this key
	// already exists but corresponds to another measurement
	// type, e.g. a Counter, an error is returned
	GetGauge(Measurement) (Gauge, error)

	// MustGetGauge gets a gauge by key. If a value for this key
	// already exists but corresponds to another measurement type,
	// e.g. a Counter, it panics
	MustGetGauge(Measurement) Gauge

	// GetMovingAverage gets a moving average by key. If a value for this key
	// already exists but corresponds to another measurement
	// type, e.g. a Counter, an error is returned
	GetSimpleMovingAvg(Measurement) (MovingAverage, error)

	// MustGetMovingAverage gets a moving average by key. If a value for this key
	// already exists but corresponds to another measurement type,
	// e.g. a Counter, it panics
	MustGetSimpleMovingAvg(Measurement) MovingAverage

	// GetMovingAverage gets a moving average by key. If a value for this key
	// already exists but corresponds to another measurement
	// type, e.g. a Counter, an error is returned
	GetVarMovingAvg(m Measurement, age float64) (MovingAverage, error)

	// MustGetMovingAverage gets a moving average by key. If a value for this key
	// already exists but corresponds to another measurement type,
	// e.g. a Counter, it panics
	MustGetVarMovingAvg(m Measurement, age float64) MovingAverage

	// Range scans across all metrics
	Range(f func(key, value interface{}) bool)

	// GetMetricsByName gets all metrics with this name
	GetMetricsByName(name string) []TagsWithValue
}

Registry is a safe way to capture metrics in a highly concurrent environment. The registry is responsible for creating and storing the various measurements and guarantees consistency when competing goroutines try to update the same measurement at the same time.

E.g. assuming that you already have created a new registry

registry :=  NewRegistry()

the following is guaranteed to be executed atomically:

registry.MustGetCounter("key").Inc()

func NewRegistry

func NewRegistry() Registry

type SimpleEWMA

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

A SimpleEWMA represents the exponentially weighted moving average of a series of numbers. It WILL have different behavior than the VariableEWMA for multiple reasons. It has no warm-up period and it uses a constant decay. These properties let it use less memory. It will also behave differently when it's equal to zero, which is assumed to mean uninitialized, so if a value is likely to actually become zero over time, then any non-zero value will cause a sharp jump instead of a small change. However, note that this takes a long time, and the value may just decays to a stable value that's close to zero, but which won't be mistaken for uninitialized. See http://play.golang.org/p/litxBDr_RC for example.

func (*SimpleEWMA) Add

func (e *SimpleEWMA) Add(value float64)

Add adds a value to the series and updates the moving average.

func (*SimpleEWMA) Set

func (e *SimpleEWMA) Set(value float64)

Set sets the EWMA's value.

func (*SimpleEWMA) Value

func (e *SimpleEWMA) Value() float64

Value returns the current value of the moving average.

type Tags

type Tags map[string]string

type TagsWithValue

type TagsWithValue struct {
	Tags  Tags
	Value interface{}
}

type VariableEWMA

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

VariableEWMA represents the exponentially weighted moving average of a series of numbers. Unlike SimpleEWMA, it supports a custom age, and thus uses more memory.

func (*VariableEWMA) Add

func (e *VariableEWMA) Add(value float64)

Add adds a value to the series and updates the moving average.

func (*VariableEWMA) Set

func (e *VariableEWMA) Set(value float64)

Set sets the EWMA's value.

func (*VariableEWMA) Value

func (e *VariableEWMA) Value() float64

Value returns the current value of the average, or 0.0 if the series hasn't warmed up yet.

Jump to

Keyboard shortcuts

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