snarkvm_metrics/lib.rs
1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#![forbid(unsafe_code)]
17
18const GAUGE_NAMES: [&str; 1] = [committee::TOTAL_STAKE];
19
20pub mod committee {
21 pub const TOTAL_STAKE: &str = "snarkvm_ledger_committee_total_stake";
22}
23
24/// Registers all snarkVM metrics.
25pub fn register_metrics() {
26 for name in GAUGE_NAMES {
27 register_gauge(name);
28 }
29}
30
31/******** Counter ********/
32
33/// Registers a counter with the given name.
34pub fn register_counter(name: &'static str) {
35 let _counter = ::metrics::counter!(name);
36}
37
38/// Updates a counter with the given name to the given value.
39///
40/// Counters represent a single monotonic value, which means the value can only be incremented,
41/// not decremented, and always starts out with an initial value of zero.
42pub fn counter<V: Into<u64>>(name: &'static str, value: V) {
43 let counter = ::metrics::counter!(name);
44 counter.absolute(value.into());
45}
46
47/// Increments a counter with the given name by one.
48///
49/// Counters represent a single monotonic value, which means the value can only be incremented,
50/// not decremented, and always starts out with an initial value of zero.
51pub fn increment_counter(name: &'static str) {
52 let counter = ::metrics::counter!(name);
53 counter.increment(1);
54}
55
56/******** Gauge ********/
57
58/// Registers a gauge with the given name.
59pub fn register_gauge(name: &'static str) {
60 let _gauge = ::metrics::gauge!(name);
61}
62
63/// Updates a gauge with the given name to the given value.
64///
65/// Gauges represent a single value that can go up or down over time,
66/// and always starts out with an initial value of zero.
67pub fn gauge<V: Into<f64>>(name: &'static str, value: V) {
68 let gauge = ::metrics::gauge!(name);
69 gauge.set(value.into());
70}
71
72/// Increments a gauge with the given name by the given value.
73///
74/// Gauges represent a single value that can go up or down over time,
75/// and always starts out with an initial value of zero.
76pub fn increment_gauge<V: Into<f64>>(name: &'static str, value: V) {
77 let gauge = ::metrics::gauge!(name);
78 gauge.increment(value.into());
79}
80
81/// Decrements a gauge with the given name by the given value.
82///
83/// Gauges represent a single value that can go up or down over time,
84/// and always starts out with an initial value of zero.
85pub fn decrement_gauge<V: Into<f64>>(name: &'static str, value: V) {
86 let gauge = ::metrics::gauge!(name);
87 gauge.decrement(value.into());
88}
89
90/******** Histogram ********/
91
92/// Registers a histogram with the given name.
93pub fn register_histogram(name: &'static str) {
94 let _histogram = ::metrics::histogram!(name);
95}
96
97/// Updates a histogram with the given name to the given value.
98pub fn histogram<V: Into<f64>>(name: &'static str, value: V) {
99 let histogram = ::metrics::histogram!(name);
100 histogram.record(value.into());
101}
102
103pub fn histogram_label<V: Into<f64>>(name: &'static str, label_key: &'static str, label_value: String, value: V) {
104 ::metrics::histogram!(name, label_key => label_value).record(value.into());
105}