mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 06:48:37 +08:00
Memory allocation improvements for metrics collector .
This commit is contained in:
31
src/utils/fordebug.rs
Normal file
31
src/utils/fordebug.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::alloc::{GlobalAlloc, Layout, System};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
pub struct CountingAllocator;
|
||||
|
||||
pub static ALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
pub static DEALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
pub static ALLOC_BYTES: AtomicUsize = AtomicUsize::new(0);
|
||||
#[allow(dead_code)]
|
||||
unsafe impl GlobalAlloc for CountingAllocator {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
ALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
|
||||
ALLOC_BYTES.fetch_add(layout.size(), Ordering::Relaxed);
|
||||
System.alloc(layout)
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
DEALLOC_COUNT.fetch_add(1, Ordering::Relaxed);
|
||||
System.dealloc(ptr, layout)
|
||||
}
|
||||
}
|
||||
|
||||
// Uncomment following lines and comment allocator in main.rs
|
||||
// #[global_allocator]
|
||||
// pub static A: CountingAllocator = CountingAllocator;
|
||||
#[allow(dead_code)]
|
||||
fn for_example() {
|
||||
let before = crate::utils::fordebug::ALLOC_COUNT.load(Ordering::Relaxed);
|
||||
let after = crate::utils::fordebug::ALLOC_COUNT.load(Ordering::Relaxed);
|
||||
println!("Allocations : {}", after - before);
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use dashmap::DashMap;
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
use pingora::prelude::sleep;
|
||||
use rand::Rng;
|
||||
use rand::RngExt;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
@@ -19,10 +19,6 @@ use std::time::Duration;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
// #[derive(Debug, Deserialize)]
|
||||
// pub struct KubeEndpointsList {
|
||||
// pub items: Vec<KubeEndpoints>,
|
||||
// }
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct KubeEndpoints {
|
||||
pub subsets: Option<Vec<KubeSubset>>,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use http::method::Method;
|
||||
use http::StatusCode;
|
||||
use pingora_http::Version;
|
||||
use prometheus::{register_histogram, register_int_counter, register_int_counter_vec, Histogram, IntCounter, IntCounterVec};
|
||||
use std::sync::Arc;
|
||||
@@ -7,7 +8,7 @@ use std::time::Duration;
|
||||
pub struct MetricTypes {
|
||||
pub method: Method,
|
||||
pub upstream: Arc<str>,
|
||||
pub code: String,
|
||||
pub code: Option<StatusCode>,
|
||||
pub latency: Duration,
|
||||
pub version: Version,
|
||||
}
|
||||
@@ -65,7 +66,7 @@ pub fn calc_metrics(metric_types: &MetricTypes) {
|
||||
_ => "Unknown",
|
||||
};
|
||||
REQUESTS_BY_VERSION.with_label_values(&[&version_str]).inc();
|
||||
RESPONSE_CODES.with_label_values(&[&metric_types.code]).inc();
|
||||
RESPONSE_CODES.with_label_values(&[metric_types.code.unwrap_or(http::StatusCode::GONE).as_str()]).inc();
|
||||
REQUESTS_BY_METHOD.with_label_values(&[&metric_types.method]).inc();
|
||||
REQUESTS_BY_UPSTREAM.with_label_values(&[metric_types.upstream.as_ref()]).inc();
|
||||
RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64());
|
||||
|
||||
Reference in New Issue
Block a user