Reference / Metrics API for Go
Metrics API for Go
Every public type, function and constant of the Metrics engine in tinyshed.org/tinystore/metrics, generated from its source. The Metrics guide explains how to use them, and the Bun and Node, Python and Rust pages list the same API.
Delete
func Delete(ctx context.Context, store *tinystore.Store, name string, labels Labels) (bool, error)Delete removes the one series of exactly this name and these labels, whether its samples still read or not, and says whether the store held it; nil labels are the series without any.
Describe
func Describe(ctx context.Context, store *tinystore.Store, name string, description Description) errorDescribe sets a name's unit and help in place of what it had: what is not given is removed. They belong to the name, so a Delete of its series leaves them.
ExponentialBuckets
func ExponentialBuckets(start, factor float64, count int) []float64ExponentialBuckets is upper bounds from start, each factor times the one before, as Prometheus' ExponentialBuckets: ExponentialBuckets(1, 2, 14) is 1, 2, 4 … 8192.
Flush
func Flush(ctx context.Context, store *tinystore.Store) errorFlush sends what every instrument changed and has the store write its totals now, as it does every 15 seconds. One the store cannot be reached for fails as a call does, and what it carried goes once it can be; what the store refused since the flush before, and what a store that started again may have counted, fail it, Invalid and OutcomeUnknown, each once.
Ingest
func Ingest(ctx context.Context, store *tinystore.Store, series ...Series) errorIngest writes series and their samples, every one or none: a refused series names itself by its labels. It returns once they are written.
OpenGaugeFunc
func OpenGaugeFunc(store *tinystore.Store, name string, read func(ctx context.Context) (float64, error), opts ...Option) errorOpenGaugeFunc is a gauge whose value read gives every 15 seconds and at Flush, of the set of labels Labels gives, none unless given. Another set of the name takes a function of its own. It is never called again while a call of it runs, and an error or a value that is no finite number writes nothing that time, said once while it repeats.
err = metrics.OpenGaugeFunc(store, "queue_depth", emails.Depth, metrics.Labels{"queue": "emails"})Bucket
type Bucket struct {
// At is where it begins.
At time.Time
// Value is none for a timer's percentile.
Value *float64
Lower *float64
// Upper is +Inf past the last bound.
Upper *float64
Estimate *float64
// Samples is how many samples it comes from; a timer's, how many durations.
Samples int
Resets int
// Partial says its value comes from less than its span: the range, the
// clock or retention cut it.
Partial bool
// Gap says its first step began before the bucket before it: a whole
// bucket between had no samples.
Gap bool
}Bucket is a bucket of an aggregate. One of count … last has its Value; a percentile's is a gauge's sample at the rank, or a timer's bucket of its spread that holds it, from Lower to Upper, and with Estimate asked a point between them.
Condition
type Condition struct {
// contains filtered or unexported fields
}Condition is what a label's value must be beside a value of its own: one of a list, none of one, or a prefix.
Not
func Not(values ...any) ConditionNot is a label equal to none of these, or absent.
OneOf
func OneOf(values ...any) ConditionOneOf is a label equal to one of these.
Prefix
func Prefix(text string) ConditionPrefix is a label that starts with this text, in a query; in a listing, the names, labels or values that do.
Counter
type Counter struct {
// contains filtered or unexported fields
}Counter is a total that only goes up.
OpenCounter
func OpenCounter(store *tinystore.Store, name string, opts ...Option) (*Counter, error)OpenCounter is a total that only goes up, which the store keeps: processes counting into one series count into one total. Another form or other options than the name has while the store is open is Invalid.
Counter.Add
func (c *Counter) Add(n float64)Add adds n, a finite number of 0 or more; Add(0) makes the set of labels known with a total of zero.
Counter.Inc
func (c *Counter) Inc()Inc adds one.
Counter.With
func (c *Counter) With(pairs ...any) *CounterWith is the same counter with more labels, given in pairs, a series of its own; the same labels in any order are one.
Damage
type Damage struct {
Name string
Labels Labels
Error string
}Damage is a series whose bytes no longer read, which Delete removes.
Damaged
func Damaged(ctx context.Context, store *tinystore.Store) ([]Damage, error)Damaged is the series whose bytes no longer read, 1000 at most, each with what broke; deleting them shows the next.
Description
type Description struct {
Unit, Help string
}Description is a name's unit and help; both empty when nothing described it.
DescriptionOf
func DescriptionOf(ctx context.Context, store *tinystore.Store, name string) (Description, error)DescriptionOf is a name's unit and help.
Exceeds
type Exceeds struct {
// Limit is series, bytes read or samples decoded.
Limit string
Wanted, Bound int64
}Exceeds is the bound a call would stop at, as its Limit error names it.
Gauge
type Gauge struct {
// contains filtered or unexported fields
}Gauge is a value that goes up and down.
OpenGauge
func OpenGauge(store *tinystore.Store, name string, opts ...Option) (*Gauge, error)OpenGauge is a value that goes up and down, written while this program is connected: a queue's length, a temperature.
Gauge.Add
func (g *Gauge) Add(n float64)Add moves it by n, from zero when it was never set.
Gauge.Dec
func (g *Gauge) Dec()Gauge.Inc
func (g *Gauge) Inc()Gauge.Set
func (g *Gauge) Set(value float64)Set sets its value, a finite number.
Gauge.With
func (g *Gauge) With(pairs ...any) *GaugeWith is the same gauge with more labels, given in pairs, a series of its own.
Group
type Group struct {
Name string
// Labels is the labels By kept or Without left.
Labels Labels
Buckets []Bucket
}Group is the buckets of a group of series: a bucket without samples is left out.
Aggregate
func Aggregate(ctx context.Context, store *tinystore.Store, q Query) ([]Group, error)Aggregate is the buckets of each group of the series the query finds, each one exact value of its Op, rounded once.
Histogram
type Histogram struct {
// contains filtered or unexported fields
}Histogram is how many numbers of what size: their count, sum and largest, and their spread.
OpenHistogram
func OpenHistogram(store *tinystore.Store, name string, opts ...Option) (*Histogram, error)OpenHistogram is a timer of numbers that are not durations: a response's size in bytes.
Histogram.Record
func (h *Histogram) Record(value float64)Record records a value of 0 or more.
Histogram.With
func (h *Histogram) With(pairs ...any) *HistogramWith is the same histogram with more labels, given in pairs, a series of its own.
Kind
type Kind stringKind is what a series' samples are.
KindGauge
const (
KindGauge Kind = "gauge"
KindCounter Kind = "counter"
// KindHistogram is the _bucket series of a histogram written elsewhere, each with its le.
KindHistogram Kind = "histogram"
)Labels
type Labels map[string]stringLabels are a series' labels, by name.
Name
type Name struct {
Name string
Kind Kind
SeriesCount int
Unit, Help string
Keep time.Duration
// RetainedFrom is its oldest sample, or where its keep begins while it
// holds samples from before, which no read finds.
RetainedFrom time.Time
Newest time.Time
}Name is a name the store holds samples of.
NamePage
type NamePage struct {
Names []Name
Next string
}NamePage is a page of names and where the next starts: "" on the last.
Names
func Names(ctx context.Context, store *tinystore.Store, opts ...PageOption) (NamePage, error)Names is a page of the names the store holds samples of, in the byte order of their text; none of it reads a sample.
Op
type Op stringOp is what an aggregate's bucket answers.
Count
const (
Count Op = "count"
Sum Op = "sum"
Avg Op = "avg"
Min Op = "min"
Max Op = "max"
Increase Op = "increase"
// Rate is a second's increase; a timer's durations a second.
Rate Op = "rate"
Delta Op = "delta"
First Op = "first"
Last Op = "last"
)Percentile
func Percentile(rank float64) OpPercentile is a gauge's sample at this rank, or a timer's bucket of its spread that holds it: Percentile(95), Percentile(99.9).
Option
type Option interface {
// contains filtered or unexported methods
}Option is what an instrument is told.
Buckets
func Buckets(bounds ...float64) OptionBuckets are a timer's or a histogram's upper bounds, ascending; the store adds +Inf, and a percentile of it becomes possible.
Exponential
func Exponential() OptionExponential lets the store keep a timer's spread on Prometheus' native grid, eight intervals a doubling, an experiment: a percentile then lies within one interval, with no bound to choose.
Help
func Help(text string) OptionHelp is a line on what the name means, 1024 bytes at most.
Keep
func Keep(d time.Duration) OptionKeep is how long the name's samples stay, kept in the file with it: 30 days unless a name was given one.
Unit
func Unit(text string) OptionUnit is the unit of its values, 32 bytes at most: "bytes", "%"; a timer's is ms.
PageOption
type PageOption interface {
// contains filtered or unexported methods
}PageOption is how a page of names, labels or values reads: Prefix, After, PageLimit.
After
func After(next string) PageOptionAfter starts a page after the page before's Next.
PageLimit
func PageLimit(n int) PageOptionPageLimit is how many a page holds: 1000 unless given, 10,000 at most.
Plan
type Plan struct {
Series, Bytes, Decoded int64
// Exceeds is nil when the call fits every bound.
Exceeds *Exceeds
}Plan is what a read or an aggregate would spend of each bound.
Explain
func Explain(ctx context.Context, store *tinystore.Store, q Query) (Plan, error)Explain is what Read, or Aggregate when given an Op, would spend of each bound; it decodes no sample.
Point
type Point struct {
Name string
Labels Labels
At time.Time
Value float64
}Point is a series' newest sample in the range.
Latest
func Latest(ctx context.Context, store *tinystore.Store, q Query) ([]Point, error)Latest is the newest sample of each series the query finds, when it has one in the range: a series without one is left out.
Query
type Query struct {
// Name is the series of this name; a timer's name, the series it writes.
Name string
Where Where
// Since is the span before now the range covers; or From and To.
Since time.Duration
// From is the range's start, included, and To its end, excluded: now,
// a sample taken now in the range, when zero.
From, To time.Time
// Every is the buckets' span, counted from 1970 in UTC; the range is one bucket without it.
Every time.Duration
Op Op
// By is the labels a group keeps; without By and Without, every series of a name is one group.
By []string
// Without is the labels a group leaves out, keeping every other.
Without []string
// Estimate gives a timer's percentile a point between its bounds, interpolated.
Estimate bool
}Query is the series a call finds and its range, and for an aggregate how it cuts and groups them. A query needs a Name, or a label's value, a list or a prefix in Where.
Sample
type Sample struct {
At time.Time
Value float64
}Sample is a time, kept to the millisecond, and a number, kept bit for bit.
Series
type Series struct {
Name string
Kind Kind
Labels Labels
Samples []Sample
Times []time.Time
Values []float64
// Keep is how long the name's samples stay, its own, kept in the file;
// what it keeps already unless given.
Keep time.Duration
}Series is a series: what Ingest writes, and what a read gives back. Ingest takes its samples as Samples, or as Times and Values, two columns of one length; a read gives Samples.
Read
func Read(ctx context.Context, store *tinystore.Store, q Query) ([]Series, error)Read is the samples of each series the query finds, in its range: whole, or a Limit error naming its bound, never fewer.
TextPage
type TextPage struct {
Texts []string
Next string
}TextPage is a page of a name's labels or a label's values, and where the next starts.
LabelNames
func LabelNames(ctx context.Context, store *tinystore.Store, name string, opts ...PageOption) (TextPage, error)LabelNames is a page of a name's label names, but the store's own.
LabelValues
func LabelValues(ctx context.Context, store *tinystore.Store, name, label string, opts ...PageOption) (TextPage, error)LabelValues is a page of the values a label of a name's series holds.
Timer
type Timer struct {
// contains filtered or unexported fields
}Timer is how many things took how long.
OpenTimer
func OpenTimer(store *tinystore.Store, name string, opts ...Option) (*Timer, error)OpenTimer is how many things took how long: their count, sum and longest in milliseconds, and their spread with Buckets. A timer named http_request_ms writes http_request_ms_count, _sum, _max and, with buckets, _bucket.
Timer.Record
func (t *Timer) Record(d time.Duration)Record records a duration measured elsewhere.
Timer.Start
func (t *Timer) Start() *TimingStart is a timing that records when it stops, for a span that is no one call: defer latency.Start().Stop().
Timer.With
func (t *Timer) With(pairs ...any) *TimerWith is the same timer with more labels, given in pairs, a series of its own; le is its _bucket series'.
Timing
type Timing struct {
// contains filtered or unexported fields
}Timing is a span a timer measures from its start.
Timing.Stop
func (t *Timing) Stop() time.DurationStop records how long since the timing started, the first time it is called, and gives it.
Where
type Where map[string]anyWhere is what a query finds of each label: a value, a number or a bool as its text, or a Condition.