remove lock from metrics agent singleton (#31785)

This commit is contained in:
Andrew Fitzgerald 2023-05-24 12:31:08 -07:00 committed by GitHub
parent 6752019a80
commit 5953768cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 16 deletions

View File

@ -326,16 +326,12 @@ impl Drop for MetricsAgent {
} }
} }
fn get_singleton_agent() -> Arc<Mutex<MetricsAgent>> { fn get_singleton_agent() -> &'static MetricsAgent {
static INIT: Once = Once::new(); lazy_static! {
static mut AGENT: Option<Arc<Mutex<MetricsAgent>>> = None; static ref AGENT: MetricsAgent = MetricsAgent::default();
unsafe { };
INIT.call_once(|| AGENT = Some(Arc::new(Mutex::new(MetricsAgent::default()))));
match AGENT { &AGENT
Some(ref agent) => agent.clone(),
None => panic!("Failed to initialize metrics agent"),
}
}
} }
lazy_static! { lazy_static! {
@ -357,16 +353,14 @@ pub fn set_host_id(host_id: String) {
/// Submits a new point from any thread. Note that points are internally queued /// Submits a new point from any thread. Note that points are internally queued
/// and transmitted periodically in batches. /// and transmitted periodically in batches.
pub fn submit(point: DataPoint, level: log::Level) { pub fn submit(point: DataPoint, level: log::Level) {
let agent_mutex = get_singleton_agent(); let agent = get_singleton_agent();
let agent = agent_mutex.lock().unwrap();
agent.submit(point, level); agent.submit(point, level);
} }
/// Submits a new counter or updates an existing counter from any thread. Note that points are /// Submits a new counter or updates an existing counter from any thread. Note that points are
/// internally queued and transmitted periodically in batches. /// internally queued and transmitted periodically in batches.
pub(crate) fn submit_counter(point: CounterPoint, level: log::Level, bucket: u64) { pub(crate) fn submit_counter(point: CounterPoint, level: log::Level, bucket: u64) {
let agent_mutex = get_singleton_agent(); let agent = get_singleton_agent();
let agent = agent_mutex.lock().unwrap();
agent.submit_counter(point, level, bucket); agent.submit_counter(point, level, bucket);
} }
@ -432,8 +426,7 @@ pub fn query(q: &str) -> Result<String, String> {
/// Blocks until all pending points from previous calls to `submit` have been /// Blocks until all pending points from previous calls to `submit` have been
/// transmitted. /// transmitted.
pub fn flush() { pub fn flush() {
let agent_mutex = get_singleton_agent(); let agent = get_singleton_agent();
let agent = agent_mutex.lock().unwrap();
agent.flush(); agent.flush();
} }