-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defined metrics for suggest. I used labeled timing distributions, which is a relatively new metric that seems great for our needs. Using these metrics, we can track a separate timing distribution for each record type / provider. Updated `ingest` to return metrics. Added `query_with_metrics`, which returns metrics alongside the usual results. Added some classes to record metrics as the operations execute. Moved the top-level `fetch_suggestions` from db.rs to store.rs. It's now responsible for metrics, so I thought it fit better in store.
- Loading branch information
Showing
10 changed files
with
381 additions
and
73 deletions.
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
# This file defines the metrics that will be gathered for the Suggest | ||
# component. Changes to these metrics require data review. | ||
# | ||
# We can't record metrics from Rust directly. To work around that we: | ||
# - Define the metrics in application-services | ||
# - Define API calls in application-services that return the metrics | ||
# alongside the normal results. | ||
# - Record the metrics with Glean in the consumer code | ||
|
||
--- | ||
$schema: moz://mozilla.org/schemas/glean/metrics/2-0-0 | ||
|
||
suggest: | ||
ingest_time: | ||
type: labeled_timing_distribution | ||
description: Time for ingestion (excluding download time), labelled by record type | ||
time_unit: microsecond | ||
labels: | ||
- icon | ||
- data | ||
- amo-suggestions | ||
- pocket-suggestions | ||
- yelp-suggestions | ||
- mdn-suggestions | ||
- weather | ||
- configuration | ||
- amp-mobile-suggestions | ||
- fakespot-suggestions | ||
bugs: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1908397 | ||
data_reviews: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1911664 | ||
notification_emails: | ||
- [email protected] | ||
- [email protected] | ||
expires: "never" | ||
data_sensitivity: | ||
- technical | ||
|
||
ingest_download_time: | ||
type: labeled_timing_distribution | ||
description: Download time for ingestion, labelled by record type | ||
time_unit: microsecond | ||
labels: | ||
- icon | ||
- data | ||
- amo-suggestions | ||
- pocket-suggestions | ||
- yelp-suggestions | ||
- mdn-suggestions | ||
- weather | ||
- configuration | ||
- amp-mobile-suggestions | ||
- fakespot-suggestions | ||
bugs: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1908397 | ||
data_reviews: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1911664 | ||
notification_emails: | ||
- [email protected] | ||
- [email protected] | ||
expires: "never" | ||
data_sensitivity: | ||
- technical | ||
|
||
query_time: | ||
type: labeled_timing_distribution | ||
description: Time executing queries, labelled by provider type | ||
time_unit: microsecond | ||
labels: | ||
- amp | ||
- ampmobile | ||
- wikipedia | ||
- amo | ||
- yelp | ||
- mdn | ||
- weather | ||
- fakespot | ||
bugs: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1908397 | ||
data_reviews: | ||
- https://bugzilla.mozilla.org/show_bug.cgi?id=1911664 | ||
notification_emails: | ||
- [email protected] | ||
- [email protected] | ||
expires: "never" | ||
data_sensitivity: | ||
- technical |
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
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
use std::time::Instant; | ||
|
||
/// Single sample for a Glean labeled_timing_distribution | ||
pub struct LabeledTimingSample { | ||
pub label: String, | ||
pub value: u64, // time in microseconds | ||
} | ||
|
||
impl LabeledTimingSample { | ||
fn new(label: String, value: u64) -> Self { | ||
Self { label, value } | ||
} | ||
} | ||
|
||
/// Ingestion metrics | ||
/// | ||
/// These are recorded during [crate::Store::ingest] and returned to the consumer to record. | ||
#[derive(Default)] | ||
pub struct SuggestIngestionMetrics { | ||
pub ingestion_times: Vec<LabeledTimingSample>, | ||
pub download_times: Vec<LabeledTimingSample>, | ||
} | ||
|
||
impl SuggestIngestionMetrics { | ||
/// Wraps each iteration in `ingest` and records the time for it. | ||
/// | ||
/// Passes the closure a DownloadTimer. Use this to measure the times for all | ||
/// downloads that happen during the ingest | ||
pub fn measure_ingest<F, T>(&mut self, record_type: impl Into<String>, operation: F) -> T | ||
where | ||
F: FnOnce(&mut DownloadTimer) -> T, | ||
{ | ||
let timer = Instant::now(); | ||
let record_type = record_type.into(); | ||
let mut download_metrics = DownloadTimer::default(); | ||
let result = operation(&mut download_metrics); | ||
let elapsed = timer.elapsed().as_micros() as u64; | ||
self.ingestion_times.push(LabeledTimingSample::new( | ||
record_type.clone(), | ||
elapsed - download_metrics.total_time, | ||
)); | ||
self.download_times.push(LabeledTimingSample::new( | ||
record_type, | ||
download_metrics.total_time, | ||
)); | ||
result | ||
} | ||
} | ||
|
||
/// Records download times for a single loop in ingest | ||
/// | ||
/// [Self::measure_download] can be called multiple times. [DownloadTimer] will track the total | ||
/// time for all calls. | ||
#[derive(Default)] | ||
pub struct DownloadTimer { | ||
total_time: u64, | ||
} | ||
|
||
impl DownloadTimer { | ||
pub fn measure_download<F, T>(&mut self, operation: F) -> T | ||
where | ||
F: FnOnce() -> T, | ||
{ | ||
let timer = Instant::now(); | ||
let result = operation(); | ||
self.total_time += timer.elapsed().as_micros() as u64; | ||
result | ||
} | ||
} | ||
|
||
/// Query metrics | ||
/// | ||
/// These are recorded during [crate::Store::query] and returned to the consumer to record. | ||
#[derive(Default)] | ||
pub struct SuggestQueryMetrics { | ||
pub times: Vec<LabeledTimingSample>, | ||
} | ||
|
||
impl SuggestQueryMetrics { | ||
pub fn measure_query<F, T>(&mut self, provider: impl Into<String>, operation: F) -> T | ||
where | ||
F: FnOnce() -> T, | ||
{ | ||
let provider = provider.into(); | ||
let timer = Instant::now(); | ||
// Make sure the compiler doesn't reorder/inline in a way that invalidates this | ||
// measurement. | ||
let result = std::hint::black_box(operation()); | ||
let elapsed = timer.elapsed().as_micros() as u64; | ||
self.times.push(LabeledTimingSample::new(provider, elapsed)); | ||
result | ||
} | ||
} |
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
Oops, something went wrong.