This commit is contained in:
Jan-Erik Rediger 2019-04-10 16:03:30 +02:00
Родитель 1be687506d
Коммит 20901084a1
7 изменённых файлов: 84 добавлений и 4 удалений

Просмотреть файл

@ -1,4 +1,4 @@
use glean_core::metrics::{BooleanMetric, StringMetric};
use glean_core::metrics::{BooleanMetric, StringMetric, CounterMetric};
use glean_core::{storage, CommonMetricData, Lifetime, Glean};
use lazy_static::lazy_static;
@ -23,12 +23,23 @@ fn main() {
.. Default::default()
});
let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "calls".into(),
category: "local".into(),
send_in_pings: vec!["core".into(), "metrics".into()],
.. Default::default()
});
GLOBAL_METRIC.set(true);
local_metric.set("I can set this");
call_counter.add(1);
println!("Core Data:\n{}", storage::StorageManager.snapshot("core", true));
call_counter.add(2);
println!("Metrics Data:\n{}", storage::StorageManager.snapshot("metrics", true));
call_counter.add(3);
println!();
println!("Core Data 2:\n{}", storage::StorageManager.snapshot("core", true));
println!("Metrics Data 2:\n{}", storage::StorageManager.snapshot("metrics", true));

Просмотреть файл

@ -0,0 +1,17 @@
use crate::storage::CounterStorage;
use crate::CommonMetricData;
pub struct CounterMetric {
meta: CommonMetricData,
}
impl CounterMetric {
pub fn new(meta: CommonMetricData) -> Self {
Self { meta }
}
pub fn add(&self, value: u32) {
let mut lock = CounterStorage.write().unwrap();
lock.record(&self.meta, value)
}
}

Просмотреть файл

@ -1,5 +1,7 @@
mod boolean;
mod string;
mod counter;
pub use boolean::BooleanMetric;
pub use string::StringMetric;
pub use counter::CounterMetric;

Просмотреть файл

@ -19,7 +19,10 @@ impl BooleanStorageImpl {
pub fn record(&mut self, data: &CommonMetricData, value: bool) {
let name = data.fullname();
for ping_name in data.storage_names() {
let data_store = self.store.entry(ping_name.clone()).or_insert_with(HashMap::new);
let data_store = self
.store
.entry(ping_name.clone())
.or_insert_with(HashMap::new);
data_store.insert(name.clone(), value);
}
}

Просмотреть файл

@ -0,0 +1,40 @@
use std::collections::HashMap;
use serde_json::{json, Value as JsonValue};
use super::StorageDump;
use crate::CommonMetricData;
pub struct CounterStorageImpl {
store: HashMap<String, HashMap<String, u32>>,
}
impl CounterStorageImpl {
pub fn new() -> Self {
Self {
store: HashMap::new(),
}
}
pub fn record(&mut self, data: &CommonMetricData, value: u32) {
let name = data.fullname();
for ping_name in data.storage_names() {
let data_store = self
.store
.entry(ping_name.clone())
.or_insert_with(HashMap::new);
let entry = data_store.entry(name.clone()).or_insert(0);
*entry += value;
}
}
}
impl StorageDump for CounterStorageImpl {
fn snapshot(&mut self, store_name: &str, clear_store: bool) -> Option<JsonValue> {
let result = self.store.get(store_name).map(|store| json!(store));
if clear_store {
self.store.remove(store_name);
}
result
}
}

Просмотреть файл

@ -6,15 +6,19 @@ use lazy_static::lazy_static;
use serde_json::{json, Value as JsonValue};
mod boolean;
mod counter;
mod string;
use boolean::BooleanStorageImpl;
use counter::CounterStorageImpl;
use string::StringStorageImpl;
lazy_static! {
pub static ref BooleanStorage: RwLock<BooleanStorageImpl> =
RwLock::new(BooleanStorageImpl::new());
pub static ref StringStorage: RwLock<StringStorageImpl> = RwLock::new(StringStorageImpl::new());
pub static ref CounterStorage: RwLock<CounterStorageImpl> =
RwLock::new(CounterStorageImpl::new());
}
pub trait StorageDump {
@ -36,7 +40,7 @@ macro_rules! dump_storages {
impl StorageManager {
pub fn snapshot(&self, store_name: &str, clear_store: bool) -> String {
let data = dump_storages!(store_name, clear_store => ("bool", BooleanStorage), ("string", StringStorage),);
let data = dump_storages!(store_name, clear_store => ("bool", BooleanStorage), ("string", StringStorage), ("counter", CounterStorage), );
::serde_json::to_string_pretty(&data).unwrap()
}

Просмотреть файл

@ -19,7 +19,10 @@ impl StringStorageImpl {
pub fn record(&mut self, data: &CommonMetricData, value: String) {
let name = data.fullname();
for ping_name in data.storage_names() {
let data_store = self.store.entry(ping_name.clone()).or_insert_with(HashMap::new);
let data_store = self
.store
.entry(ping_name.clone())
.or_insert_with(HashMap::new);
data_store.insert(name.clone(), value.clone());
}
}