Skip to main content

risingwave_common_metrics/
lib.rs

1// Copyright 2024 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![feature(type_alias_impl_trait)]
16#![feature(impl_trait_in_assoc_type)]
17#![feature(register_tool)]
18#![feature(trait_alias)]
19#![register_tool(rw)]
20
21use hytra::TrAdder;
22use prometheus::core::{Atomic, GenericGauge};
23use prometheus::proto::Metric;
24
25mod error_metrics;
26mod gauge_ext;
27mod guarded_metrics;
28mod metrics;
29pub mod monitor;
30mod relabeled_metric;
31
32pub use error_metrics::*;
33pub use gauge_ext::*;
34pub use guarded_metrics::*;
35pub use metrics::*;
36pub use relabeled_metric::*;
37
38#[derive(Debug)]
39pub struct TrAdderAtomic(TrAdder<i64>);
40
41impl Atomic for TrAdderAtomic {
42    type T = i64;
43
44    fn new(val: i64) -> Self {
45        let v = TrAdderAtomic(TrAdder::new());
46        v.0.inc(val);
47        v
48    }
49
50    fn set(&self, _val: i64) {
51        panic!("TrAdderAtomic doesn't support set operation.")
52    }
53
54    fn get(&self) -> i64 {
55        self.0.get()
56    }
57
58    fn inc_by(&self, delta: i64) {
59        self.0.inc(delta)
60    }
61
62    fn dec_by(&self, delta: i64) {
63        self.0.inc(-delta)
64    }
65}
66
67pub type TrAdderGauge = GenericGauge<TrAdderAtomic>;
68
69#[derive(Debug, Default, Clone, Copy, serde::Serialize, serde::Deserialize)]
70pub enum MetricLevel {
71    #[default]
72    Disabled = 0,
73    Critical = 1,
74    Info = 2,
75    Debug = 3,
76}
77
78impl clap::ValueEnum for MetricLevel {
79    fn value_variants<'a>() -> &'a [Self] {
80        &[Self::Disabled, Self::Critical, Self::Info, Self::Debug]
81    }
82
83    fn to_possible_value<'a>(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
84        match self {
85            Self::Disabled => Some(clap::builder::PossibleValue::new("disabled").alias("0")),
86            Self::Critical => Some(clap::builder::PossibleValue::new("critical")),
87            Self::Info => Some(clap::builder::PossibleValue::new("info").alias("1")),
88            Self::Debug => Some(clap::builder::PossibleValue::new("debug")),
89        }
90    }
91}
92
93impl PartialEq<Self> for MetricLevel {
94    fn eq(&self, other: &Self) -> bool {
95        (*self as u8).eq(&(*other as u8))
96    }
97}
98
99impl PartialOrd for MetricLevel {
100    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
101        (*self as u8).partial_cmp(&(*other as u8))
102    }
103}
104
105pub fn get_label<T: std::str::FromStr>(metric: &Metric, label: &str) -> Option<T> {
106    metric
107        .get_label()
108        .iter()
109        .find(|lp| lp.name() == label)
110        .and_then(|lp| lp.value().parse::<T>().ok())
111}
112
113// Must ensure the label exists and can be parsed into `T`
114pub fn get_label_infallible<T: std::str::FromStr>(metric: &Metric, label: &str) -> T {
115    get_label(metric, label).expect("label not found or can't be parsed")
116}