From 1e7736f8bcac5600f787484262b7153af98f8345 Mon Sep 17 00:00:00 2001 From: JmPotato Date: Tue, 23 Mar 2021 18:53:57 +0800 Subject: [PATCH] Clean up the use of macro_use and extern crate (#398) * Clean up the use of macro_use and extern crate Signed-off-by: JmPotato * Fix the build Signed-off-by: JmPotato --- examples/example_custom_registry.rs | 6 ++---- examples/example_hyper.rs | 8 +++----- examples/example_int_metrics.rs | 10 +++++----- examples/example_push.rs | 8 +++----- src/lib.rs | 15 ++++++--------- src/macros.rs | 18 +++++++++--------- src/process_collector.rs | 2 ++ src/push.rs | 2 ++ src/registry.rs | 3 +++ src/timer.rs | 2 ++ static-metric/Cargo.toml | 1 + static-metric/Makefile | 2 ++ static-metric/README.md | 12 ++++-------- static-metric/benches/benches.rs | 5 +---- static-metric/examples/advanced.rs | 9 +++------ static-metric/examples/local.rs | 7 ++----- .../examples/make_auto_flush_static_counter.rs | 17 +++++------------ .../make_auto_flush_static_metric_histogram.rs | 16 +++++----------- static-metric/examples/metric_enum.rs | 4 +--- static-metric/examples/register_integration.rs | 13 ++++++------- static-metric/examples/simple.rs | 4 +--- static-metric/examples/with_lazy_static.rs | 9 +++------ static-metric/src/auto_flush_builder.rs | 2 ++ static-metric/src/auto_flush_from.rs | 2 ++ static-metric/src/builder.rs | 2 ++ static-metric/src/lib.rs | 13 +++---------- static-metric/src/register_macro.rs | 2 ++ static-metric/tests/label_enum.rs | 3 --- static-metric/tests/metric.rs | 3 --- 29 files changed, 82 insertions(+), 118 deletions(-) diff --git a/examples/example_custom_registry.rs b/examples/example_custom_registry.rs index 9b281d3a..0c99cb31 100644 --- a/examples/example_custom_registry.rs +++ b/examples/example_custom_registry.rs @@ -3,14 +3,12 @@ //! This examples shows how to use multiple and custom registries, //! and how to perform registration across function boundaries. -#[macro_use] -extern crate lazy_static; -extern crate prometheus; - use std::collections::HashMap; use prometheus::{Encoder, IntCounter, Registry}; +use lazy_static::lazy_static; + lazy_static! { static ref DEFAULT_COUNTER: IntCounter = IntCounter::new("default", "generic counter").unwrap(); static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap(); diff --git a/examples/example_hyper.rs b/examples/example_hyper.rs index f20e7826..1a3cea28 100644 --- a/examples/example_hyper.rs +++ b/examples/example_hyper.rs @@ -1,10 +1,5 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; - use hyper::{ header::CONTENT_TYPE, service::{make_service_fn, service_fn}, @@ -12,6 +7,9 @@ use hyper::{ }; use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder}; +use lazy_static::lazy_static; +use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec}; + lazy_static! { static ref HTTP_COUNTER: Counter = register_counter!(opts!( "example_http_requests_total", diff --git a/examples/example_int_metrics.rs b/examples/example_int_metrics.rs index e32fd1e2..05e57e81 100644 --- a/examples/example_int_metrics.rs +++ b/examples/example_int_metrics.rs @@ -1,12 +1,12 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; - use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec}; +use lazy_static::lazy_static; +use prometheus::{ + register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec, +}; + lazy_static! { static ref A_INT_COUNTER: IntCounter = register_int_counter!("A_int_counter", "foobar").unwrap(); diff --git a/examples/example_push.rs b/examples/example_push.rs index bd005e16..22d01953 100644 --- a/examples/example_push.rs +++ b/examples/example_push.rs @@ -2,11 +2,6 @@ #![cfg_attr(not(feature = "push"), allow(unused_imports, dead_code))] -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; - use std::env; use std::thread; use std::time; @@ -14,6 +9,9 @@ use std::time; use getopts::Options; use prometheus::{Counter, Histogram}; +use lazy_static::lazy_static; +use prometheus::{labels, register_counter, register_histogram}; + lazy_static! { static ref PUSH_COUNTER: Counter = register_counter!( "example_push_total", diff --git a/src/lib.rs b/src/lib.rs index a3bb9f68..b3bebd0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,10 +53,11 @@ This crate supports staticly built metrics. You can use it with some metrics. ```rust -#[macro_use] extern crate lazy_static; -#[macro_use] extern crate prometheus; use prometheus::{self, IntCounter, TextEncoder, Encoder}; +use lazy_static::lazy_static; +use prometheus::register_int_counter; + lazy_static! { static ref HIGH_FIVE_COUNTER: IntCounter = register_int_counter!("highfives", "Number of high fives received").unwrap(); @@ -71,11 +72,12 @@ By default, this registers with a default registry. To make a report, you can ca [`Encoder`](trait.Encoder.html) and report to Promethus. ``` -# #[macro_use] extern crate lazy_static; -#[macro_use] extern crate prometheus; # use prometheus::IntCounter; use prometheus::{self, TextEncoder, Encoder}; +use lazy_static::lazy_static; +use prometheus::register_int_counter; + // Register & measure some metrics. # lazy_static! { # static ref HIGH_FIVE_COUNTER: IntCounter = @@ -145,11 +147,6 @@ macro_rules! from_vec { }; } -#[macro_use] -extern crate cfg_if; -#[macro_use] -extern crate lazy_static; - #[macro_use] mod macros; mod atomic64; diff --git a/src/macros.rs b/src/macros.rs index 61511c73..3c643213 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -5,8 +5,8 @@ /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; /// # use std::collections::HashMap; +/// # use prometheus::labels; /// # fn main() { /// let labels = labels!{ /// "test" => "hello", @@ -41,7 +41,7 @@ macro_rules! labels { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{labels, opts}; /// # fn main() { /// let name = "test_opts"; /// let help = "test opts help"; @@ -87,8 +87,8 @@ macro_rules! opts { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; /// # use prometheus::linear_buckets; +/// # use prometheus::{histogram_opts, labels}; /// # fn main() { /// let name = "test_histogram_opts"; /// let help = "test opts help"; @@ -135,7 +135,7 @@ macro_rules! histogram_opts { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{opts, register_counter}; /// # fn main() { /// let opts = opts!("test_macro_counter_1", "help"); /// let res1 = register_counter!(opts); @@ -189,7 +189,7 @@ macro_rules! __register_counter_vec { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{opts, register_counter_vec}; /// # fn main() { /// let opts = opts!("test_macro_counter_vec_1", "help"); /// let counter_vec = register_counter_vec!(opts, &["a", "b"]); @@ -238,7 +238,7 @@ macro_rules! __register_gauge { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{opts, register_gauge}; /// # fn main() { /// let opts = opts!("test_macro_gauge", "help"); /// let res1 = register_gauge!(opts); @@ -287,7 +287,7 @@ macro_rules! __register_gauge_vec { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{opts, register_gauge_vec}; /// # fn main() { /// let opts = opts!("test_macro_gauge_vec_1", "help"); /// let gauge_vec = register_gauge_vec!(opts, &["a", "b"]); @@ -327,7 +327,7 @@ macro_rules! register_int_gauge_vec { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{histogram_opts, register_histogram}; /// # fn main() { /// let opts = histogram_opts!("test_macro_histogram", "help"); /// let res1 = register_histogram!(opts); @@ -363,7 +363,7 @@ macro_rules! register_histogram { /// # Examples /// /// ``` -/// # #[macro_use] extern crate prometheus; +/// # use prometheus::{histogram_opts, register_histogram_vec}; /// # fn main() { /// let opts = histogram_opts!("test_macro_histogram_vec_1", "help"); /// let histogram_vec = register_histogram_vec!(opts, &["a", "b"]); diff --git a/src/process_collector.rs b/src/process_collector.rs index 04b11f84..51d1039f 100644 --- a/src/process_collector.rs +++ b/src/process_collector.rs @@ -6,6 +6,8 @@ use std::sync::Mutex; +use lazy_static::lazy_static; + use crate::counter::Counter; use crate::desc::Desc; use crate::gauge::Gauge; diff --git a/src/push.rs b/src/push.rs index 80a253a6..525b3421 100644 --- a/src/push.rs +++ b/src/push.rs @@ -10,6 +10,8 @@ use reqwest::blocking::Client; use reqwest::header::CONTENT_TYPE; use reqwest::{Method, StatusCode, Url}; +use lazy_static::lazy_static; + use crate::encoder::{Encoder, ProtobufEncoder}; use crate::errors::{Error, Result}; use crate::metrics::Collector; diff --git a/src/registry.rs b/src/registry.rs index 97528e69..3aa0d22e 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -12,6 +12,9 @@ use crate::errors::{Error, Result}; use crate::metrics::Collector; use crate::proto; +use cfg_if::cfg_if; +use lazy_static::lazy_static; + struct RegistryCore { pub collectors_by_id: HashMap>, pub dim_hashes_by_name: HashMap, diff --git a/src/timer.rs b/src/timer.rs index 60bf93a8..7530c72d 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -2,6 +2,8 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::thread; use std::time::{Duration, Instant}; +use lazy_static::lazy_static; + /// Milliseconds since ANCHOR. static RECENT: AtomicU64 = AtomicU64::new(0); lazy_static! { diff --git a/static-metric/Cargo.toml b/static-metric/Cargo.toml index d72fcf95..fdf06f23 100644 --- a/static-metric/Cargo.toml +++ b/static-metric/Cargo.toml @@ -7,6 +7,7 @@ description = "Static metric helper utilities for rust-prometheus." repository = "https://github.com/tikv/rust-prometheus" homepage = "https://github.com/tikv/rust-prometheus" documentation = "https://docs.rs/prometheus-static-metric" +edition = "2018" [lib] proc-macro = true diff --git a/static-metric/Makefile b/static-metric/Makefile index 9f475cf8..a8b99a92 100644 --- a/static-metric/Makefile +++ b/static-metric/Makefile @@ -10,6 +10,8 @@ build: test: cargo test --features="${ENABLE_FEATURES}" -- --nocapture +dev: format test + format: @cargo fmt --all -- --check >/dev/null || cargo fmt --all diff --git a/static-metric/README.md b/static-metric/README.md index 8d845ec4..cdf66a1e 100644 --- a/static-metric/README.md +++ b/static-metric/README.md @@ -46,7 +46,7 @@ introducing a lot of templating code. - Add to `lib.rs`: ```rust - extern crate prometheus_static_metric; + use prometheus_static_metric; ``` ## Example @@ -89,14 +89,10 @@ fn main() { For heavier scenario that a global shared static-metric might not be effecient enough, you can use `make_auto_flush_static_metric!` macro, which will store data in local thread storage, with a custom rate to flush to global `MetricVec`. ```rust -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::*; -use prometheus_static_metric::auto_flush_from; -use prometheus_static_metric::make_auto_flush_static_metric; + +use lazy_static:lazy_static; +use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { diff --git a/static-metric/benches/benches.rs b/static-metric/benches/benches.rs index 9edfd570..e5230b0f 100644 --- a/static-metric/benches/benches.rs +++ b/static-metric/benches/benches.rs @@ -1,11 +1,8 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -extern crate criterion; -extern crate prometheus; -extern crate prometheus_static_metric; - use criterion::{criterion_group, criterion_main, Criterion}; use prometheus::{IntCounter, IntCounterVec, Opts}; + use prometheus_static_metric::make_static_metric; /// Single `IntCounter` performance. diff --git a/static-metric/examples/advanced.rs b/static-metric/examples/advanced.rs index 610a1f5b..5a788e71 100644 --- a/static-metric/examples/advanced.rs +++ b/static-metric/examples/advanced.rs @@ -1,12 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::IntCounterVec; + +use lazy_static::lazy_static; +use prometheus::register_int_counter_vec; use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/examples/local.rs b/static-metric/examples/local.rs index e648061d..5ea9247f 100644 --- a/static-metric/examples/local.rs +++ b/static-metric/examples/local.rs @@ -1,13 +1,10 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; - use std::cell::Cell; use prometheus::*; + +use lazy_static::lazy_static; use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/examples/make_auto_flush_static_counter.rs b/static-metric/examples/make_auto_flush_static_counter.rs index 2474b703..4c6fa1bb 100644 --- a/static-metric/examples/make_auto_flush_static_counter.rs +++ b/static-metric/examples/make_auto_flush_static_counter.rs @@ -5,14 +5,10 @@ Use metric enums to reuse possible values of a label. */ -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::*; -use prometheus_static_metric::auto_flush_from; -use prometheus_static_metric::make_auto_flush_static_metric; + +use lazy_static::lazy_static; +use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -95,11 +91,6 @@ fn main() { /* /// Pseudo macro expanded code of make_auto_flush_static_counter -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; - use std::cell::Cell; #[allow(unused_imports)] @@ -110,6 +101,8 @@ use std::mem; use std::mem::MaybeUninit; use std::thread::LocalKey; +use lazy_static::lazy_static; + #[allow(dead_code)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq)] diff --git a/static-metric/examples/make_auto_flush_static_metric_histogram.rs b/static-metric/examples/make_auto_flush_static_metric_histogram.rs index 22bb06cc..9ae3247f 100644 --- a/static-metric/examples/make_auto_flush_static_metric_histogram.rs +++ b/static-metric/examples/make_auto_flush_static_metric_histogram.rs @@ -5,14 +5,11 @@ Use metric enums to reuse possible values of a label. */ -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; use prometheus::*; -use prometheus_static_metric::auto_flush_from; -use prometheus_static_metric::make_auto_flush_static_metric; + +use lazy_static::lazy_static; +use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric}; make_auto_flush_static_metric! { @@ -90,11 +87,6 @@ fn main() { /* /// Pseudo macro expanded code of make_auto_flush_static_metric_histogram -#[macro_use] -extern crate lazy_static; -extern crate prometheus; -extern crate prometheus_static_metric; - use std::cell::Cell; #[allow(unused_imports)] @@ -105,6 +97,8 @@ use std::mem; use std::mem::MaybeUninit; use std::thread::LocalKey; +use lazy_static::lazy_static; + #[allow(dead_code)] #[allow(non_camel_case_types)] #[derive(Clone, Copy, PartialEq)] diff --git a/static-metric/examples/metric_enum.rs b/static-metric/examples/metric_enum.rs index d05a5690..8e835f93 100644 --- a/static-metric/examples/metric_enum.rs +++ b/static-metric/examples/metric_enum.rs @@ -6,10 +6,8 @@ Use metric enums to reuse possible values of a label. */ -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::{CounterVec, IntCounterVec, Opts}; + use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/examples/register_integration.rs b/static-metric/examples/register_integration.rs index 82d0ce91..6c0627a7 100644 --- a/static-metric/examples/register_integration.rs +++ b/static-metric/examples/register_integration.rs @@ -7,14 +7,13 @@ by using the `register_static_xxx!` macro provided by this crate. */ -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::exponential_buckets; -use prometheus_static_metric::*; + +use lazy_static::lazy_static; +use prometheus::{register_counter_vec, register_histogram_vec}; +use prometheus_static_metric::{ + make_static_metric, register_static_counter_vec, register_static_histogram_vec, +}; make_static_metric! { pub struct HttpRequestStatistics: Counter { diff --git a/static-metric/examples/simple.rs b/static-metric/examples/simple.rs index c27d9edb..25582a26 100644 --- a/static-metric/examples/simple.rs +++ b/static-metric/examples/simple.rs @@ -1,9 +1,7 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::{CounterVec, Opts}; + use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/examples/with_lazy_static.rs b/static-metric/examples/with_lazy_static.rs index 93cfa3a6..198a1c39 100644 --- a/static-metric/examples/with_lazy_static.rs +++ b/static-metric/examples/with_lazy_static.rs @@ -1,12 +1,9 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::CounterVec; + +use lazy_static::lazy_static; +use prometheus::register_counter_vec; use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/src/auto_flush_builder.rs b/static-metric/src/auto_flush_builder.rs index 24027af3..1c86bcb4 100644 --- a/static-metric/src/auto_flush_builder.rs +++ b/static-metric/src/auto_flush_builder.rs @@ -7,6 +7,8 @@ use proc_macro2::{Span, TokenStream as Tokens}; use quote::{ToTokens, TokenStreamExt}; use syn::{Ident, Visibility}; +use quote::quote; + use super::parser::*; use super::util; use crate::builder::TokensBuilder; diff --git a/static-metric/src/auto_flush_from.rs b/static-metric/src/auto_flush_from.rs index ecb4f81f..d89d375e 100644 --- a/static-metric/src/auto_flush_from.rs +++ b/static-metric/src/auto_flush_from.rs @@ -6,6 +6,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::*; use syn::*; +use quote::quote; + pub struct AutoFlushFromDef { class_name: Ident, inner_class_name: Ident, diff --git a/static-metric/src/builder.rs b/static-metric/src/builder.rs index c717e776..680c416c 100644 --- a/static-metric/src/builder.rs +++ b/static-metric/src/builder.rs @@ -7,6 +7,8 @@ use proc_macro2::{Span, TokenStream as Tokens}; use quote::TokenStreamExt; use syn::{Ident, Visibility}; +use quote::quote; + use super::parser::*; use super::util; diff --git a/static-metric/src/lib.rs b/static-metric/src/lib.rs index 98776506..fdb70b4f 100644 --- a/static-metric/src/lib.rs +++ b/static-metric/src/lib.rs @@ -6,10 +6,11 @@ This crate provides staticly built metrics to your Prometheus application. This is useful since it reduces the amount of branching and processing needed at runtime to collect metrics. ```rust -#[macro_use] extern crate lazy_static; -#[macro_use] extern crate prometheus; use prometheus::{self, IntCounter, TextEncoder, Encoder}; +use lazy_static::lazy_static; +use prometheus::register_int_counter; + lazy_static! { static ref HIGH_FIVE_COUNTER: IntCounter = register_int_counter!("highfives", "Number of high fives recieved").unwrap(); @@ -21,14 +22,6 @@ assert_eq!(HIGH_FIVE_COUNTER.get(), 1); Is it reccomended that you consult the [`prometheus` documentation for more information.](https://docs.rs/prometheus/) */ - -extern crate lazy_static; -extern crate proc_macro; -extern crate proc_macro2; -#[macro_use] -extern crate quote; -extern crate syn; - mod auto_flush_builder; mod auto_flush_from; mod builder; diff --git a/static-metric/src/register_macro.rs b/static-metric/src/register_macro.rs index 72d497d8..a41c45c4 100644 --- a/static-metric/src/register_macro.rs +++ b/static-metric/src/register_macro.rs @@ -5,6 +5,8 @@ use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::*; +use quote::quote; + /// Matches `register_static_xxx_vec!(static_struct_name, name, desc, labels, ...)`. pub struct RegisterMethodInvoking { static_struct_name: Ident, diff --git a/static-metric/tests/label_enum.rs b/static-metric/tests/label_enum.rs index aaed94d6..4acde6e5 100644 --- a/static-metric/tests/label_enum.rs +++ b/static-metric/tests/label_enum.rs @@ -1,8 +1,5 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus_static_metric::make_static_metric; make_static_metric! { diff --git a/static-metric/tests/metric.rs b/static-metric/tests/metric.rs index d605b38f..df45be68 100644 --- a/static-metric/tests/metric.rs +++ b/static-metric/tests/metric.rs @@ -1,8 +1,5 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. -extern crate prometheus; -extern crate prometheus_static_metric; - use prometheus::core::Collector; use prometheus::{Counter, CounterVec, Opts}; use prometheus_static_metric::make_static_metric;