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
12 changes: 5 additions & 7 deletions examples/example_custom_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
//! This examples shows how to use multiple and custom registries,
//! and how to perform registration across function boundaries.

use std::collections::HashMap;
use std::{collections::HashMap, 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
21 changes: 11 additions & 10 deletions examples/example_int_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::sync::LazyLock;

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();
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
16 changes: 9 additions & 7 deletions examples/example_push.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
// 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};

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
19 changes: 8 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ 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
[`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) to quickly build up and collect
some metrics.

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

use lazy_static::lazy_static;
use std::sync::LazyLock;
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 @@ -75,14 +74,12 @@ By default, this registers with a default registry. To make a report, you can ca
# use prometheus::IntCounter;
use prometheus::{self, TextEncoder, Encoder};

use lazy_static::lazy_static;
use std::sync::LazyLock;
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
28 changes: 13 additions & 15 deletions static-metric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ For heavier scenario that a global shared static-metric might not be effecient e
```rust
use prometheus::*;

use lazy_static:lazy_static;
use std::sync::LazyLock;
use prometheus_static_metric::{auto_flush_from, make_auto_flush_static_metric};

make_auto_flush_static_metric! {
Expand All @@ -118,20 +118,18 @@ make_auto_flush_static_metric! {
}
}

lazy_static! {
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
register_int_counter_vec ! (
"http_requests_total",
"Number of HTTP requests.",
& ["product", "method", "version"] // it doesn't matter for the label order
).unwrap();
}

lazy_static! {
// You can also use default flush duration which is 1 second.
// pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs);
pub static ref TLS_HTTP_COUNTER: Lhrs = auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1));
}
pub static HTTP_COUNTER_VEC: LazyLock<CounterVec> = LazyLock::new(|| {
register_counter_vec!(
"http_requests_total",
"Number of HTTP requests.",
& ["product", "method", "version"] // it doesn't matter for the label order
)
.unwrap()
});

// You can also use default flush duration which is 1 second.
// pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock;;new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs));
pub static TLS_HTTP_COUNTER: LazyLock<Lhrs> = LazyLock::new(|| auto_flush_from!(HTTP_COUNTER_VEC, Lhrs, std::time::Duration::from_secs(1)));

fn main() {
TLS_HTTP_COUNTER.foo.post.http1.inc();
Expand Down
23 changes: 12 additions & 11 deletions static-metric/examples/advanced.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::sync::LazyLock;

use prometheus::IntCounterVec;

use lazy_static::lazy_static;
use prometheus::register_int_counter_vec;
use prometheus_static_metric::make_static_metric;

Expand All @@ -25,17 +26,17 @@ make_static_metric! {
}
}

lazy_static! {
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
register_int_counter_vec!(
"http_requests_total",
"Number of HTTP requests.",
&["product", "method", "version"] // it doesn't matter for the label order
).unwrap();
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!(
"http_requests_total",
"Number of HTTP requests.",
&["product", "method", "version"] // it doesn't matter for the label order
)
.unwrap()
});

pub static ref HTTP_COUNTER: HttpRequestStatistics = HttpRequestStatistics
::from(&HTTP_COUNTER_VEC);
}
pub static HTTP_COUNTER: LazyLock<HttpRequestStatistics> =
LazyLock::new(|| HttpRequestStatistics::from(&HTTP_COUNTER_VEC));

/// This example demonstrates the usage of:
/// 1. using alternative metric types (i.e. IntCounter)
Expand Down
19 changes: 9 additions & 10 deletions static-metric/examples/local.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::cell::Cell;
use std::{cell::Cell, sync::LazyLock};

use prometheus::*;

use lazy_static::lazy_static;
use prometheus_static_metric::make_static_metric;

make_static_metric! {
Expand All @@ -28,14 +27,14 @@ make_static_metric! {
}
}

lazy_static! {
pub static ref HTTP_COUNTER_VEC: IntCounterVec =
register_int_counter_vec!(
"http_requests_total",
"Number of HTTP requests.",
&["product", "method", "version"] // it doesn't matter for the label order
).unwrap();
}
pub static HTTP_COUNTER_VEC: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!(
"http_requests_total",
"Number of HTTP requests.",
&["product", "method", "version"] // it doesn't matter for the label order
)
.unwrap()
});

thread_local! {
static THREAD_LAST_TICK_TIME: Cell<u64> = Cell::new(timer::now_millis());
Expand Down
Loading
Loading