Skip to content

Commit

Permalink
增加监控度量模块,增加CounterManager #64
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Jun 18, 2024
1 parent 9359405 commit 632fb7b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod starter;
pub mod user;
pub mod utils;
pub mod web_config;
pub mod metrics;

pub use inner_mem_cache::TimeoutSet;

Expand Down
19 changes: 19 additions & 0 deletions src/metrics/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::collections::HashMap;
use crate::metrics::metrics_type::MetricsType;

pub struct CounterManager{
date_map : HashMap<MetricsType,u64>,
}

pub struct GaugeManager{
date_map : HashMap<MetricsType,u64>,
}

pub struct HistogramManager {

}

pub struct MetricsManager{
//counter: CounterManager,

}
60 changes: 60 additions & 0 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::collections::HashMap;
use crate::metrics::metrics_type::MetricsType;


#[derive(Default,Debug)]
pub struct CounterItem(u64);

impl CounterItem {
pub fn increment(&mut self, value: u64) {
self.0 += value;
}

pub fn absolute(&mut self, value: u64) {
self.0 = value;
}

pub fn value(&self) -> u64 {
self.0
}
}

impl From<CounterItem> for u64 {
fn from(value: CounterItem) -> Self {
value.0
}
}

impl From<u64> for CounterItem {
fn from(value: u64) -> Self {
Self(value)
}
}

type Key = MetricsType;

#[derive(Default,Debug)]
pub struct CounterManager{
date_map : HashMap<Key,CounterItem>,
}

impl CounterManager {
pub fn increment(&mut self,key:Key,value:u64) {
if let Some(item) = self.date_map.get_mut(&key) {
item.increment(value);
}
else {
self.date_map.insert(key,value.into());
}
}

pub fn absolute(&mut self,key:Key,value:u64) {
if let Some(item) = self.date_map.get_mut(&key) {
item.absolute(value);
}
else {
self.date_map.insert(key,value.into());
}
}
}

6 changes: 6 additions & 0 deletions src/metrics/gauge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::collections::HashMap;
use crate::metrics::metrics_type::MetricsType;

pub struct GaugeManager{
date_map : HashMap<MetricsType,u64>,
}
5 changes: 5 additions & 0 deletions src/metrics/histogram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


pub struct HistogramManager {

}
13 changes: 13 additions & 0 deletions src/metrics/metrics_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub enum MetricsType {
None,
ConfigDataSize,
ConfigListenerSize,
}

impl Default for MetricsType {
fn default() -> Self {
Self::None
}
}
5 changes: 5 additions & 0 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod core;
pub mod metrics_type;
pub mod counter;
mod gauge;
mod histogram;

0 comments on commit 632fb7b

Please sign in to comment.