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

go
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

go
func Describe(ctx context.Context, store *tinystore.Store, name string, description Description) error

Describe 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

go
func ExponentialBuckets(start, factor float64, count int) []float64

ExponentialBuckets is upper bounds from start, each factor times the one before, as Prometheus' ExponentialBuckets: ExponentialBuckets(1, 2, 14) is 1, 2, 4 … 8192.

Flush

go
func Flush(ctx context.Context, store *tinystore.Store) error

Flush 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

go
func Ingest(ctx context.Context, store *tinystore.Store, series ...Series) error

Ingest writes series and their samples in one write, every one or none: a refused series names itself by its labels. It returns once they are written. One call takes 100,000 samples, as one exporter's text does; one past a message goes up in pieces.

IngestPrometheus

go
func IngestPrometheus(ctx context.Context, store *tinystore.Store, text []byte, opts ...Option) error

IngestPrometheus reads the text an exporter serves, in Prometheus' format or OpenMetrics', and writes its series, every one or none: a counter as a counter, a histogram's _bucket series as KindHistogram. It is told Labels that go beside every series' own, At, the time of a sample the text gives none, the store's clock unless given, and Keep, how long every name of the text keeps its samples. A line it cannot read is an Invalid error naming the line.

body, err := io.ReadAll(resp.Body)
err = metrics.IngestPrometheus(ctx, store, body, metrics.Labels{"instance": "web-1"})

OpenGaugeFunc

go
func OpenGaugeFunc(store *tinystore.Store, name string, read func(ctx context.Context) (float64, error), opts ...Option) error

OpenGaugeFunc 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"})

Replace

go
func Replace(ctx context.Context, store *tinystore.Store, series Series, from, to time.Time) error

Replace replaces a gauge's samples from from to to, to excluded, with the series' own, whether the store had packed them or not: a time given takes its value, a time not there before is added, and a time the series does not give goes. It is one write: a read sees the range as it was or as it is now, never half, and the same samples sent again write nothing. A counter's or a histogram's is an Invalid error.

// the last three days read again from the shop's database, a refund taken off yesterday
corrected := metrics.Series{Name: "revenue", Kind: metrics.KindGauge, Labels: metrics.Labels{"shop": "msk"},
	Samples: []metrics.Sample{{At: day(3), Value: 1000}, {At: day(2), Value: 1000}, {At: day(1), Value: 900}}}
err := metrics.Replace(ctx, store, corrected, day(3), day(0))

AggregatePage

go
type AggregatePage struct {
	Groups []Group
	// Next is where the page after it begins; empty on the last.
	Next string
	// contains filtered or unexported fields
}

AggregatePage is one page of an aggregate: its groups, and Next while there is more, walked as a SeriesPage is. A page ends between two whole buckets of a group.

Aggregate

go
func Aggregate(ctx context.Context, store *tinystore.Store, q Query) (AggregatePage, error)

Aggregate is the buckets of each group of the series the query finds, each one exact value of its Op, rounded once: without Limit whole, or a Limit error naming its bound; with Limit a page, which ends between two whole buckets of a group.

AggregatePage.All

go
func (p AggregatePage) All(ctx context.Context) iter.Seq2[Group, error]

All is the page's groups, then those of each page after it, read one page at a time as the walk reaches the end of the one it holds. A group a page cut comes once, whole, its parts joined.

Bucket

go
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
	// Skipped is the NaN samples it passed over, with SkipNaN.
	Skipped 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
	// Date is a calendar's bucket's local date, "2026-11-19": a week's
	// Monday, a month's first; empty for buckets of Every.
	Date string
}

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.

ComparePage

go
type ComparePage struct {
	Groups []ComparedGroup
	// Next is where the page after it begins; empty on the last.
	Next string
	// contains filtered or unexported fields
}

ComparePage is one page of CompareGroups: its groups, and Next while there is more. All walks it and every page after it.

CompareGroupsNew in 0.13.1

go
func CompareGroups(ctx context.Context, store *tinystore.Store, c Comparison) (ComparePage, error)

CompareGroups is what Compare finds, for each group of the series apart, as an aggregate groups them By labels. Without Limit it is every group, or a Limit error naming its bound; with Limit a page of that many groups at most, and Next while there is more. A group past the series one query finds is read in parts, for every Op but a percentile.

ComparePage.All

go
func (p ComparePage) All(ctx context.Context) iter.Seq2[ComparedGroup, error]

All is the page's groups, then those of each page after it, read one page at a time as the walk reaches the end of the one it holds.

Compared

go
type Compared struct {
	// Value is the Op over the newest window; nil when it holds no sample.
	Value *float64
	// Usual is the median over the windows before that hold samples; nil when none does.
	Usual *float64
	// Ratio is Value / Usual; nil when either is nil or Usual is 0.
	Ratio *float64
	// Samples are the newest window's.
	Samples int
	// Windows are those the usual stands on.
	Windows int
}

