Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ push = ["reqwest", "libc", "protobuf"]
[dependencies]
cfg-if = "^1.0"
fnv = "^1.0"
lazy_static = "^1.4"
libc = { version = "^0.2", optional = true }
parking_lot = "^0.12"
protobuf = { version = "^3.7.2", optional = true }
Expand Down
11 changes: 5 additions & 6 deletions examples/example_custom_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
//! and how to perform registration across function boundaries.

use std::collections::HashMap;
use std::sync::LazyLock;

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();
}
static DEFAULT_COUNTER: LazyLock<IntCounter> =
LazyLock::new(|| IntCounter::new("default", "generic counter").unwrap());
static CUSTOM_COUNTER: LazyLock<IntCounter> =
LazyLock::new(|| IntCounter::new("custom", "dedicated counter").unwrap());

fn main() {
// Register default metrics.
Expand Down
22 changes: 13 additions & 9 deletions examples/example_hyper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::net::SocketAddr;
use std::sync::LazyLock;

use hyper::body::Incoming;
use hyper::header::CONTENT_TYPE;
Expand All @@ -9,33 +10,36 @@ use hyper::service::service_fn;
use hyper::Request;
use hyper::Response;
use hyper_util::rt::TokioIo;
use lazy_static::lazy_static;
use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec};
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
use tokio::net::TcpListener;

type BoxedErr = Box<dyn std::error::Error + Send + Sync + 'static>;

lazy_static! {
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
static HTTP_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
register_counter!(opts!(
"example_http_requests_total",
"Number of HTTP requests made.",
labels! {"handler" => "all",}
))
.unwrap();
static ref HTTP_BODY_GAUGE: Gauge = register_gauge!(opts!(
.unwrap()
});
static HTTP_BODY_GAUGE: LazyLock<Gauge> = LazyLock::new(|| {
register_gauge!(opts!(
"example_http_response_size_bytes",
"The HTTP response sizes in bytes.",
labels! {"handler" => "all",}
))
.unwrap();
static ref HTTP_REQ_HISTOGRAM: HistogramVec = register_histogram_vec!(
.unwrap()
});
static HTTP_REQ_HISTOGRAM: LazyLock<HistogramVec> = LazyLock::new(|| {
register_histogram_vec!(
"example_http_request_duration_seconds",
"The HTTP request latencies in seconds.",
&["handler"]
)
.unwrap();
}
.unwrap()
});

async fn serve_req(_req: Request<Incoming>) -> Result<Response<String>, BoxedErr> {
let encoder = TextEncoder::new();
Expand Down
22 changes: 11 additions & 11 deletions examples/example_int_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
use std::sync::LazyLock;

use lazy_static::lazy_static;
use prometheus::{
register_int_counter, register_int_counter_vec, register_int_gauge, register_int_gauge_vec,
};
use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec};

lazy_static! {
static ref A_INT_COUNTER: IntCounter =
register_int_counter!("A_int_counter", "foobar").unwrap();
static ref A_INT_COUNTER_VEC: IntCounterVec =
register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap();
static ref A_INT_GAUGE: IntGauge = register_int_gauge!("A_int_gauge", "foobar").unwrap();
static ref A_INT_GAUGE_VEC: IntGaugeVec =
register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap();
}
static A_INT_COUNTER: LazyLock<IntCounter> =
LazyLock::new(|| register_int_counter!("A_int_counter", "foobar").unwrap());
static A_INT_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!("A_int_counter_vec", "foobar", &["a", "b"]).unwrap()
});
static A_INT_GAUGE: LazyLock<IntGauge> =
LazyLock::new(|| register_int_gauge!("A_int_gauge", "foobar").unwrap());
static A_INT_GAUGE_VEC: LazyLock<IntGaugeVec> =
LazyLock::new(|| register_int_gauge_vec!("A_int_gauge_vec", "foobar", &["a", "b"]).unwrap());

fn main() {
A_INT_COUNTER.inc();
Expand Down
19 changes: 10 additions & 9 deletions examples/example_push.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::env;
use std::sync::LazyLock;
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};
use prometheus::{Counter, Histogram};

lazy_static! {
static ref PUSH_COUNTER: Counter = register_counter!(
static PUSH_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
register_counter!(
"example_push_total",
"Total number of prometheus client pushed."
)
.unwrap();
static ref PUSH_REQ_HISTOGRAM: Histogram = register_histogram!(
.unwrap()
});
static PUSH_REQ_HISTOGRAM: LazyLock<Histogram> = LazyLock::new(|| {
register_histogram!(
"example_push_request_duration_seconds",
"The push request latencies in seconds."
)
.unwrap();
}
.unwrap()
});

