-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_usage.rs
More file actions
51 lines (43 loc) · 2.22 KB
/
basic_usage.rs
File metadata and controls
51 lines (43 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use prometric::{Counter, Gauge, Histogram, Summary};
use prometric_derive::metrics;
// The `scope` attribute is used to set the prefix for the metric names in this struct.
#[metrics(scope = "app")]
struct AppMetrics {
/// The total number of HTTP requests.
#[metric(rename = "http_requests_total", labels = ["method", "path"])]
http_requests: Counter,
// For histograms, the `buckets` attribute is optional. It will default to
// [prometheus::DEFAULT_BUCKETS] if not provided. `buckets` can also be an expression that
// evaluates into a `Vec<f64>`.
/// The duration of HTTP requests.
#[metric(labels = ["method", "path"], buckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0])]
http_requests_duration: Histogram,
/// The size fo HTTP requests.
#[metric(labels = ["method", "path"], quantiles = [0.0, 0.5, 0.9, 0.95, 0.99, 0.999, 1.0])]
http_request_sizes: Summary,
/// This doc comment will be overwritten by the `help` attribute.
#[metric(rename = "current_active_users", labels = ["service"], help = "The current number of active users.")]
current_users: Gauge,
/// The balance of the account, in dollars. Uses a floating point number.
#[metric(rename = "account_balance", labels = ["account_id"])]
account_balance: Gauge<f64>,
/// The total number of errors.
#[metric]
errors: Counter,
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
// Build the metrics struct with static labels, which will initialize and register the metrics
// with the default registry. A custom registry can be used by passing it to the builder
// using `with_registry`.
let metrics =
AppMetrics::builder().with_label("host", "localhost").with_label("port", "8080").build();
// Metric fields each get an accessor method generated, which can be used to interact with the
// metric. The arguments to the accessor method are the labels for the metric.
metrics.http_requests("GET", "/").inc();
metrics.http_requests_duration("GET", "/").observe(1.0);
metrics.http_request_sizes("GET", "/").observe(12345);
metrics.current_users("service-1").set(10);
metrics.account_balance("1234567890").set(-12.2);
metrics.errors().inc();
}