prometheus_static_metric/
lib.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3/*!
4This crate provides staticly built metrics to your Prometheus application.
5
6This is useful since it reduces the amount of branching and processing needed at runtime to collect metrics.
7
8```rust
9#[macro_use] extern crate lazy_static;
10#[macro_use] extern crate prometheus;
11use prometheus::{self, IntCounter, TextEncoder, Encoder};
12
13lazy_static! {
14    static ref HIGH_FIVE_COUNTER: IntCounter =
15        register_int_counter!("highfives", "Number of high fives recieved").unwrap();
16}
17
18HIGH_FIVE_COUNTER.inc();
19assert_eq!(HIGH_FIVE_COUNTER.get(), 1);
20```
21
22Is it reccomended that you consult the [`prometheus` documentation for more information.](https://docs.rs/prometheus/)
23*/
24
25extern crate lazy_static;
26extern crate proc_macro;
27extern crate proc_macro2;
28#[macro_use]
29extern crate quote;
30extern crate syn;
31
32mod auto_flush_builder;
33mod auto_flush_from;
34mod builder;
35mod parser;
36mod register_macro;
37mod util;
38
39use proc_macro::TokenStream;
40
41use self::builder::TokensBuilder;
42use self::parser::StaticMetricMacroBody;
43use self::register_macro::RegisterMethodInvoking;
44use crate::auto_flush_from::AutoFlushFromDef;
45use auto_flush_builder::AutoFlushTokensBuilder;
46
47/// Build static metrics.
48#[proc_macro]
49pub fn make_static_metric(input: TokenStream) -> TokenStream {
50    let body: StaticMetricMacroBody = syn::parse(input).unwrap();
51    TokensBuilder::build(body).into()
52}
53
54/// Build auto flush able static metrics.
55/// refer to https://github.com/tikv/rust-prometheus/tree/master/static-metric for more info.
56#[proc_macro]
57pub fn make_auto_flush_static_metric(input: TokenStream) -> TokenStream {
58    let body: StaticMetricMacroBody = syn::parse(input).unwrap();
59    AutoFlushTokensBuilder::build(body).into()
60}
61
62/// Instantiate a auto flush able static metric struct from a HistogramVec or CounterVec.
63#[proc_macro]
64pub fn auto_flush_from(input: TokenStream) -> TokenStream {
65    let def: AutoFlushFromDef = syn::parse(input).unwrap();
66    def.auto_flush_from()
67}
68
69/// Register a `CounterVec` and create static metrics from it.
70#[proc_macro]
71pub fn register_static_counter_vec(input: TokenStream) -> TokenStream {
72    register_static_vec("counter", input)
73}
74
75/// Register a `IntCounterVec` and create static metrics from it.
76#[proc_macro]
77pub fn register_static_int_counter_vec(input: TokenStream) -> TokenStream {
78    register_static_vec("int_counter", input)
79}
80
81/// Register a `GaugeVec` and create static metrics from it.
82#[proc_macro]
83pub fn register_static_gauge_vec(input: TokenStream) -> TokenStream {
84    register_static_vec("gauge", input)
85}
86
87/// Register a `IntGaugeVec` and create static metrics from it.
88#[proc_macro]
89pub fn register_static_int_gauge_vec(input: TokenStream) -> TokenStream {
90    register_static_vec("int_gauge", input)
91}
92
93/// Register a `HistogramVec` and create static metrics from it.
94#[proc_macro]
95pub fn register_static_histogram_vec(input: TokenStream) -> TokenStream {
96    register_static_vec("histogram", input)
97}
98
99/// Procedural macro handler for `register_static_xxx_vec!`.
100fn register_static_vec(register_type: &str, input: TokenStream) -> TokenStream {
101    let invoking: RegisterMethodInvoking = syn::parse(input).unwrap();
102    invoking.into_tokens(register_type).into()
103}