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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! # Instrumented
//!
//! `instrumented` provides an attribute macro that enables instrumentation of
//! functions for use with Prometheus.
//!
//! This crate is largely based on the [`log-derive`](https://docs.rs/log-derive/) crate, and
//! inspired by the [`metered`](https://docs.rs/metered/) crate.
//!
//! To get started, add the [`instrumented_codegen::instrument`] proc macro to any function you
//! want to instrument. Have a look in the `example` directory for a full usage.
//!
//! ## Configuring
//!
//! You can specify the global metrics prefix with the `METRICS_PREFIX` env var,
//! and provide default labels with the `METRICS_LABELS` env var, which accepts a
//! command separated list of `label=value` pairs. For example:
//!
//! ```shell
//! METRICS_PREFIX=myapp
//! METRICS_LABELS=app=myapp,env=prod,region=us
//! ```
//!
//! ## Example
//!
//! ```rust
//! extern crate instrumented;
//! extern crate log;
//! extern crate reqwest;
//!
//! use instrumented::instrument;
//!
//! // Logs at warn level with the `special` context.
//! #[instrument(WARN, ctx = "special")]
//! fn my_func() {
//!     use std::{thread, time};
//!     let ten_millis = time::Duration::from_millis(10);
//!     thread::sleep(ten_millis);
//! }
//!
//! #[derive(Debug)]
//! pub struct MyError;
//!
//! // Logs result at info level
//! #[instrument(INFO)]
//! fn my_func_with_ok_result() -> Result<String, MyError> {
//!     use std::{thread, time};
//!     let ten_millis = time::Duration::from_millis(10);
//!     thread::sleep(ten_millis);
//!
//!     Ok(String::from("hello world"))
//! }
//!
//! // Logs result at debug level
//! #[instrument(DEBUG)]
//! fn my_func_with_err_result() -> Result<String, MyError> {
//!     use std::{thread, time};
//!     let ten_millis = time::Duration::from_millis(10);
//!     thread::sleep(ten_millis);
//!
//!     Err(MyError)
//! }
//!
//! fn main() {
//!     let addr = "127.0.0.1:5000".to_string();
//!     instrumented::init(&addr);
//!
//!     my_func();
//!     assert_eq!(my_func_with_ok_result().is_ok(), true);
//!     assert_eq!(my_func_with_err_result().is_err(), true);
//!
//!     let body = reqwest::get(&format!("http://{}/metrics", addr))
//!         .unwrap()
//!         .text()
//!         .unwrap();
//!
//!     println!("{}", body);
//! }
//! ```
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate hyper;
#[allow(unused_imports)]
#[macro_use]
extern crate instrumented_codegen;

/// Codegen crate
pub use instrumented_codegen::instrument;

/// `rust-prometheus` crate
pub mod prometheus {
    extern crate prometheus;
    /// `rust-prometheus` crate
    pub use self::prometheus::*;
}

use hyper::http::StatusCode;
use hyper::rt::Future;
use hyper::service::service_fn_ok;
use hyper::{Body, Request, Response, Server};

#[cfg(all(target_os = "linux"))]
fn register_default_process_collector(
    reg: &crate::prometheus::Registry,
) -> crate::prometheus::Result<()> {
    use crate::prometheus::process_collector::ProcessCollector;

    let pc = ProcessCollector::for_self();
    reg.register(Box::new(pc))
}

