This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: instrument cache hit and misses #208
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
964c529
feat: instrument cache hit and misses
Dexterp37 020b21c
f missing docs
Dexterp37 913132d
f report both storage and pointers len
Dexterp37 265ba55
f remove stale doc entry
Dexterp37 9c82b05
f change cache memory size metrics to gague
mythmon 8082204
f finish counter to gauge change
mythmon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
|
||
use anyhow::anyhow; | ||
use async_trait::async_trait; | ||
use cadence::{Histogrammed, StatsdClient}; | ||
use cadence::{Counted, CountedExt, Histogrammed, StatsdClient}; | ||
use deduped_dashmap::{ControlFlow, DedupedMap}; | ||
use lazy_static::lazy_static; | ||
use merino_settings::providers::MemoryCacheConfig; | ||
|
@@ -108,6 +108,7 @@ impl Suggester { | |
let items = Arc::new(DedupedMap::new()); | ||
|
||
{ | ||
let metrics = metrics_client.clone(); | ||
let task_items = items.clone(); | ||
let task_interval = config.cleanup_interval; | ||
tokio::spawn(async move { | ||
|
@@ -118,7 +119,7 @@ impl Suggester { | |
timer.tick().await; | ||
loop { | ||
timer.tick().await; | ||
Self::remove_expired_entries(&task_items); | ||
Self::remove_expired_entries(&task_items, &metrics); | ||
} | ||
}); | ||
} | ||
|
@@ -138,6 +139,7 @@ impl Suggester { | |
#[tracing::instrument(level = "debug", skip(items))] | ||
fn remove_expired_entries<K: Eq + Hash + Debug>( | ||
items: &Arc<DedupedMap<K, Instant, Vec<Suggestion>>>, | ||
metrics_client: &StatsdClient, | ||
) { | ||
let start = Instant::now(); | ||
let count_before_storage = items.len_storage(); | ||
|
@@ -174,6 +176,13 @@ impl Suggester { | |
?removed_storage, | ||
"finished removing expired entries from cache" | ||
); | ||
|
||
metrics_client | ||
.count("cache.memory.storage_len", items.len_storage() as i64) | ||
.ok(); | ||
metrics_client | ||
.count("cache.memory.pointers_len", items.len_pointers() as i64) | ||
.ok(); | ||
} | ||
} | ||
|
||
|
@@ -207,6 +216,7 @@ impl SuggestionProvider for Suggester { | |
} | ||
Some((expiration, suggestions)) => { | ||
tracing::debug!("cache hit"); | ||
self.metrics_client.incr("cache.memory.hit").ok(); | ||
rv = Some(SuggestionResponse { | ||
cache_status: CacheStatus::Hit, | ||
cache_ttl: Some(expiration - now), | ||
|
@@ -215,6 +225,7 @@ impl SuggestionProvider for Suggester { | |
} | ||
None => { | ||
tracing::debug!("cache miss"); | ||
self.metrics_client.incr("cache.memory.miss").ok(); | ||
} | ||
} | ||
|
||
|
@@ -271,6 +282,7 @@ impl SuggestionProvider for Suggester { | |
#[cfg(test)] | ||
mod tests { | ||
use super::{Suggester, LOCK_TABLE}; | ||
use cadence::{SpyMetricSink, StatsdClient}; | ||
use deduped_dashmap::DedupedMap; | ||
use fake::{Fake, Faker}; | ||
use merino_suggest::Suggestion; | ||
|
@@ -299,12 +311,26 @@ mod tests { | |
assert!(cache.contains_key(&"current".to_owned())); | ||
assert!(cache.contains_key(&"expired".to_owned())); | ||
|
||
Suggester::remove_expired_entries(&cache); | ||
// Provide an inspectable metrics sink to validate the collected data. | ||
let (rx, sink) = SpyMetricSink::new(); | ||
let metrics_client = StatsdClient::from_sink("merino-test", sink); | ||
|
||
Suggester::remove_expired_entries(&cache, &metrics_client); | ||
|
||
assert_eq!(cache.len_storage(), 1); | ||
assert_eq!(cache.len_pointers(), 1); | ||
assert!(cache.contains_key(&"current".to_owned())); | ||
assert!(!cache.contains_key(&"expired".to_owned())); | ||
|
||
// Verify the reported metric. | ||
assert_eq!(rx.len(), 2); | ||
let collected_data: Vec<String> = rx | ||
.iter() | ||
.take(2) | ||
.map(|x| String::from_utf8(x).unwrap()) | ||
.collect(); | ||
assert!(collected_data.contains(&"merino-test.cache.memory.storage_len:1|c".to_string())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs updating per the gauge type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. thanks. |
||
assert!(collected_data.contains(&"merino-test.cache.memory.pointers_len:1|c".to_string())); | ||
} | ||
|
||
#[test] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this the first time, but this needs to be
metrics_client.gauge
, not.count
. That's becausecount
is equivalent toincr
, but with a larger step. (more specifically,incr
iscount
with n = 1). With count we'd end up with a graph of the integral of the cache size overtime, which definitely isn't what we want.