#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::collapsible_if)]
pub mod emf;
pub mod flex;
pub mod instrument;
mod keep_alive;
pub mod timers;
pub mod slot;
use metrique_core::CloseEntry;
use metrique_writer_core::Entry;
use metrique_writer_core::EntryWriter;
use metrique_writer_core::entry::SampleGroupElement;
pub use slot::{FlushGuard, ForceFlushGuard, LazySlot, OnParentDrop, Slot, SlotGuard};
pub use flex::Flex;
use core::ops::Deref;
use core::ops::DerefMut;
use keep_alive::DropAll;
use keep_alive::Guard;
use keep_alive::Parent;
use metrique_writer_core::EntrySink;
use std::sync::Arc;
pub use metrique_core::{CloseValue, CloseValueRef, Counter, InflectableEntry, NameStyle};
pub mod unit {
pub use metrique_writer_core::unit::{
Bit, BitPerSecond, Byte, BytePerSecond, Count, Gigabit, GigabitPerSecond, Gigabyte,
GigabytePerSecond, Kilobit, KilobitPerSecond, Kilobyte, KilobytePerSecond, Megabit,
MegabitPerSecond, Megabyte, MegabytePerSecond, Microsecond, Millisecond, None, Percent,
Second, Terabit, TerabitPerSecond, Terabyte, TerabytePerSecond,
};
use metrique_writer_core::{MetricValue, unit::WithUnit};
#[doc(hidden)]
pub trait AttachUnit: Sized {
type Output<U>;
fn make<U>(self) -> Self::Output<U>;
}
impl<V: MetricValue> AttachUnit for V {
type Output<U> = WithUnit<V, U>;
fn make<U>(self) -> Self::Output<U> {
WithUnit::from(self)
}
}
}
#[doc(hidden)]
pub mod format {
pub use metrique_writer_core::value::FormattedValue;
}
#[cfg(feature = "test-util")]
pub mod test_util {
pub use crate::writer::test_util::{
Inspector, Metric, TestEntry, TestEntrySink, test_entry_sink, to_test_entry,
};
}
pub mod unit_of_work {
pub use metrique_macro::metrics;
}
pub type DefaultSink = metrique_writer_core::sink::BoxEntrySink;
pub struct AppendAndCloseOnDrop<E: CloseEntry, S: EntrySink<RootMetric<E>>> {
inner: Parent<AppendAndCloseOnDropInner<E, S>>,
}
impl<E: CloseEntry + Send + Sync + 'static, S: EntrySink<RootMetric<E>> + Send + Sync + 'static>
AppendAndCloseOnDrop<E, S>
{
pub fn flush_guard(&self) -> FlushGuard {
FlushGuard {
_drop_guard: self.inner.new_guard(),
}
}
pub fn force_flush_guard(&self) -> ForceFlushGuard {
ForceFlushGuard::new(self.inner.force_drop_guard())
}
pub fn handle(self) -> AppendAndCloseOnDropHandle<E, S> {
AppendAndCloseOnDropHandle {
inner: std::sync::Arc::new(self),
}
}
}
struct AppendAndCloseOnDropInner<E: CloseEntry, S: EntrySink<RootMetric<E>>> {
entry: Option<E>,
sink: S,
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> Deref for AppendAndCloseOnDrop<E, S> {
type Target = E;
fn deref(&self) -> &Self::Target {
self.inner.deref()
}
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> DerefMut for AppendAndCloseOnDrop<E, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.inner.deref_mut()
}
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> Deref for AppendAndCloseOnDropInner<E, S> {
type Target = E;
fn deref(&self) -> &Self::Target {
self.entry.as_ref().unwrap()
}
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> DerefMut for AppendAndCloseOnDropInner<E, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.entry.as_mut().unwrap()
}
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> Drop for AppendAndCloseOnDropInner<E, S> {
fn drop(&mut self) {
let entry = self.entry.take().expect("only drop calls this");
let entry = entry.close();
self.sink.append(RootEntry::new(entry));
}
}
pub struct AppendAndCloseOnDropHandle<E: CloseEntry, S: EntrySink<RootMetric<E>>> {
inner: Arc<AppendAndCloseOnDrop<E, S>>,
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> Clone for AppendAndCloseOnDropHandle<E, S> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<E: CloseEntry, S: EntrySink<RootMetric<E>>> std::ops::Deref
for AppendAndCloseOnDropHandle<E, S>
{
type Target = E;
fn deref(&self) -> &Self::Target {
self.inner.deref()
}
}
pub fn append_and_close<
C: CloseEntry + Send + Sync + 'static,
S: EntrySink<RootMetric<C>> + Send + Sync + 'static,
>(
base: C,
sink: S,
) -> AppendAndCloseOnDrop<C, S> {
AppendAndCloseOnDrop {
inner: Parent::new(AppendAndCloseOnDropInner {
entry: Some(base),
sink,
}),
}
}
pub struct SharedChild<T>(Arc<T>);
impl<T> SharedChild<T> {
pub fn new(value: T) -> Self {
Self(Arc::from(value))
}
}
impl<T> Clone for SharedChild<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: Default> Default for SharedChild<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> Deref for SharedChild<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[diagnostic::do_not_recommend]
impl<T: CloseValue> CloseValue for SharedChild<T> {
type Closed = Option<T::Closed>;
fn close(self) -> Self::Closed {
Arc::into_inner(self.0).map(|t| t.close())
}
}
pub type RootMetric<E> = RootEntry<<E as CloseValue>::Closed>;
pub struct RootEntry<M: InflectableEntry> {
metric: M,
}
impl<M: InflectableEntry> RootEntry<M> {
pub fn new(metric: M) -> Self {
Self { metric }
}
}
impl<M: InflectableEntry> Entry for RootEntry<M> {
fn write<'a>(&'a self, w: &mut impl EntryWriter<'a>) {
self.metric.write(w);
}
fn sample_group(&self) -> impl Iterator<Item = SampleGroupElement> {
self.metric.sample_group()
}
}
#[cfg(feature = "service-metrics")]
pub use metrique_service_metrics::ServiceMetrics;
#[cfg(feature = "metrics-rs-bridge")]
pub use metrique_metricsrs as metrics_rs;
pub use metrique_core::concat;
pub mod writer {
pub use metrique_writer::GlobalEntrySink;
pub use metrique_writer::{AnyEntrySink, BoxEntrySink, EntrySink};
pub use metrique_writer::{BoxEntry, EntryConfig, EntryWriter, core::Entry};
pub use metrique_writer::{Convert, Unit};
pub use metrique_writer::{EntryIoStream, IoStreamError};
pub use metrique_writer::{MetricFlags, MetricValue, Observation, Value, ValueWriter};
pub use metrique_writer::{ValidationError, ValidationErrorBuilder};
pub use metrique_writer_macro::MetriqueEntry as Entry;
pub use metrique_writer::AttachGlobalEntrySinkExt;
pub use metrique_writer::{AttachGlobalEntrySink, EntryIoStreamExt, FormatExt};
pub use metrique_writer::{entry, format, sample, sink, stream, value};
#[cfg(feature = "test-util")]
#[doc(hidden)] pub use metrique_writer::test_util;
#[doc(hidden)] pub use metrique_writer::unit;
#[doc(hidden)]
pub use metrique_writer::core;
}