1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Numbers that move over time: a program's requests, its queue's length, how
//! long its calls take, and samples other programs send, kept exactly in
//! `metrics.db` and read back by series and range.
//!
//! ```no_run
//! # use std::time::{Duration, SystemTime};
//! # fn main() -> tinystore::Result<()> {
//! use tinystore::metrics::Series;
//! let store = tinystore::Store::open("data", Default::default())?;
//! let stats = store.metrics()?;
//! let requests = stats.counter("http_requests_total").open()?;
//! requests.with(&[("route", "/notes")]).inc();
//! stats.ingest(&[Series::gauge("cpu").label("host", "web-1").sample(SystemTime::now(), 0.42)])?;
//! let cpu = stats.query().name("cpu").since(Duration::from_hours(1)).read()?.series;
//! assert_eq!(cpu[0].labels()["host"], "web-1");
//! # Ok(())
//! # }
//! ```
//!
//! A program measures itself with instruments, whose totals the store keeps
//! and writes every 15 seconds; samples from elsewhere come by `ingest`.
//! Both write series, and every read finds both.
mod aggregate;
mod binary;
mod bucket;
mod buckets;
mod budget;
mod clock;
mod codec;
mod counting;
mod damage;
mod delete;
mod describe;
mod directory;
mod engine;
mod exact;
mod explain;
#[cfg(test)]
pub(crate) mod fixture;
mod flush;
#[cfg(any(test, feature = "fuzzing"))]
#[doc(hidden)]
pub mod fuzzing;
mod grid;
mod head;
mod held;
mod ingest;
mod instruments;
mod labels;
mod ladder;
mod latest;
mod listing;
mod maintain;
mod matching;
mod names;
mod own;
mod pack;
mod page;
pub(crate) mod prometheus;
mod publish;
mod query;
mod rank;
mod read;
mod recorder;
mod registry;
#[cfg(feature = "wire")]
mod remote;
mod repeat;
mod replace;
mod residuals;
mod retention;
mod series;
mod snapshot;
mod spread;
mod summary;
#[cfg(feature = "system")]
mod system;
mod timer;
mod timestamp;
mod totals;
mod values;
use std::fmt;
use std::sync::Arc;
pub use aggregate::{Aggregate, Bucket, Op, Value};
pub use buckets::{Buckets, exponential_buckets};
pub use damage::Damage;
pub use describe::Description;
pub use exact::ExactSum;
pub use explain::{Exceeds, Plan};
pub use held::{SeriesQuery, query};
pub use instruments::{Builder, Counter, Gauge, GaugeFunc, Histogram, Reading, Timer, Timing};
pub use latest::Latest;
pub use listing::{LabelPage, List, NameInfo, NamePage, ValuePage};
pub use maintain::{Maintenance, maintain};
pub use own::SelfMetrics;
pub(crate) use own::measure_the_store;
pub use page::{AggregatePage, All, SeriesPage};
pub use prometheus::Prometheus;
pub use query::{Condition, Not, Prefix, Query, Values};
pub use recorder::Recorder;
#[cfg(feature = "wire")]
pub(crate) use remote::{Ack, Batch, Change, Declared, Remote, Set};
pub use series::{Kind, Labels, Sample, Series};
use crate::{Result, Store};
impl Store {
/// The store's metrics: series written by `ingest`, read back by `query`.
/// The engine opens with the first call.
pub fn metrics(&self) -> Result<Metrics> {
Ok(Metrics { engine: engine::Engine::of(self)? })
}
}
/// The store's metrics, a handle that clones cheaply.
#[derive(Clone)]
pub struct Metrics {
engine: Arc<engine::Engine>,
}
impl fmt::Debug for Metrics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Metrics")
}
}
impl Metrics {
/// 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.
pub fn ingest(&self, series: &[Series]) -> Result<()> {
self.engine.ingest(series)
}
/// An ingest's series checked and gathered, before the call reserves the
/// store's memory their write holds: an awaited ingest waits for those
/// bytes on no thread, then writes them with `ingest_checked`.
#[cfg(feature = "async")]
pub(crate) fn check(&self, series: &[Series]) -> Result<ingest::Checked> {
self.engine.check(series)
}
#[cfg(feature = "async")]
pub(crate) fn ingest_checked(&self, checked: ingest::Checked, held: crate::Reservation) -> Result<()> {
self.engine.ingest_checked(checked, held)
}
/// The store's memory, which an awaited ingest waits for on no thread.
#[cfg(feature = "async")]
pub(crate) fn memory(&self) -> &Arc<crate::Memory> {
&self.engine.memory
}
/// A query of series by name and labels, over a range of time, which
/// `read`, `aggregate`, `latest` or `explain` then runs.
pub fn query(&self) -> Query {
Query::of(Arc::clone(&self.engine))
}
/// The store these metrics are of, while a handle of it is held: an
/// async handle made from them runs its calls on that store's pool.
#[cfg(feature = "async")]
pub(crate) fn store(&self) -> Option<Store> {
self.engine.store.upgrade()
}
#[cfg(all(test, feature = "async"))]
pub(crate) fn file(&self) -> &crate::sqlite::File {
&self.engine.file
}
}