fn main() {
let args: Vec<String> = env::args().collect();
Expand Down
2 changes: 1 addition & 1 deletion proto/proto_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file is generated by rust-protobuf 3.7.2. Do not edit
// .proto file is parsed by protoc 29.3
// .proto file is parsed by protoc 3.21.12
// @generated

// https://github.com/rust-lang/rust-clippy/issues/702
Expand Down
21 changes: 8 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,16 @@ You can find more examples within
# Static Metrics

This crate supports staticly built metrics. You can use it with
[`lazy_static`](https://docs.rs/lazy_static/) to quickly build up and collect
some metrics.
`LazyLock` to quickly build up and collect some metrics.

```rust
use prometheus::{self, IntCounter, TextEncoder, Encoder};
use std::sync::LazyLock;

use lazy_static::lazy_static;
use prometheus::{self, IntCounter, TextEncoder, Encoder};
use prometheus::register_int_counter;

lazy_static! {
static ref HIGH_FIVE_COUNTER: IntCounter =
register_int_counter!("highfives", "Number of high fives received").unwrap();
}
static HIGH_FIVE_COUNTER: LazyLock<IntCounter> =
LazyLock::new(|| register_int_counter!("highfives", "Number of high fives received").unwrap());

HIGH_FIVE_COUNTER.inc();
assert_eq!(HIGH_FIVE_COUNTER.get(), 1);
Expand All @@ -72,17 +69,15 @@ By default, this registers with a default registry. To make a report, you can ca
[`Encoder`](trait.Encoder.html) and report to Promethus.

```
use std::sync::LazyLock;
# 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 =
# register_int_counter!("highfives", "Number of high fives received").unwrap();
# }
# static HIGH_FIVE_COUNTER: LazyLock<IntCounter> = LazyLock::new(||
# register_int_counter!("highfives", "Number of high fives received").unwrap());
# HIGH_FIVE_COUNTER.inc();

let mut buffer = Vec::new();
Expand Down
24 changes: 8 additions & 16 deletions src/process_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! This module only supports **Linux** platform.

use lazy_static::lazy_static;
use std::sync::LazyLock;

use crate::counter::IntCounter;
use crate::desc::Desc;
Expand Down Expand Up @@ -190,21 +190,13 @@ impl Collector for ProcessCollector {
}
}

lazy_static! {
// getconf CLK_TCK
static ref CLK_TCK: i64 = {
unsafe {
libc::sysconf(libc::_SC_CLK_TCK)
}.into()
};

// getconf PAGESIZE
static ref PAGESIZE: i64 = {
unsafe {
libc::sysconf(libc::_SC_PAGESIZE)
}.into()
};
}
// getconf CLK_TCK
static CLK_TCK: LazyLock<i64> =
LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_CLK_TCK) }.into());

// getconf PAGESIZE
static PAGESIZE: LazyLock<i64> =
LazyLock::new(|| unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.into());

#[cfg(test)]
mod tests {
Expand Down
11 changes: 5 additions & 6 deletions src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
use std::collections::HashMap;
use std::hash::BuildHasher;
use std::str::{self, FromStr};
use std::sync::LazyLock;
use std::time::Duration;

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 All @@ -20,12 +19,12 @@ use crate::registry::Registry;

const REQWEST_TIMEOUT_SEC: Duration = Duration::from_secs(10);

lazy_static! {
static ref HTTP_CLIENT: Client = Client::builder()
static HTTP_CLIENT: LazyLock<Client> = LazyLock::new(|| {
Client::builder()
.timeout(REQWEST_TIMEOUT_SEC)
.build()
.unwrap();
}
.unwrap()
});

/// `BasicAuthentication` holder for supporting `push` to Pushgateway endpoints
/// using Basic access authentication.
Expand Down
17 changes: 7 additions & 10 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::btree_map::Entry as BEntry;
use std::collections::hash_map::Entry as HEntry;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;
use std::sync::LazyLock;

use parking_lot::RwLock;

Expand All @@ -13,7 +14,6 @@ use crate::metrics::Collector;
use crate::proto;

use cfg_if::cfg_if;
use lazy_static::lazy_static;

#[derive(Default)]
struct RegistryCore {
Expand Down Expand Up @@ -293,20 +293,17 @@ cfg_if! {
}

// Default registry for rust-prometheus.
lazy_static! {
static ref DEFAULT_REGISTRY: Registry = {
let reg = Registry::default();
static DEFAULT_REGISTRY: LazyLock<Registry> = LazyLock::new(|| {
let reg = Registry::default();

// Register a default process collector.
register_default_process_collector(&reg).unwrap();
// Register a default process collector.
register_default_process_collector(&reg).unwrap();

reg
};
}
reg
});

/// Default registry (global static).
pub fn default_registry() -> &'static Registry {
lazy_static::initialize(&DEFAULT_REGISTRY);
&DEFAULT_REGISTRY
}

Expand Down
11 changes: 3 additions & 8 deletions src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::LazyLock;
use std::thread;
use std::time::{Duration, Instant};

use lazy_static::lazy_static;

/// Milliseconds since ANCHOR.
static RECENT: AtomicU64 = AtomicU64::new(0);
lazy_static! {
static ref ANCHOR: Instant = Instant::now();
}
static ANCHOR: LazyLock<Instant> = LazyLock::new(Instant::now);

/// Convert a duration to millisecond.
#[inline]
Expand Down Expand Up @@ -39,9 +36,7 @@ pub fn recent_millis() -> u64 {
RECENT.load(Ordering::Relaxed)
}

lazy_static! {
static ref UPDATER_IS_RUNNING: AtomicBool = AtomicBool::new(false);
}
static UPDATER_IS_RUNNING: LazyLock<AtomicBool> = LazyLock::new(AtomicBool::default);

const CHECK_UPDATE_INTERVAL: Duration = Duration::from_millis(200);

Expand Down
1 change: 0 additions & 1 deletion static-metric/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ proc-macro = true
syn = { version = "2.0", features = ["full", "extra-traits"] }
proc-macro2 = "1.0"
quote = "1.0"
lazy_static = "1.4"

[dev-dependencies]
criterion = "0.5"
Expand Down
Loading