Compared is what Compare found; a value it could not give is nil, never zero.

Compare

go
func Compare(ctx context.Context, store *tinystore.Store, c Comparison) (Compared, error)

Compare sets the newest window's Op against the median of the windows before it. A window without a sample counts in no median.

ComparedGroup

go
type ComparedGroup struct {
	Name   string
	Labels Labels
	Compared
}

ComparedGroup is what CompareGroups found of one group: its name, the labels By kept, and what Compare finds of a group.

ComparisonNew in 0.13.1

go
type Comparison struct {
	// Name is the series compared, merged into one answer as an aggregate without By merges them.
	Name    string
	Where   Where
	Op      Op
	Window  time.Duration
	Against time.Duration
	// SameHour compares with the same hours of each earlier day alone.
	SameHour bool
	// Estimate makes a timer's percentile compare its estimate; without it such a percentile is refused.
	Estimate bool
	SkipNaN  bool

	// By is the labels a group of CompareGroups keeps; without By and
	// Without, every series of the name is one group. Compare takes none.
	By []string
	// Without is the labels a group of CompareGroups leaves out.
	Without []string
	// Limit makes CompareGroups a page of this many groups at most;
	// without it, every group. Compare takes none.
	Limit int
	// After is the page after the one whose Next this is.
	After string
}

Comparison is an Op over the newest Window, [now − Window, now), against the median of the same Op over the windows before it within Against: the same hours of each earlier day with SameHour, a day 24 hours in no time zone, or every window before otherwise, 1000 at most.

Condition

go
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

go
func Not(values ...any) Condition

Not is a label equal to none of these, or absent.

OneOf

go
func OneOf(values ...any) Condition

OneOf is a label equal to one of these.

Prefix

go
func Prefix(text string) Condition

Prefix is a label that starts with this text, in a query; in a listing, the names, labels or values that do.

Counter

go
type Counter struct {
	// contains filtered or unexported fields
}

Counter is a total that only goes up.

OpenCounter

go
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

go
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

go
func (c *Counter) Inc()

Inc adds one.

Counter.With

go
func (c *Counter) With(pairs ...any) *Counter

With is the same counter with more labels, given in pairs, a series of its own; the same labels in any order are one.

Damage

go
type Damage struct {
	Name   string
	Labels Labels
	Error  string
}

Damage is a series whose bytes no longer read, which Delete removes.

Damaged

go
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

go
type Description struct {
	Unit, Help string
}

Description is a name's unit and help; both empty when nothing described it.

DescriptionOf

go
func DescriptionOf(ctx context.Context, store *tinystore.Store, name string) (Description, error)

DescriptionOf is a name's unit and help.

Exceeds

go
type Exceeds struct {
	// Limit is series, bytes read, samples decoded or answer.
	Limit         string
	Wanted, Bound int64
}

Exceeds is the bound a call would stop at, as its Limit error names it.

Gauge

go
type Gauge struct {
	// contains filtered or unexported fields
}

Gauge is a value that goes up and down.

OpenGauge

go
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

go
func (g *Gauge) Add(n float64)

Add moves it by n, from zero when it was never set.

Gauge.Dec

go
func (g *Gauge) Dec()

Gauge.Inc

go
func (g *Gauge) Inc()

Gauge.Set

go
func (g *Gauge) Set(value float64)

Set sets its value, a finite number.

Gauge.With

go
func (g *Gauge) With(pairs ...any) *Gauge

With is the same gauge with more labels, given in pairs, a series of its own.

Group

go
type Group struct {
	Name string
	// Labels is the labels By kept or Without left.
	Labels  Labels
	Buckets []Bucket
	// More says a page of an aggregate cut the group here: the page its Next
	// reads begins with the rest of its buckets.
	More bool
}

Group is the buckets of a group of series: a bucket without samples is left out, and so is one of NaN alone, unless the query gave Fill.

Histogram

go
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

go
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

go
func (h *Histogram) Record(value float64)

Record records a value of 0 or more.

Histogram.With

go
func (h *Histogram) With(pairs ...any) *Histogram

With is the same histogram with more labels, given in pairs, a series of its own.

Kind

go
type Kind string

Kind is what a series' samples are.

KindGauge

go
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

go
type Labels map[string]string

Labels are a series' labels, by name.

Name

go
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

go
type NamePage struct {
	Names []Name
	Next  string
}

NamePage is a page of names and where the next starts: "" on the last.

Names

go
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

go
type Op string

Op is what an aggregate's bucket answers.

CountNew in 0.13.1

go
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

