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
//! Client library for exposing [prometheus][prometheus] metrics.
//!
//! Currently this crate only support text format for exposing metrics.
//!
//! [prometheus]: https://prometheus.io/
//!
//! # Examples
//!
//! ```
//! use prometrics::default_gatherer;
//! use prometrics::metrics::{CounterBuilder, GaugeBuilder};
//!
//! let mut counter = CounterBuilder::new("count")
//!     .default_registry()
//!     .finish()
//!     .unwrap();
//! let mut gauge = GaugeBuilder::new("gauge")
//!     .label("foo", "bar")
//!     .default_registry()
//!     .finish()
//!     .unwrap();
//!
//!  counter.increment();
//!  gauge.set(12.3);
//!
//!  let metrics = default_gatherer().lock().unwrap().gather();
//!  assert_eq!(
//!     metrics
//!         .into_iter()
//!         .map(|m| format!("\n{}", m))
//!         .collect::<Vec<_>>()
//!         .join(""),
//!     format!("\n{}\n{}\n\n{}\n{}\n",
//!             "# TYPE count counter",
//!             "count 1",
//!             "# TYPE gauge gauge",
//!             "gauge{foo=\"bar\"} 12.3"));
//! ```
//!
//! # References
//!
//! - [Data model](https://prometheus.io/docs/concepts/data_model/)
//! - [Metric types](https://prometheus.io/docs/concepts/metric_types/)
//! - [Writing client libraries](https://prometheus.io/docs/instrumenting/writing_clientlibs/)
//! - [Exposition formats](https://prometheus.io/docs/instrumenting/exposition_formats/)
#![warn(missing_docs)]
extern crate atomic_immut;
#[macro_use]
extern crate lazy_static;
#[cfg(target_os = "linux")]
extern crate libc;
#[cfg(target_os = "linux")]
extern crate procinfo;
#[macro_use]
extern crate trackable;

pub use collect::Collect;
pub use error::{Error, ErrorKind};
pub use registry::{default_registry, default_gatherer, Registry, Gatherer};

pub mod bucket;
pub mod label;
pub mod metric;
pub mod metrics;
pub mod quantile;
pub mod timestamp;

mod atomic;
mod collect;
mod error;
mod registry;

/// This crate specific `Result` type.
pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod test {
    use metrics::{CounterBuilder, GaugeBuilder};
    use super::*;

    #[test]
    fn it_works() {
        let mut counter = CounterBuilder::new("count")
            .default_registry()
            .finish()
            .unwrap();
        let mut gauge = GaugeBuilder::new("gauge")
            .label("foo", "bar")
            .default_registry()
            .finish()
            .unwrap();

        counter.increment();
        gauge.set(12.3);

        let metrics = default_gatherer().lock().unwrap().gather();
        assert_eq!(
            metrics
                .into_iter()
                .map(|m| format!("\n{}", m))
                .collect::<Vec<_>>()
                .join(""),
            r#"
# TYPE count counter
count 1

# TYPE gauge gauge
gauge{foo="bar"} 12.3
"#
        );
    }
}