lazy_static! {
    static ref INSTRUMENTED_REGISTRY: ::prometheus::Registry = {
        use std::collections::HashMap;

        let prefix =  match std::env::var("METRICS_PREFIX")  {
            Ok(value) => Some(value.to_string()),
            Err(_) => None,
        };
        let labels =  match std::env::var("METRICS_LABELS")  {
            Ok(value) => {
                let mut labels = HashMap::new();
                value.split(',').for_each(|s| {let v: Vec<&str> = s.splitn(2, '=').collect();
                if v.len() ==2 {
                    labels.insert(v[0].to_owned(), v[1].to_owned());
                }});
                Some(labels)
            },
            Err(_) => None,
        };

        #[allow(clippy::let_and_return)]
        let reg = ::prometheus::Registry::new_custom(prefix, labels).unwrap();

        // Register a default process collector.
        #[cfg(all(target_os = "linux"))]
        register_default_process_collector(&reg).unwrap();

        reg
    };
    static ref FUNC_CALLED: prometheus::IntCounterVec = {
        let counter_opts = prometheus::Opts::new(
            "function_called_total",
            "Number of times a function was called",
        );
        let counter = prometheus::IntCounterVec::new(counter_opts, &["type","name","ctx"]).unwrap();

        INSTRUMENTED_REGISTRY
            .register(Box::new(counter.clone())).unwrap();

        counter
    };
    static ref FUNC_ERRORS: prometheus::IntCounterVec = {
        let counter_opts = prometheus::Opts::new(
            "function_error_total",
            "Number of times the result of a function was an error",
        );
        let counter = prometheus::IntCounterVec::new(counter_opts, &["type","name","ctx","err"]).unwrap();

        INSTRUMENTED_REGISTRY
            .register(Box::new(counter.clone())).unwrap();

        counter
    };
    static ref FUNC_TIMER: prometheus::HistogramVec = {
        let histogram_opts = prometheus::HistogramOpts::new(
            "function_time_seconds",
            "Histogram of function call times observed",
        );
        let histogram = prometheus::HistogramVec::new(histogram_opts, &["type","name","ctx"]).unwrap();

        INSTRUMENTED_REGISTRY
            .register(Box::new(histogram.clone())).unwrap();

        histogram
    };
    static ref FUNC_INFLIGHT: prometheus::IntGaugeVec = {
        let gauge_opts = prometheus::Opts::new(
            "function_calls_inflight_total",
            "Number of function calls currently in flight",
        );
        let gauge = prometheus::IntGaugeVec::new(gauge_opts, &["type","name","ctx"]).unwrap();

        INSTRUMENTED_REGISTRY
            .register(Box::new(gauge.clone())).unwrap();

        gauge
    };
}

#[doc(hidden)]
pub fn inc_called_counter_for(name: &'static str, ctx: &'static str) {
    FUNC_CALLED
        .with_label_values(&["func_call", name, ctx])
        .inc();
}

#[doc(hidden)]
pub fn inc_error_counter_for(name: &'static str, ctx: &'static str, err: String) {
    FUNC_ERRORS
        .with_label_values(&["func_call", name, ctx, &err])
        .inc();
}

#[doc(hidden)]
pub fn get_timer_for(name: &'static str, ctx: &'static str) -> prometheus::HistogramTimer {
    FUNC_TIMER
        .with_label_values(&["func_call", name, ctx])
        .start_timer()
}

#[doc(hidden)]
pub fn inc_inflight_for(name: &'static str, ctx: &'static str) {
    FUNC_INFLIGHT
        .with_label_values(&["func_call", name, ctx])
        .inc();
}

#[doc(hidden)]
pub fn dec_inflight_for(name: &'static str, ctx: &'static str) {
    FUNC_INFLIGHT
        .with_label_values(&["func_call", name, ctx])
        .dec();
}

/// Initializes the metrics context, and starts an HTTP server
/// to serve metrics.
pub fn init(addr: &str) {
    let parsed_addr = addr.parse().unwrap();
    let server = Server::bind(&parsed_addr)
        .serve(|| {
            // This is the `Service` that will handle the connection.
            // `service_fn_ok` is a helper to convert a function that
            // returns a Response into a `Service`.
            service_fn_ok(move |req: Request<Body>| {
                use crate::prometheus::*;
                if req.uri().path() == "/metrics" {
                    let metric_families = INSTRUMENTED_REGISTRY.gather();
                    let mut buffer = vec![];
                    let encoder = TextEncoder::new();
                    encoder.encode(&metric_families, &mut buffer).unwrap();

                    Response::builder()
                        .status(StatusCode::OK)
                        .header("Content-Type", encoder.format_type())
                        .body(Body::from(buffer))
                        .expect("Error constructing response")
                } else {
                    Response::builder()
                        .status(StatusCode::NOT_FOUND)
                        .body(Body::from("Not found."))
                        .expect("Error constructing response")
                }
            })
        })
        .map_err(|e| error!("server error: {}", e));

    info!("Exporting metrics at http://{}/metrics", addr);

    let mut rt = tokio::runtime::Builder::new()
        .core_threads(1) // one thread is sufficient
        .build()
        .expect("Unable to build metrics exporter tokio runtime");

    std::thread::spawn(move || {
        rt.spawn(server);
        rt.shutdown_on_idle().wait().unwrap();
    });
}

/// Register a collector with the global registry.
pub fn register(c: Box<dyn::prometheus::core::Collector>) -> ::prometheus::Result<()> {
    INSTRUMENTED_REGISTRY.register(c)
}