go
func Percentile(rank float64) Op

Percentile 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

go
type Option interface {
	// contains filtered or unexported methods
}

Option is what an instrument, or IngestPrometheus, is told.

At

go
func At(at time.Time) Option

At is the time of every sample of IngestPrometheus' text that gives none.

Buckets

go
func Buckets(bounds ...float64) Option

Buckets are a timer's or a histogram's upper bounds, ascending; the store adds +Inf, and a percentile of it becomes possible.

Exponential

go
func Exponential() Option

Exponential 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

go
func Help(text string) Option

Help is a line on what the name means, 1024 bytes at most.

Keep

go
func Keep(d time.Duration) Option

Keep is how long the name's samples stay, kept in the file with it: 30 days unless a name was given one.

MaxSeries

go
func MaxSeries(sets int) Option

MaxSeries is the sets of labels an instrument holds at once, 1 to 2000, 2000 unless given: a new set past them counts into its series of __overflow, said once in TinyStore's lines. A timer's set is one, whatever series it writes.

Unit

go
func Unit(text string) Option

Unit is the unit of its values, 32 bytes at most: "bytes", "%"; a timer's is ms.

PageOption

go
type PageOption interface {
	// contains filtered or unexported methods
}

PageOption is how a page of names, labels or values reads: Prefix, After, PageLimit.

After

go
func After(next string) PageOption

After starts a page after the page before's Next.

PageLimit

go
func PageLimit(n int) PageOption

PageLimit is how many a page holds: 1000 unless given, 10,000 at most.

Plan

go
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

go
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

go
type Point struct {
	Name   string
	Labels Labels
	At     time.Time
	Value  float64
}

Point is a series' newest sample in the range.

Latest

go
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

go
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
	// Limit makes a Read or an Aggregate a page of this many samples or
	// buckets at most, 100,000 at most; without it, the whole answer.
	Limit int
	// After is the page after the one whose Next this is, read with the
	// same query and Limit.
	After string

	// Every is the buckets' span, counted from 1970 in UTC; the range is one bucket without it.
	Every time.Duration
	// Calendar is buckets of a calendar in TimeZone, in place of Every:
	// tinystore.Day begins each at a local midnight.
	Calendar tinystore.Calendar
	// TimeZone is the IANA zone a calendar's buckets are in: "Europe/Moscow", "UTC".
	TimeZone string
	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
	// SkipNaN passes a NaN sample over, counted in its bucket's Skipped
	// rather than refusing the aggregate; an infinity is still refused.
	SkipNaN bool
	// Fill answers every bucket of the range retention keeps, an empty one
	// with Samples 0 and Value nil, which is no zero; it takes Since or
	// From, and a page's Limit counts the empty buckets too.
	Fill 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

go
type Sample struct {
	At    time.Time
	Value float64
}

Sample is a time, kept to the millisecond, and a number, kept bit for bit.

Series

go
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
	// More says a page of a read cut the series here: the page its Next
	// reads begins with the rest of it.
	More bool
}

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.

SeriesPage

go
type SeriesPage struct {
	Series []Series
	// Next is where the page after it begins; empty on the last.
	Next string
	// contains filtered or unexported fields
}

SeriesPage is one page of a read: its series, and Next while there is more. All walks it and every page after it. Without Limit it is the whole answer.

Read

go
func Read(ctx context.Context, store *tinystore.Store, q Query) (SeriesPage, error)

Read is the samples of each series the query finds, in its range: without Limit whole, or a Limit error naming its bound, never fewer; with Limit a page, which may hold fewer samples than Limit and still have Next.

SeriesPage.All

go
func (p SeriesPage) All(ctx context.Context) iter.Seq2[Series, error]

All is the page's series, then those of each page after it, read one page at a time as the walk reaches the end of the one it holds.

TextPage

go
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

go
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

go
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

go
type Timer struct {
	// contains filtered or unexported fields
}

Timer is how many things took how long.

OpenTimer

go
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

go
func (t *Timer) Record(d time.Duration)

Record records a duration measured elsewhere.

Timer.Start

go
func (t *Timer) Start() *Timing

Start is a timing that records when it stops, for a span that is no one call: defer latency.Start().Stop().

Timer.With

go
func (t *Timer) With(pairs ...any) *Timer

With is the same timer with more labels, given in pairs, a series of its own; le is its _bucket series'.

Timing

go
type Timing struct {
	// contains filtered or unexported fields
}

Timing is a span a timer measures from its start.

Timing.Stop

go
func (t *Timing) Stop() time.Duration

Stop records how long since the timing started, the first time it is called, and gives it.

Where

go
type Where map[string]any

Where is what a query finds of each label: a value, a number or a bool as its text, or a Condition.

Generated from the source Updated 27 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results