Reference / Metrics API for Rust
Metrics API for Rust
Every public type, trait and function of the Metrics engine in tinystore::metrics and tinystore::aio::metrics, generated from its source. The Metrics guide explains how to use them, and the Bun and Node, Python and Go pages list the same API.
Store
impl StoreStore.metricsNew in 0.13.1
pub fn metrics(&self) -> Result<Metrics>The store's metrics: series written by ingest, read back by query.
The engine opens with the first call.
aio::Store
impl aio::Storeaio::Store.metrics
pub fn metrics(&self) -> Result<Metrics>The store's metrics, as crate::Store::metrics gives them: a plain
call, the first of which opens the engine and migrates metrics.db.
Not
pub struct Not<T>(pub T)A value equal to none of these, or absent: Not("dev"), Not(["dev", "test"]).
Prefix
pub struct Prefix<T>(pub T)A text that starts with this one: Prefix("api-").
Aggregate
pub struct Aggregate {
pub name: String,
pub labels: Labels,
pub buckets: Vec<Bucket>,
pub more: bool,
}A group of an aggregate: the series of one name that share the labels the query kept, and their buckets in the order of their times.
Bucket
pub struct Bucket {
pub at: SystemTime,
pub value: Value,
pub samples: u64,
pub resets: u64,
pub skipped: u64,
pub gap: bool,
pub partial: bool,
pub date: Option<LocalDate>,
}One bucket of an aggregate.
OpNew in 0.13.1
pub enum Op {
Count,
Sum,
Avg,
Min,
Max,
Increase,
Rate,
Delta,
First,
Last,
Percentile(f64),
}What a bucket's value is of its samples.
Value
pub enum Value {
Exact(f64),
Between { lower: f64, upper: f64, estimate: Option<f64> },
Empty,
}A bucket's answer: one exact number, or where a timer's percentile lies.
Value.exact
pub fn exact(self) -> Option<f64>The number, when the value is one.
Buckets
pub enum Buckets {
Bounds(Vec<f64>),
Exponential,
}A timer's or a histogram's buckets.
use tinystore::metrics::{Buckets, exponential_buckets};
let api = stats.timer("http_request_ms").buckets(&[5.0, 10.0, 25.0, 50.0, 100.0]).open()?;
let jobs = stats.timer("job_ms").buckets(exponential_buckets(1.0, 2.0, 14)).open()?;
let db = stats.timer("db_query_ms").buckets(Buckets::Exponential).open()?;exponential_buckets
pub fn exponential_buckets(start: f64, factor: f64, count: usize) -> Vec<f64>count bounds from start, each factor times the one before:
exponential_buckets(1.0, 2.0, 14) is 1, 2, 4 … 8192, bounds a program
gives, as Prometheus' ExponentialBuckets makes them;
Buckets::Exponential lets the store keep a finer grid instead, of
the intervals values fell in.
Compare
pub struct Compare { /* private fields */ }A comparison being asked: the query's series, the op of each window,
its length, and the windows before it it compares with.
Compare.againstNew in 0.13.1
pub fn against(self, last: Duration) -> SelfHow far back the windows it compares with reach.
Compare.same_hour
pub fn same_hour(self) -> SelfCompares with the same hours of each earlier day alone, rather than with every window before the newest.
Compare.run
pub fn run(&self) -> Result<Compared>The newest window's op against the median of the earlier windows'.
Each window is one aggregate of the query's series, merged as an
aggregate without by merges them, all from one snapshot of the
file, every bound of a query on them together. A window without a
sample is left out of the median, and so is a partial one, as an
aggregate marks its bucket: one retention cut short, or a rate's or
an increase's whose step into its first sample began at a sample
retention took or after a NaN. A NaN sample in any window is
invalid, as in an aggregate, unless the query
skip_nans, so no NaN reaches the median.
Each window reads the blocks it reaches on its own: a block that
spans several windows is decoded, and counted against the query's
bounds, once for each, so minute windows over blocks of hours cost
dozens of times one aggregate of the span. same_hour's windows,
a day apart, read a block about once.
A timer's percentile is a number only with the query's
estimate, as aggregate gives one; without it
that op is invalid, since the bounds it lies between are no
number to compare.
More than 1000 earlier windows is invalid, with no pages: a wider
window or a shorter against asks for fewer. Past a bound of a
query it is limit, whose way on for
series is groups with limit, where a group of
any count of series is read in parts.
Compare.groupsNew in 0.13.1
pub fn groups(&self) -> Result<ComparePage>What run finds, for each group of the query's series apart: those
of a name that share the labels by keeps, or every label but those
without leaves, in the order of their names and labels. A group
without a sample in any window is left out. Without limit it is one
page, whole, from one snapshot, and past a bound it is limit. With
limit a page holds that many groups at most, whole, of as many
series as one query finds together, each from one snapshot, and gives
next while there is more; it stops before a group that would pass a
bound, and a group that alone passes one is limit. A group of more
series than one query finds is alone on its page and read in parts,
each from a snapshot of its own, for every op but a percentile,
which ranks every series of its group at once: such a group is
limit, and the groups before it are not.
Compared
pub struct Compared {
pub value: Option<f64>,
pub usual: Option<f64>,
pub ratio: Option<f64>,
pub samples: u64,
pub windows: u32,
}What compare found: the newest window's value, the usual one, and what
each stands on. A value it could not give is none, never zero.
ComparedGroup
pub struct ComparedGroup {
pub name: String,
pub labels: Labels,
pub value: Option<f64>,
pub usual: Option<f64>,
pub ratio: Option<f64>,
pub samples: u64,
pub windows: u32,
}What groups found of one group: its name and the labels the query
kept, and what run finds of a group, each field as Compared's.
Damage
pub struct Damage {
pub name: String,
pub labels: Labels,
pub error: String,
}A series whose bytes no longer read, as damaged lists it and delete
takes it.
Description
pub struct Description {
pub unit: String,
pub help: String,
}A name's unit and help, empty when nothing gave them.
ExactSum
pub struct ExactSum { /* private fields */ }A sum of floats kept exactly and rounded once, as an aggregate of the store sums its samples: for a program that sums what it never writes and wants the number a query of the same samples gives.
use tinystore::metrics::ExactSum;
let tenths: ExactSum = [0.1; 10].into_iter().collect();
assert_eq!(tenths.value(), 1.0, "where a float sum gives 0.9999999999999999");
let mut sum = ExactSum::default();
[1e308, 1e308, -1e308].into_iter().for_each(|value| sum.add(value));
assert_eq!(sum.value(), 1e308, "and no infinity on the way");An infinity or a NaN added makes the sum what a float sum makes it, since it has no exact value; past what a float holds the value is an infinity.
ExactSum.add
pub fn add(&mut self, value: f64)ExactSum.sub
pub fn sub(&mut self, value: f64)Takes value away, exactly: a sum less what one series gave it.
ExactSum.merge
pub fn merge(&mut self, other: &ExactSum)Adds another sum, exactly: a group's sum of its series' sums.
ExactSum.value
pub fn value(&self) -> f64The float nearest the sum, a tie to the even one.
Exceeds
pub struct Exceeds {
pub limit: &'static str,
pub wanted: u64,
pub bound: u64,
}A bound a call would stop at, as its limit error would name it.
Plan
pub struct Plan {
pub series: u64,
pub bytes: u64,
pub decoded: u64,
pub exceeds: Option<Exceeds>,
}What a call would spend of a query's bounds.
Exposition
pub struct Exposition { /* private fields */ }What writes the newest sample of every series the store holds, its
options before its write.
Exposition.only_prefix
pub fn only_prefix(self, prefix: impl Into<String>) -> SelfOnly the names that start with this text.
Exposition.since
pub fn since(self, span: Duration) -> SelfOnly the series whose newest sample is at most this old; without it every series the store still keeps, however long it has been silent.
Exposition.timestamps
pub fn timestamps(self, on: bool) -> SelfWhether each line carries its sample's time, as it does unless this turns them off: node_exporter's textfile collector and the Pushgateway refuse a sample with a time of its own, and a Prometheus drops a silent series' old one as out of bounds.
Exposition.write
pub fn write(&self, out: &mut impl Write) -> Result<Written>Writes the text to out, a part of a name's series at a time, and says
how many names and series it wrote. Every series is written or the
call fails, out then holding the lines before the failure.
Written
pub struct Written {
pub names: u64,
pub series: u64,
pub skipped: u64,
}What an exposition wrote: the store's names with a series in the text, and their series; and the series it could not spell apart from another's, which it left out.
SeriesQuery
pub struct SeriesQuery<'a> { /* private fields */ }An aggregate of series a program holds, its range and its buckets as a
store's Query says them. Without from and to the
range is the samples', from the first to the last.
SeriesQuery.from
pub fn from(self, from: SystemTime) -> SelfThe range from this time on; a sample before it is where a step into the first bucket begins, as in a store's query.
SeriesQuery.to
pub fn to(self, to: SystemTime) -> SelfThe range until this time, not at it.
SeriesQuery.every
pub fn every(self, span: Duration) -> SelfBuckets of this span, counted from 1970 in UTC; without them the range is one bucket.
SeriesQuery.calendar
pub fn calendar(self, unit: Calendar, zone: &str) -> SelfBuckets of a calendar's days, weeks or months in the time zone of its IANA name, as long as each is there.
SeriesQuery.estimate
pub fn estimate(self) -> SelfA timer's or a histogram's percentile gives a point between its
bounds too, as Prometheus' histogram_quantile interpolates one.
SeriesQuery.skip_nan
pub fn skip_nan(self) -> SelfA NaN sample is passed over, as a store's query passes it with
Query::skip_nan: each bucket's skipped
counts them.
SeriesQuery.aggregate
pub fn aggregate(&self, op: Op) -> Result<Aggregate>The group's buckets, each one exact value of op, rounded once, with
samples, resets, gap and partial as a query gives them; a
bucket without a sample is left out. An op the series' kind has
not is invalid, and so is a bucket over a NaN, unless skip_nan
passes it over, an infinity or a counter below zero, or two samples
of one series at one millisecond.
query
pub fn query(series: &[Series]) -> SeriesQuery<'_>An aggregate of the series a program holds, one group, as a query of the
store answers one for the same samples: series share a name and a kind.
Builder
pub struct Builder<T> { /* private fields */ }An instrument being opened: its name and what it is told, which open
checks against what the store and its file hold.
Builder.help
pub fn help(self, help: impl Into<String>) -> SelfA line on what it means, 1024 bytes at most.
Builder.keep
pub fn keep(self, keep: Duration) -> SelfHow long its name's samples stay, as ingest takes it.
Builder.unit
pub fn unit(self, unit: impl Into<String>) -> SelfThe unit of its values, 32 bytes at most: bytes, %.
Builder.unit
pub fn unit(self, unit: impl Into<String>) -> SelfThe unit of its values, 32 bytes at most: bytes, %.
Builder.unit
pub fn unit(self, unit: impl Into<String>) -> SelfThe unit of its values, 32 bytes at most: bytes, %.
Builder.unit
pub fn unit(self, unit: impl Into<String>) -> SelfThe unit of its values, 32 bytes at most: bytes, %.
Builder.buckets
pub fn buckets(self, buckets: impl Into<Buckets>) -> SelfIts buckets: the upper bounds of its spread, ascending, each a
number of 0 or more, to which the store adds +Inf, or
Buckets::Exponential, a grid the store keeps.
Builder.buckets
pub fn buckets(self, buckets: impl Into<Buckets>) -> SelfIts buckets: the upper bounds of its spread, ascending, each a
number of 0 or more, to which the store adds +Inf, or
Buckets::Exponential, a grid the store keeps.
Builder.max_series
pub fn max_series(self, sets: usize) -> SelfThe sets of labels it holds at once, 1 to 2000, 2000 unless
given: a new set past them counts into its series of
__overflow, said once, so that a label that takes a value a
user stays bounded and its groups still add up. A timer's set
is one, whatever series it writes.
Builder.max_series
pub fn max_series(self, sets: usize) -> SelfThe sets of labels it holds at once, 1 to 2000, 2000 unless
given: a new set past them counts into its series of
__overflow, said once, so that a label that takes a value a
user stays bounded and its groups still add up. A timer's set
is one, whatever series it writes.
Builder.max_series
pub fn max_series(self, sets: usize) -> SelfThe sets of labels it holds at once, 1 to 2000, 2000 unless
given: a new set past them counts into its series of
__overflow, said once, so that a label that takes a value a
user stays bounded and its groups still add up. A timer's set
is one, whatever series it writes.
Builder.max_series
pub fn max_series(self, sets: usize) -> SelfThe sets of labels it holds at once, 1 to 2000, 2000 unless
given: a new set past them counts into its series of
__overflow, said once, so that a label that takes a value a
user stays bounded and its groups still add up. A timer's set
is one, whatever series it writes.
Builder.open
pub fn open(self) -> Result<Counter>Builder.open
pub fn open(self) -> Result<Gauge>Builder.labels
pub fn labels(self, labels: &[(&str, &str)]) -> SelfThe set of labels its function reads; another set of the name takes a function of its own, and a set is read by one.
Builder.open
pub fn open(self) -> Result<()>Opens the gauge; its function is read from the next write on, while the store is open.
Builder.open
pub fn open(self) -> Result<Timer>Builder.open
pub fn open(self) -> Result<Histogram>Counter
pub struct Counter { /* private fields */ }A total that only goes up, which the store keeps.
Counter.with
pub fn with(&self, labels: &[(&str, &str)]) -> CounterThe same counter with more labels, a series of its own; a label given again takes its new value, and the same labels in any order are one series.
Counter.inc
pub fn inc(&self)Counter.add
pub fn add(&self, n: f64)Adds n, a finite number of 0 or more; add(0.0) makes the set of
labels known with a total of zero.
Gauge
pub struct Gauge { /* private fields */ }A value that goes up and down, written while the process that sets it is connected.
Gauge.with
pub fn with(&self, labels: &[(&str, &str)]) -> GaugeThe same gauge with more labels, a series of its own.
Gauge.set
pub fn set(&self, value: f64)Sets its value, a finite number.
Gauge.add
pub fn add(&self, delta: f64)Moves its value by delta, from zero when it was never set.
Gauge.inc
pub fn inc(&self)Gauge.dec
pub fn dec(&self)GaugeFunc
pub struct GaugeFunc { /* private fields */ }What gauge_func opens: a gauge its function reads at each write.
Histogram
pub struct Histogram { /* private fields */ }A timer of numbers that are not durations, each 0 or more.
Histogram.with
pub fn with(&self, labels: &[(&str, &str)]) -> HistogramThe same histogram with more labels, a series of its own; le is
its buckets'.
Histogram.record
pub fn record(&self, value: f64)Records a value, a finite number of 0 or more.
Reading
pub trait ReadingWhat a gauge's function gives: its value, or its value or why it has none.
Reading.value
fn value(self) -> Result<f64, String>Timer
pub struct Timer { /* private fields */ }How many things took how long: their count, their sum and the longest since the write before, in milliseconds, and their spread over its buckets.
Timer.with
pub fn with(&self, labels: &[(&str, &str)]) -> TimerThe same timer with more labels, a series of its own; le is its
buckets'.
Timer.record
pub fn record(&self, duration: Duration)Records a duration measured elsewhere.
Timer.start
pub fn start(&self) -> TimingA timing that records how long it lived when it drops, for a span
that is no one function: kept in a name, let _timing = latency.start(), since let _ = drops it at once.
Timer.measure
pub fn measure<T>(&self, work: impl FnOnce() -> T) -> TRuns work and records how long it took, a panic's unwinding too.
Timing
pub struct Timing { /* private fields */ }A duration being measured, recorded when it drops.
Latest
pub struct Latest {
pub name: String,
pub labels: Labels,
pub at: SystemTime,
pub value: f64,
}A series' newest sample.
LabelPage
pub struct LabelPage {
pub labels: Vec<String>,
pub next: Option<String>,
}A page of a name's label names.
List
pub struct List<P> { /* private fields */ }A page of what a store holds, its options before its page: 1000 unless
limit says.
List.prefix
pub fn prefix(self, prefix: impl Into<String>) -> SelfOnly those that start with this text.
List.after
pub fn after(self, next: impl Into<String>) -> SelfThe page that starts past this one, as a page's next says it.
List.limit
pub fn limit(self, most: usize) -> SelfHow many a page holds at most, 1 to 10,000.
List.page
pub fn page(self) -> Result<NamePage>List.page
pub fn page(self) -> Result<LabelPage>List.page
pub fn page(self) -> Result<ValuePage>NameInfo
pub struct NameInfo {
pub name: String,
pub kind: Kind,
pub series_count: u64,
pub unit: String,
pub help: String,
pub keep: Duration,
pub retained_from: SystemTime,
pub newest: SystemTime,
}A name the store holds series of, as names gives it.
NamePage
pub struct NamePage {
pub names: Vec<NameInfo>,
pub next: Option<String>,
}A page of the names a store holds.
ValuePage
pub struct ValuePage {
pub values: Vec<String>,
pub next: Option<String>,
}A page of the values a label of a name holds.
Maintenance
pub struct Maintenance {
pub expired: u64,
pub reclaimed: u64,
pub packed: u64,
pub blocks: u64,
pub suspended: u64,
}What maintenance did.
maintain
pub fn maintain(store: &Store) -> Result<Maintenance>Runs the store's metrics maintenance until nothing is left to do, as a store opened without background work has to.
SelfMetrics
pub struct SelfMetrics {
pub system: bool,
}What a store measures of itself, when it is opened with its own metrics.
AggregatePage
pub struct AggregatePage {
pub groups: Vec<Aggregate>,
pub next: Option<String>,
}One page of an aggregate: its groups, and next while there is more. A
group the page cut says Aggregate::more, and the page next reads
begins with the rest of its buckets. An aggregate without limit is one
page, whole, without next.
AggregatePage.all
pub fn all(self) -> All<Aggregate>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: the walk holds its buckets until the page that ends it.
All
pub struct All<T> { /* private fields */ }The series or the groups of a page and of every page after it, one page held at a time.
ComparePage
pub struct ComparePage {
pub groups: Vec<ComparedGroup>,
pub next: Option<String>,
}One page of a comparison's groups: each group's comparison, and next
while there is more. Without limit it is one page, whole, without
next.
ComparePage.allNew in 0.13.1
pub fn all(self) -> All<ComparedGroup>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.
SeriesPage
pub struct SeriesPage {
pub series: Vec<Series>,
pub next: Option<String>,
}One page of a read: its series, and next while there is more. A series
the page cut says Series::more, and the page next reads begins with
the rest of it. A read without limit is one page, whole, without next.
SeriesPage.all
pub fn all(self) -> All<Series>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. A series a page cut comes in its parts, since a series is what pages bound.
Prometheus
pub struct Prometheus { /* private fields */ }A text an exporter serves, in Prometheus' format or OpenMetrics', and the
labels, the time and the keep its ingest gives every series of it. It is
kept for each scrape of one target, ingest taking each scrape's text.
let stats = store.metrics()?;
let web = stats.prometheus().label("instance", "web-1");
web.ingest(&body)?;Prometheus.label
pub fn label(self, name: impl Into<String>, value: impl Display) -> PrometheusA label every series of the text gets beside its own, such as the
instance scraped; a text that gives it too is invalid, naming the
line.
Prometheus.at
pub fn at(self, at: SystemTime) -> PrometheusThe time of every sample the text gives none: the store's clock at
ingest unless given.
Prometheus.keep
pub fn keep(self, keep: Duration) -> PrometheusHow long every name of the text keeps its samples, as a series' own
keep.
Prometheus.ingest
pub fn ingest(&self, text: impl AsRef<[u8]>) -> Result<()>Reads the text and writes its series, every one or none, as ingest
does: a line it cannot read is invalid, naming the line, and a text
past 100,000 samples or 32 MiB is a limit.
Condition
pub enum Condition {
OneOf(Vec<String>),
NoneOf(Vec<String>),
Prefix(String),
}What a label's value must be for a query to take a series.
Query
pub struct Query { /* private fields */ }A query being asked: its name, conditions and range, and for an aggregate its buckets and groups.
Query.nameNew in 0.13.1
pub fn name(self, name: impl Into<String>) -> SelfThe series of this name.
Query.filter
pub fn filter(self, label: impl Into<String>, condition: impl Into<Condition>) -> SelfA label's value under a condition: a value, a list of them, a
Not or a Prefix.
Query.since
pub fn since(self, span: Duration) -> SelfThe span before now the range covers; or from and to.
Query.from
pub fn from(self, at: SystemTime) -> SelfThe range's start, which is in it.
Query.to
pub fn to(self, at: SystemTime) -> SelfThe range's end, which is not in it; now unless given, a sample taken now in the range.
Query.every
pub fn every(self, span: Duration) -> SelfAn aggregate's buckets: spans this long, each beginning at a whole number of them since 1970 in UTC, the range cutting the first and the last. Without it the range is one bucket.
Query.calendar
pub fn calendar(self, unit: Calendar, zone: &str) -> SelfAn aggregate's buckets on the calendar of a time zone, its IANA name:
calendar(Calendar::Day, "Europe/Moscow") begins each at a local
midnight, a day 23 or 25 hours long when the clock moves. A bucket's
at is the instant it begins, and a rate counts its own seconds.
Query.byNew in 0.13.1
pub fn by(self, labels: &[&str]) -> SelfThe labels an aggregate keeps: the series of a name that share them
are one group. Without by and without, every series of a name is
one group.
Query.without
pub fn without(self, labels: &[&str]) -> SelfThe labels an aggregate leaves out, keeping every other: &[] keeps
every series apart.
Query.estimate
pub fn estimate(self) -> SelfA timer's percentile also gives a point between the bounds of the
bucket of its spread that holds the rank, interpolated as Prometheus'
histogram_quantile does: an estimate, which no measurement gave.
Query.skip_nan
pub fn skip_nan(self) -> SelfAn aggregate passes a NaN sample over rather than refuse it: the
sample counts in no value, and its bucket's skipped says how many
it passed over. A bucket of NaN alone is left out, as one without
samples is. An infinity is still refused, since it is a value.
Query.fill
pub fn fill(self) -> SelfAn aggregate gives every bucket of the range that retention keeps, a
bucket without samples Value::Empty, with
no samples and partial as its span gives it: a chart draws every
hour of a day, an empty one as it chooses. A group without a sample
is still left out, since nothing names it. It takes since or
from. Without limit, past 100,000 buckets a group it is limit,
before anything is read; with limit each page fills the stretch of
each group it holds, and its limit counts the empty buckets too, so
that the pages give the buckets one call gives.
use tinystore::metrics::Op;
let day = stats.query().name("errors_total").since(Duration::from_hours(24)).every(Duration::from_hours(1));
let hours = day.fill().aggregate(Op::Increase)?.groups;
let drawn: Vec<f64> = hours[0].buckets.iter().map(|hour| hour.value.exact().unwrap_or(0.0)).collect();Query.limit
pub fn limit(self, samples: usize) -> SelfA read in pages: each at most this many samples, 100,000 at most, and
its next while there is more.
Query.after
pub fn after(self, next: &str) -> SelfThe page after the one whose next this is, read with the same query
and limit: the range, and the clock, the first page read.
Query.read
pub fn read(&self) -> Result<SeriesPage>The samples of each series the query finds, in its range: a series
without one is left out. Without limit the page is the whole
answer, and past a bound of a read it is a limit error, naming the
bound, never fewer series or samples. With limit it is a page,
which stops before what would pass limit or a bound and gives
next; it may hold fewer samples than limit and still have one.
Query.aggregate
pub fn aggregate(&self, op: Op) -> Result<AggregatePage>The buckets of each group of the series the query finds, each one
exact value of op, rounded once: a group without a sample in the
range is left out, and so is a bucket without one. An op a series'
kind has not is invalid, and so is a bucket over a NaN, unless the
query passes NaN over with skip_nan, an infinity or a counter below
zero. Without limit the page is the whole answer, and past a bound
it is a limit error, naming the bound, never fewer buckets. With limit it is a page of that many buckets
with samples at most, a stretch without any costing it nothing, which
ends between two whole buckets of a group and gives next; one
bucket that alone passes a bound is still limit. A page reads only
the series of its own groups, so pages go past the series a query
finds a group at a time, and a group whose series alone pass them is
limit too.
Query.compareNew in 0.13.1
pub fn compare(&self, op: Op, window: Duration) -> CompareThe op over the newest window against the median of the windows
before it, which Compare::against reaches back over: the query's
name and conditions, and its estimate and skip_nan, as an
aggregate takes them; no range, buckets or fill. Its by,
without, limit and after are Compare::groups's.
Query.latest
pub fn latest(&self) -> Result<Vec<Latest>>The newest sample of each series the query finds, when it has one in the range: a series that stopped writing leaves the answer rather than show its last value for ever.
Query.explain
pub fn explain(&self, op: impl Into<Option<Op>>) -> Result<Plan>What read, or aggregate when given its op, would spend of each
bound of a query, and the bound it would stop at. It decodes no
sample.
use tinystore::metrics::Op;
let month = stats.query().name("cpu").since(Duration::from_hours(30 * 24));
let plan = month.clone().every(Duration::from_hours(1)).explain(Op::Avg)?;
assert!(plan.exceeds.is_none(), "whole hours answer from their summaries");
let read = month.explain(None)?;Values
pub trait ValuesThe values a condition lists: text, or a number or a boolean as its text, as a label keeps them.
Values.values
fn values(self) -> Vec<String>Recorder
pub struct Recorder { /* private fields */ }The metrics crate's recorder for a store's metrics.
Recorder.buckets
pub fn buckets(self, name: impl Into<String>, buckets: impl Into<Buckets>) -> RecorderThe buckets of the histogram of name, as a timer's are.
Kind
pub enum Kind {
Gauge,
Counter,
Histogram,
}What a series' samples are.
Kind.as_str
pub fn as_str(self) -> &'static strThe kind as the file, the wire and every SDK say it.
Labels
pub type Labels = BTreeMap<String, String>The labels of a series beside its name, route → /notes: text, a number
given as its text.
Sample
pub struct Sample {
pub at: SystemTime,
pub value: f64,
}A time and a number, kept bit for bit: -0, infinities and a NaN's
payload come back as they went in. A time is kept to the millisecond.
Series
pub struct Series { /* private fields */ }A series: what ingest writes, built a label and a sample at a time, and
what a read gives back.
use tinystore::metrics::Series;
let at = UNIX_EPOCH + Duration::from_secs(1_800_000_000);
let cpu = Series::gauge("cpu").label("host", "web-1").sample(at, 0.42);
assert_eq!(cpu.to_string(), r#"cpu{host="web-1"}"#);Series.query
pub fn query(&self) -> SeriesQuery<'_>An aggregate of this series alone, as a query of the store answers one for the same samples, and with no store.
Series.gauge
pub fn gauge(name: impl Into<String>) -> SeriesSeries.counter
pub fn counter(name: impl Into<String>) -> SeriesSeries.histogram
pub fn histogram(name: impl Into<String>) -> SeriesThe _bucket series of a histogram written elsewhere, one series per
bound, each with its label le, beside its _sum and _count
counters.
Series.label
pub fn label(self, name: impl Into<String>, value: impl Display) -> SeriesOne label more; a number or a boolean is kept as its text, 200 as
"200".
Series.sample
pub fn sample(self, at: SystemTime, value: f64) -> SeriesSeries.keep
pub fn keep(self, keep: Duration) -> SeriesHow long the name's samples stay: the name's own, kept in the file.
Given none, the name keeps what it has, 30 days until one is given;
another than one given before while the store is open is invalid.
Series.name
pub fn name(&self) -> &strSeries.kind
pub fn kind(&self) -> KindSeries.labels
pub fn labels(&self) -> &LabelsSeries.samples
pub fn samples(&self) -> &[Sample]Series.more
pub fn more(&self) -> boolA page of a read cut the series here: the page its next reads
begins with the rest of its samples.
Metrics
pub struct Metrics { /* private fields */ }The store's metrics, a handle that clones cheaply.
Metrics.damaged
pub fn damaged(&self) -> Result<Vec<Damage>>The series whose bytes no longer read, which a read, packing or publication met: 1000 at most, the first the file holds, each with what broke. Deleting them shows the next.
Metrics.delete
pub fn delete<K: AsRef<str>, V: AsRef<str>>(
&self,
name: &str,
labels: impl IntoIterator<Item = (K, V)>,
) -> Result<bool>Removes the one series of exactly this name and these labels, no more
and no fewer, with every sample it holds, whether they still read or
not, and says whether the store held it: delete("cpu", [("host", "web-1")]). Writing the same labels later starts a new series; the
name keeps its keep, unit and help.
Metrics.describe
pub fn describe(&self, name: &str, description: Description) -> Result<()>Sets a name's unit and help in place of what it had; empty ones
remove them, and Description::default() removes both.
Metrics.description
pub fn description(&self, name: &str) -> Result<Description>What a name's unit and help are; both empty when nothing described it.
Metrics.exposition
pub fn exposition(&self) -> ExpositionThe newest sample of every series the store holds, as Prometheus'
text format: stats.exposition().write(&mut out).
Metrics.counter
pub fn counter(&self, name: impl Into<String>) -> Builder<Counter>A total that only goes up, which the store keeps: processes counting into one series count into one total.
Metrics.gauge
pub fn gauge(&self, name: impl Into<String>) -> Builder<Gauge>A value that goes up and down: a queue's length, a temperature.
Metrics.gauge_func
pub fn gauge_func<R: Reading>(
&self,
name: impl Into<String>,
read: impl Fn() -> R + Send + Sync + 'static,
) -> Builder<GaugeFunc>A gauge whose value read gives at each write. It runs on the
thread that writes, the store's own, and must not block: a value
that takes time to read is a gauge the program sets.
Metrics.timer
pub fn timer(&self, name: impl Into<String>) -> Builder<Timer>How many things took how long: their count, their sum and the
longest, in milliseconds, and with buckets their spread.
Metrics.histogram
pub fn histogram(&self, name: impl Into<String>) -> Builder<Histogram>A timer of numbers that are not durations: a response's size.
Metrics.flush
pub fn flush(&self) -> Result<()>Writes every instrument's total now, as the store does every 15 seconds and when it closes.
Metrics.names
pub fn names(&self) -> List<NamePage>The names the store holds samples of, each with its kind, its count of series, its unit and help, what it keeps and where that begins, and its newest sample's time.
Metrics.label_names
pub fn label_names(&self, name: impl Into<String>) -> List<LabelPage>A name's label names, but the store's own.
Metrics.label_values
pub fn label_values(&self, name: impl Into<String>, label: impl Into<String>) -> List<ValuePage>The values a label of a name's series holds.
Metrics.prometheus
pub fn prometheus(&self) -> PrometheusWhat ingests the text an exporter serves, in Prometheus' format or
OpenMetrics': stats.prometheus().label("instance", "web-1").ingest(&body).
Metrics.recorder
pub fn recorder(&self) -> RecorderA recorder for the metrics crate, whose macros then count into
these metrics.
Metrics.replace
pub fn replace(&self, series: &Series, range: Range<SystemTime>) -> Result<()>Replaces a gauge's samples of range, [from, to), 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 of the range 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 an aggregate over it answers from
the samples given. Sent again with the same samples, it writes nothing.
use tinystore::metrics::Series;
let store = tinystore::Store::open("data", Default::default())?;
let stats = store.metrics()?;
let (now, day) = (SystemTime::now(), Duration::from_hours(24));
// the last three days read again from the shop's database, a refund taken off yesterday
let revenue = Series::gauge("revenue").label("shop", "msk");
let corrected = revenue.sample(now - 3 * day, 1000.0).sample(now - 2 * day, 1000.0).sample(now - day, 900.0);
stats.replace(&corrected, now - 3 * day..now)?;Only a gauge's: a counter's or a histogram's is invalid, since its
increase is added across what the store packed. A sample outside the
range is invalid, a range that begins before what the name keeps
too old, and one that ends more than ten minutes ahead of the store's
clock too new. It takes as many samples as one ingest, and what the
range holds is bounded as a query is: past either is limit.
Metrics.ingest
pub fn ingest(&self, series: &[Series]) -> Result<()>Writes series and their samples, every one or none: a refused series names itself by its labels, so the call can be sent again without it. A time given twice keeps the value given last, until the store has packed that stretch of its series; it returns once the samples are written, and a read finds them at once.
Metrics.queryNew in 0.13.1
pub fn query(&self) -> QueryA query of series by name and labels, over a range of time, which
read, aggregate, latest or explain then runs.
aio::metrics::AggregatePage
pub struct AggregatePage {
pub groups: Vec<Aggregate>,
pub next: Option<String>,
}One page of an awaited aggregate: its groups, and next while there is
more.
aio::metrics::AggregatePage.all
pub fn all(self) -> All<Aggregate>The page's groups, then those of each page after it, as a stream: a group a page cut comes once, whole, its parts joined.
aio::metrics::All
pub struct All<T> { /* private fields */ }The series or the groups of a page and of every page after it, a
Stream holding one page at a time. Dropped while a page is read, that
read stops as a dropped read does.
Stream: futures_core::Stream
aio::metrics::ComparePage
pub struct ComparePage {
pub groups: Vec<ComparedGroup>,
pub next: Option<String>,
}One page of an awaited comparison's groups: each group's comparison,
and next while there is more.
aio::metrics::ComparePage.all
pub fn all(self) -> All<ComparedGroup>The page's groups, then those of each page after it, as a stream.
aio::metrics::SeriesPage
pub struct SeriesPage {
pub series: Vec<Series>,
pub next: Option<String>,
}One page of an awaited read: its series, and next while there is more.
aio::metrics::SeriesPage.all
pub fn all(self) -> All<Series>The page's series, then those of each page after it, as a stream.
aio::metrics::Metrics
pub struct Metrics { /* private fields */ }The store's metrics, their calls awaited: the same engine and series as
crate::metrics::Metrics, a handle that clones cheaply.
aio::metrics::Metrics.blocking
pub fn blocking(&self) -> &MetricsThe same metrics, for their blocking calls.
aio::metrics::Metrics.ingest
pub fn ingest(&self, series: &[Series]) -> impl Future<Output = Result<()>> + Send + 'staticWrites series and their samples, every one or none, as
crate::metrics::Metrics::ingest does. The series are copied when
the call is made, and checked on a thread of the store's pool; the
store's memory their write holds is awaited on no thread, and the
write runs blocking on a thread of the pool, which waits there for
the commit.
aio::metrics::Metrics.replace
pub fn replace(
&self,
series: &Series,
range: Range<SystemTime>,
) -> impl Future<Output = Result<()>> + Send + 'staticReplaces a gauge's samples of range with the series' own, as
crate::metrics::Metrics::replace does. What the range holds is read
on a thread of the store's pool and the memory its write holds awaited
on no thread; the write then runs blocking on a thread of the pool,
which waits there for maintenance's pass to end and for the commit.
aio::metrics::Metrics.query
pub fn query(&self) -> QueryA query of series by name and labels, over a range of time, which
read, aggregate, latest or explain then runs.
aio::metrics::Metrics.prometheus
pub fn prometheus(&self) -> PrometheusWhat ingests the text an exporter serves, in Prometheus' format or
OpenMetrics': stats.prometheus().label("instance", "web-1").ingest(&body).
aio::metrics::Metrics.exposition
pub fn exposition(&self) -> ExpositionThe newest sample of every series the store holds, as Prometheus'
text format: stats.exposition().write(Vec::new()).await.
aio::metrics::Metrics.names
pub fn names(&self) -> List<NamePage>The names the store holds samples of, as
crate::metrics::Metrics::names lists them.
aio::metrics::Metrics.label_names
pub fn label_names(&self, name: impl Into<String>) -> List<LabelPage>A name's label names, but the store's own.
aio::metrics::Metrics.label_values
pub fn label_values(&self, name: impl Into<String>, label: impl Into<String>) -> List<ValuePage>The values a label of a name's series holds.
aio::metrics::Metrics.describe
pub fn describe(
&self,
name: &str,
description: Description,
) -> impl Future<Output = Result<()>> + Send + 'staticSets a name's unit and help in place of what it had, as
crate::metrics::Metrics::describe does, blocking on a thread of
the store's pool, which waits there for the commit.
aio::metrics::Metrics.description
pub fn description(&self, name: &str) -> impl Future<Output = Result<Description>> + Send + 'staticWhat a name's unit and help are; both empty when nothing described it.
aio::metrics::Metrics.damaged
pub fn damaged(&self) -> impl Future<Output = Result<Vec<Damage>>> + Send + 'staticThe series whose bytes no longer read, as
crate::metrics::Metrics::damaged lists them.
aio::metrics::Metrics.delete
pub fn delete<K: AsRef<str>, V: AsRef<str>>(
&self,
name: &str,
labels: impl IntoIterator<Item = (K, V)>,
) -> impl Future<Output = Result<bool>> + Send + 'staticRemoves the one series of exactly this name and these labels with
every sample it holds, as crate::metrics::Metrics::delete does,
and says whether the store held it. It runs blocking on a thread of
the store's pool, which waits there for maintenance's pass to end.
aio::metrics::Metrics.flush
pub fn flush(&self) -> impl Future<Output = Result<()>> + Send + 'staticWrites every instrument's total now, as the store does every 15 seconds and when it closes, blocking on a thread of the store's pool: a gauge function reads its value there.
aio::metrics::Metrics.counter
pub fn counter(&self, name: impl Into<String>) -> Builder<Counter>A total that only goes up, which the store keeps, as
crate::metrics::Metrics::counter opens one: an instrument counts
in memory, so its calls are plain.
aio::metrics::Metrics.gauge
pub fn gauge(&self, name: impl Into<String>) -> Builder<Gauge>A value that goes up and down: a queue's length, a temperature.
aio::metrics::Metrics.gauge_func
pub fn gauge_func<R: Reading>(
&self,
name: impl Into<String>,
read: impl Fn() -> R + Send + Sync + 'static,
) -> Builder<GaugeFunc>A gauge whose value read gives at each write, on the thread that
writes, the store's own: it must not block.
aio::metrics::Metrics.timer
pub fn timer(&self, name: impl Into<String>) -> Builder<Timer>How many things took how long: their count, their sum and the
longest, in milliseconds, and with buckets their spread.
aio::metrics::Metrics.histogram
pub fn histogram(&self, name: impl Into<String>) -> Builder<Histogram>A timer of numbers that are not durations: a response's size.
aio::metrics::Metrics.recorder
pub fn recorder(&self) -> RecorderA recorder for the metrics crate, whose macros then count into
these metrics.
aio::metrics::Query
pub struct Query { /* private fields */ }A query being asked, as crate::metrics::Query builds one: its name,
conditions and range, and for an aggregate its buckets and groups.
A call checks the query in its future's first poll, on the polling
thread: since becomes an absolute start from the store's clock there,
and a query that is invalid fails without taking a thread. The read then
runs on the store's pool under a token that the future's drop cancels.
aio::metrics::Query.name
pub fn name(self, name: impl Into<String>) -> SelfThe series of this name.
aio::metrics::Query.filter
pub fn filter(self, label: impl Into<String>, condition: impl Into<Condition>) -> SelfA label's value under a condition: a value, a list of them, a
Not or a Prefix.
aio::metrics::Query.since
pub fn since(self, span: Duration) -> SelfThe span before now the range covers; or from and to.
aio::metrics::Query.from
pub fn from(self, at: SystemTime) -> SelfThe range's start, which is in it.
aio::metrics::Query.to
pub fn to(self, at: SystemTime) -> SelfThe range's end, which is not in it; now unless given.
aio::metrics::Query.limit
pub fn limit(self, samples: usize) -> SelfA read in pages, as crate::metrics::Query::limit has them.
aio::metrics::Query.after
pub fn after(self, next: &str) -> SelfThe page after the one whose next this is, as
crate::metrics::Query::after reads it.
aio::metrics::Query.every
pub fn every(self, span: Duration) -> SelfAn aggregate's buckets: spans this long, each beginning at a whole number of them since 1970 in UTC.
aio::metrics::Query.calendar
pub fn calendar(self, unit: Calendar, zone: &str) -> SelfAn aggregate's buckets on the calendar of a time zone, as
crate::metrics::Query::calendar has them.
aio::metrics::Query.by
pub fn by(self, labels: &[&str]) -> SelfThe labels an aggregate keeps: the series of a name that share them are one group.
aio::metrics::Query.without
pub fn without(self, labels: &[&str]) -> SelfThe labels an aggregate leaves out, keeping every other.
aio::metrics::Query.estimate
pub fn estimate(self) -> SelfA timer's percentile also gives a point between the bounds of the bucket that holds the rank: an estimate, which no measurement gave.
aio::metrics::Query.skip_nan
pub fn skip_nan(self) -> SelfAn aggregate passes a NaN sample over, counted in its bucket's
skipped, as crate::metrics::Query::skip_nan does.
aio::metrics::Query.fill
pub fn fill(self) -> SelfAn aggregate gives every bucket of the range that retention keeps,
as crate::metrics::Query::fill does.
aio::metrics::Query.read
pub fn read(&self) -> impl Future<Output = Result<SeriesPage>> + Send + 'staticThe samples of each series the query finds, in its range, whole or a
page of them, as crate::metrics::Query::read gives them.
aio::metrics::Query.aggregate
pub fn aggregate(&self, op: Op) -> impl Future<Output = Result<AggregatePage>> + Send + 'staticThe buckets of each group of the series the query finds, each one
exact value of op, rounded once, as
crate::metrics::Query::aggregate gives them.
aio::metrics::Query.latest
pub fn latest(&self) -> impl Future<Output = Result<Vec<Latest>>> + Send + 'staticThe newest sample of each series the query finds, when it has one in the range.
aio::metrics::Query.explain
pub fn explain(
&self,
op: impl Into<Option<Op>>,
) -> impl Future<Output = Result<Plan>> + Send + 'staticWhat read, or aggregate when given its op, would spend of each
bound of a query, and the bound it would stop at. It decodes no
sample.
aio::metrics::Query.compare
pub fn compare(&self, op: Op, window: Duration) -> CompareThe op over the newest window against the median of the windows
before it, as crate::metrics::Query::compare gives them.
aio::metrics::Compare
pub struct Compare { /* private fields */ }A comparison being asked, as crate::metrics::Compare is, run
awaited.
aio::metrics::Compare.against
pub fn against(self, last: Duration) -> SelfHow far back the windows it compares with reach.
aio::metrics::Compare.same_hour
pub fn same_hour(self) -> SelfCompares with the same hours of each earlier day alone.
aio::metrics::Compare.run
pub fn run(&self) -> impl Future<Output = Result<Compared>> + Send + 'staticThe newest window's op against the median of the earlier windows'.
aio::metrics::Compare.groups
pub fn groups(&self) -> impl Future<Output = Result<ComparePage>> + Send + 'staticWhat run finds, for each group of the query's series apart, whole
or a page of them, as crate::metrics::Compare::groups gives them.
aio::metrics::List
pub struct List<P> { /* private fields */ }A page of what a store holds, its options before its page, as
crate::metrics::List builds one: 1000 unless limit says.
aio::metrics::List.prefix
pub fn prefix(self, prefix: impl Into<String>) -> SelfOnly those that start with this text.
aio::metrics::List.after
pub fn after(self, next: impl Into<String>) -> SelfThe page that starts past this one, as a page's next says it.
aio::metrics::List.limit
pub fn limit(self, most: usize) -> SelfHow many a page holds at most, 1 to 10,000.
aio::metrics::List.page
pub fn page(self) -> impl Future<Output = Result<NamePage>> + Send + 'staticaio::metrics::List.page
pub fn page(self) -> impl Future<Output = Result<LabelPage>> + Send + 'staticaio::metrics::List.page
pub fn page(self) -> impl Future<Output = Result<ValuePage>> + Send + 'staticaio::metrics::Prometheus
pub struct Prometheus { /* private fields */ }A text an exporter serves, in Prometheus' format or OpenMetrics', and the
labels, the time and the keep its ingest gives every series of it, as
crate::metrics::Prometheus builds one.
aio::metrics::Prometheus.label
pub fn label(self, name: impl Into<String>, value: impl Display) -> PrometheusA label every series of the text gets beside its own, such as the
instance scraped.
aio::metrics::Prometheus.at
pub fn at(self, at: SystemTime) -> PrometheusThe time of every sample the text gives none: unless given, the
store's clock at the first poll of ingest's future.
aio::metrics::Prometheus.keep
pub fn keep(self, keep: Duration) -> PrometheusHow long every name of the text keeps its samples.
aio::metrics::Prometheus.ingest
pub fn ingest(&self, text: impl AsRef<[u8]>) -> impl Future<Output = Result<()>> + Send + 'staticReads the text and writes its series, every one or none, as
crate::metrics::Prometheus::ingest does. The text is copied when
the call is made, and the time of its samples that give none is read
in the future's first poll. The memory its reading and its write hold
is awaited on no thread, and the text is then read and written
blocking on a thread of the store's pool, which waits there for the
commit.
aio::metrics::Exposition
pub struct Exposition { /* private fields */ }What writes the newest sample of every series the store holds as
Prometheus' text format, as crate::metrics::Exposition builds one.
aio::metrics::Exposition.only_prefix
pub fn only_prefix(self, prefix: impl Into<String>) -> ExpositionOnly the names that start with this text.
aio::metrics::Exposition.since
pub fn since(self, span: Duration) -> ExpositionOnly the series whose newest sample is at most this old, from the store's clock where the write begins on the pool.
aio::metrics::Exposition.timestamps
pub fn timestamps(self, on: bool) -> ExpositionWhether each line carries its sample's time, as
crate::metrics::Exposition::timestamps says.
aio::metrics::Exposition.write
pub fn write<W: Write + Send + 'static>(
&self,
out: W,
) -> impl Future<Output = Result<(W, Written)>> + Send + 'staticWrites the text to out as crate::metrics::Exposition::write
does, blocking on a thread of the store's pool, and gives out back
with what it wrote: a Vec<u8> for a handler's body, a file for a
collector's. A future dropped stops at its next read.