use std::collections::BTreeMap;
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};
use super::Metrics;
use super::buckets::{Bounds, Buckets};
use super::engine::Engine;
use super::labels::Kept;
use super::names::checked_keep;
#[cfg(feature = "wire")]
use super::remote::Declared;
use super::series::{Kind, Labels};
use super::totals::{Form, Instrument, LOCAL, MOST_SETS, Read, Slot, Told};
use crate::sqlite::sql_error;
use crate::{Error, Result};
pub(super) const MOST_UNIT: usize = 32;
pub(super) const MOST_HELP: usize = 1024;
const MILLISECONDS: &str = "ms";
const KINDS: &str = "select name, kind from names where name in (select value from json_each(?1))";
pub trait Reading {
fn value(self) -> std::result::Result<f64, String>;
}
impl Reading for f64 {
fn value(self) -> std::result::Result<f64, String> {
Ok(self)
}
}
impl<E: fmt::Display> Reading for std::result::Result<f64, E> {
fn value(self) -> std::result::Result<f64, String> {
self.map_err(|error| error.to_string())
}
}
#[must_use = "an instrument opens at `open`"]
pub struct Builder<T> {
engine: Arc<Engine>,
name: String,
form: Form,
help: Option<String>,
unit: Option<String>,
keep: Option<Duration>,
buckets: Option<Buckets>,
max_series: Option<usize>,
read: Option<Read>,
labels: Labels,
owner: u64,
own: bool,
made: PhantomData<fn() -> T>,
}
impl<T> fmt::Debug for Builder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Builder")
.field("name", &self.name)
.field("form", &self.form)
.field("help", &self.help)
.field("unit", &self.unit)
.field("keep", &self.keep)
.field("buckets", &self.buckets)
.field("max_series", &self.max_series)
.finish()
}
}
impl Metrics {
pub fn counter(&self, name: impl Into<String>) -> Builder<Counter> {
Builder::of(&self.engine, name.into(), Form::Counter)
}
pub fn gauge(&self, name: impl Into<String>) -> Builder<Gauge> {
Builder::of(&self.engine, name.into(), Form::Gauge)
}
pub fn gauge_func<R: Reading>(
&self,
name: impl Into<String>,
read: impl Fn() -> R + Send + Sync + 'static,
) -> Builder<GaugeFunc> {
let mut builder = Builder::of(&self.engine, name.into(), Form::Gauge);
builder.read = Some(Box::new(move || read().value()));
builder
}
pub fn timer(&self, name: impl Into<String>) -> Builder<Timer> {
Builder::of(&self.engine, name.into(), Form::Timer)
}
pub fn histogram(&self, name: impl Into<String>) -> Builder<Histogram> {
Builder::of(&self.engine, name.into(), Form::Histogram)
}
pub fn flush(&self) -> Result<()> {
self.engine.write_totals()
}
}
impl<T> Builder<T> {
fn of(engine: &Arc<Engine>, name: String, form: Form) -> Builder<T> {
Builder {
engine: Arc::clone(engine),
name,
form,
help: None,
unit: None,
keep: None,
buckets: None,
max_series: None,
read: None,
labels: Labels::new(),
owner: LOCAL,
own: false,
made: PhantomData,
}
}
pub fn help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
pub fn keep(mut self, keep: Duration) -> Self {
self.keep = Some(keep);
self
}
pub(super) fn own(mut self) -> Self {
self.own = true;
self
}
fn opened(&mut self) -> Result<Arc<Instrument>> {
let engine = Arc::clone(&self.engine);
let opened = engine.open_instrument(self);
opened.map_err(|error| error.within(format!("metrics {}", self.name)))
}
fn set_unit(mut self, unit: impl Into<String>) -> Self {
self.unit = Some(unit.into());
self
}
fn set_buckets(mut self, buckets: Buckets) -> Self {
self.buckets = Some(buckets);
self
}
}
macro_rules! unit {
($($made:ty),*) => {$(
impl Builder<$made> {
pub fn unit(self, unit: impl Into<String>) -> Self {
self.set_unit(unit)
}
}
)*};
}
unit!(Counter, Gauge, GaugeFunc, Histogram);
macro_rules! buckets {
($($made:ty),*) => {$(
impl Builder<$made> {
pub fn buckets(self, buckets: impl Into<Buckets>) -> Self {
self.set_buckets(buckets.into())
}
}
)*};
}
buckets!(Timer, Histogram);
macro_rules! max_series {
($($made:ty),*) => {$(
impl Builder<$made> {
pub fn max_series(mut self, sets: usize) -> Self {
self.max_series = Some(sets);
self
}
}
)*};
}
max_series!(Counter, Gauge, Timer, Histogram);
impl Builder<Counter> {
pub fn open(mut self) -> Result<Counter> {
let instrument = self.opened()?;
let slot = instrument.base()?;
Ok(Counter { instrument, slot })
}
}
impl Builder<Gauge> {
pub fn open(mut self) -> Result<Gauge> {
let instrument = self.opened()?;
let slot = instrument.base()?;
Ok(Gauge { instrument, slot, owner: self.owner })
}
#[cfg(test)]
pub(super) fn owned_by(mut self, connection: &super::totals::Connection) -> Self {
self.owner = connection.id;
self
}
}
impl Builder<GaugeFunc> {
pub fn labels(mut self, labels: &[(&str, &str)]) -> Self {
self.labels = labels.iter().map(|(label, value)| ((*label).to_owned(), (*value).to_owned())).collect();
self
}
pub fn open(mut self) -> Result<()> {
self.opened().map(drop)
}
}
impl Builder<Timer> {
pub fn open(mut self) -> Result<Timer> {
let instrument = self.opened()?;
let slot = instrument.base()?;
Ok(Timer { instrument, slot })
}
}
impl Builder<Histogram> {
pub fn open(mut self) -> Result<Histogram> {
let instrument = self.opened()?;
let slot = instrument.base()?;
Ok(Histogram { instrument, slot })
}
}
impl Engine {
#[cfg(feature = "wire")]
pub(super) fn open_declared(self: &Arc<Engine>, declared: &Declared, form: Form) -> Result<Arc<Instrument>> {
let mut builder: Builder<()> = Builder::of(self, declared.name.clone(), form);
builder.help.clone_from(&declared.help);
builder.unit.clone_from(&declared.unit);
builder.keep = declared.keep;
builder.buckets.clone_from(&declared.buckets);
builder.max_series = declared.max_series;
builder.opened()
}
fn open_instrument<T>(&self, builder: &mut Builder<T>) -> Result<Arc<Instrument>> {
if !builder.own {
refuse_the_stores_own(&builder.name)?;
}
let bounds = builder.buckets.as_ref().map(Bounds::of).transpose()?;
let most_sets = match builder.max_series {
Some(sets) if !(1..=MOST_SETS).contains(&sets) => {
return Err(Error::invalid(format!("max_series {sets}: 1 to {MOST_SETS} sets of labels")));
}
sets => sets.unwrap_or(MOST_SETS),
};
check_description(builder)?;
let keep = builder.keep.map(checked_keep).transpose()?;
let written = builder.form.written(&builder.name, bounds.is_some());
for (name, _) in &written {
Kept::of(name, &Labels::new())?;
}
self.check_kinds(&written)?;
let keeps = written.iter().map(|(name, _)| (name.as_str(), keep)).collect();
self.names.settle(&self.file, &keeps)?;
let instrument = match builder.read.take() {
Some(read) => {
self.totals.reader(&builder.name, &builder.labels, read)?;
Arc::new(Instrument::new(&builder.name, Form::Gauge, None, MOST_SETS))
}
None => self.totals.instrument(&builder.name, builder.form, bounds.clone(), most_sets)?,
};
self.describe_written(builder, &written, bounds.as_ref());
Ok(instrument)
}
fn check_kinds(&self, written: &[(String, Kind)]) -> Result<()> {
let names: Vec<&str> = written.iter().map(|(name, _)| name.as_str()).collect();
let names = serde_json::to_string(&names).map_err(|error| Error::internal("names").with_source(error))?;
let failed = |error| sql_error("read the kinds of names", error);
let held: BTreeMap<String, Option<String>> = self.file.read(|connection| {
let mut select = connection.prepare_cached(KINDS).map_err(failed)?;
let rows = select.query_map([names], |row| Ok((row.get(0)?, row.get(1)?)));
rows.and_then(Iterator::collect).map_err(failed)
})?;
for (name, kind) in written {
if let Some(Some(held)) = held.get(name)
&& held != kind.as_str()
{
return Err(Error::invalid(format!(
"{name}: a {kind}, where the name's series are {held}s: a name has one kind while it has series"
)));
}
}
Ok(())
}
fn describe_written<T>(&self, builder: &Builder<T>, written: &[(String, Kind)], bounds: Option<&Bounds>) {
let unit = match builder.form {
Form::Timer => Some(MILLISECONDS.to_owned()),
_ => builder.unit.clone(),
};
for (name, kind) in written {
let counts = builder.form != Form::Counter && *kind == Kind::Counter && name.ends_with("_count");
let description = Told {
unit: if counts { None } else { unit.clone() },
help: builder.help.clone(),
buckets: (*kind == Kind::Histogram).then(|| bounds.map(Bounds::text)).flatten(),
};
if description != Told::default() {
self.totals.describe(name, description);
}
}
}
}
fn check_description<T>(builder: &Builder<T>) -> Result<()> {
if builder.unit.as_ref().is_some_and(|unit| unit.len() > MOST_UNIT) {
return Err(Error::invalid(format!("a unit of {MOST_UNIT} bytes at most")));
}
if builder.help.as_ref().is_some_and(|help| help.len() > MOST_HELP) {
return Err(Error::invalid(format!("a help of {MOST_HELP} bytes at most")));
}
Ok(())
}
#[derive(Clone)]
pub struct Counter {
instrument: Arc<Instrument>,
slot: Arc<Slot>,
}
impl Counter {
pub fn with(&self, labels: &[(&str, &str)]) -> Counter {
Counter { instrument: Arc::clone(&self.instrument), slot: self.instrument.with(&self.slot, labels) }
}
pub fn inc(&self) {
self.add(1.0);
}
pub(super) fn advance_to(&self, total: u64) -> u64 {
self.slot.advance_to(total)
}
pub fn add(&self, n: f64) {
if !(n.is_finite() && n >= 0.0) {
self.slot.refuse(&format!("a counter's add of {n}: a counter grows by a finite number of 0 or more"));
} else if !self.slot.add(n) {
self.slot.refuse("a counter's total past what a float holds");
}
}
}
#[derive(Clone)]
pub struct Gauge {
instrument: Arc<Instrument>,
slot: Arc<Slot>,
owner: u64,
}
impl Gauge {
pub fn with(&self, labels: &[(&str, &str)]) -> Gauge {
Gauge {
instrument: Arc::clone(&self.instrument),
slot: self.instrument.with(&self.slot, labels),
owner: self.owner,
}
}
pub fn set(&self, value: f64) {
if value.is_finite() {
self.slot.set(value, self.owner);
} else {
self.slot.refuse(&format!("a gauge set to {value}: a gauge is a finite number"));
}
}
pub fn add(&self, delta: f64) {
if !(delta.is_finite() && self.slot.shift(delta, self.owner)) {
self.slot.refuse(&format!("a gauge moved by {delta}: a gauge is a finite number"));
}
}
pub fn inc(&self) {
self.add(1.0);
}
pub fn dec(&self) {
self.add(-1.0);
}
}
#[derive(Clone)]
pub struct Timer {
instrument: Arc<Instrument>,
slot: Arc<Slot>,
}
impl Timer {
pub fn with(&self, labels: &[(&str, &str)]) -> Timer {
Timer { instrument: Arc::clone(&self.instrument), slot: self.instrument.with(&self.slot, labels) }
}
pub fn record(&self, duration: Duration) {
let milliseconds = duration.as_nanos() as f64 / 1e6;
self.slot.measure(milliseconds, self.instrument.bounds.as_ref());
}
pub fn start(&self) -> Timing {
Timing { timer: self.clone(), started: Instant::now() }
}
pub fn measure<T>(&self, work: impl FnOnce() -> T) -> T {
let _timing = self.start();
work()
}
}
#[must_use = "a timing records when it drops: keep it in a name, `let _timing = latency.start()`"]
pub struct Timing {
timer: Timer,
started: Instant,
}
impl Drop for Timing {
fn drop(&mut self) {
self.timer.record(self.started.elapsed());
}
}
#[derive(Clone)]
pub struct Histogram {
instrument: Arc<Instrument>,
slot: Arc<Slot>,
}
impl Histogram {
pub fn with(&self, labels: &[(&str, &str)]) -> Histogram {
Histogram { instrument: Arc::clone(&self.instrument), slot: self.instrument.with(&self.slot, labels) }
}
pub fn record(&self, value: f64) {
if value.is_finite() && value >= 0.0 {
self.slot.measure(value, self.instrument.bounds.as_ref());
} else {
self.slot.refuse(&format!("a histogram's value of {value}: a finite number of 0 or more"));
}
}
}
#[derive(Debug)]
pub struct GaugeFunc {
_made: (),
}
macro_rules! debug_as_series {
($($handle:ty),*) => {$(
impl fmt::Debug for $handle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(stringify!($handle)).field(&self.slot.kept.to_string()).finish()
}
}
)*};
}
debug_as_series!(Counter, Gauge, Timer, Histogram);
impl fmt::Debug for Timing {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Timing").field("timer", &self.timer).field("started", &self.started).finish()
}
}
#[cfg(test)]
#[path = "instruments_tests.rs"]
mod tests;
pub(super) fn refuse_the_stores_own(name: &str) -> Result<()> {
if name.starts_with(super::own::PREFIX) {
return Err(Error::invalid(format!(
"{name}: a name that starts with {} is the store's own, which it writes when opened with self_metrics",
super::own::PREFIX
)));
}
Ok(())
}