Skip to content

Commit

Permalink
Clean up the use of macro_use and extern crate (#398)
Browse files Browse the repository at this point in the history
* Clean up the use of macro_use and extern crate

Signed-off-by: JmPotato <ghzpotato@gmail.com>

* Fix the build

Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato authored Mar 23, 2021
1 parent de89aae commit 1e7736f
Show file tree
Hide file tree
Showing 29 changed files with 82 additions and 118 deletions.
6 changes: 2 additions & 4 deletions examples/example_custom_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 3 additions & 5 deletions examples/example_hyper.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// 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},
Body, Request, Response, Server,
};
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",
Expand Down
10 changes: 5 additions & 5 deletions examples/example_int_metrics.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
8 changes: 3 additions & 5 deletions examples/example_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

#![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;

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",
Expand Down
15 changes: 6 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/// # Examples
///
/// ```
/// # #[macro_use] extern crate prometheus;
/// # use std::collections::HashMap;
/// # use prometheus::labels;
/// # fn main() {
/// let labels = labels!{
/// "test" => "hello",
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"]);
Expand Down
2 changes: 2 additions & 0 deletions src/process_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, Box<dyn Collector>>,
pub dim_hashes_by_name: HashMap<String, u64>,
Expand Down
2 changes: 2 additions & 0 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
1 change: 1 addition & 0 deletions static-metric/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions static-metric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 4 additions & 8 deletions static-metric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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! {

Expand Down
5 changes: 1 addition & 4 deletions static-metric/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 3 additions & 6 deletions static-metric/examples/advanced.rs
Original file line number Diff line number Diff line change
@@ -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! {
Expand Down
7 changes: 2 additions & 5 deletions static-metric/examples/local.rs
Original file line number Diff line number Diff line change
@@ -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! {
Expand Down
17 changes: 5 additions & 12 deletions static-metric/examples/make_auto_flush_static_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {

Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
Loading

0 comments on commit 1e7736f

Please sign in to comment.