Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
feat: Add a metric to measure the tile count of adm response (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncloudioj authored May 2, 2023
1 parent fca2c47 commit 5bb21d2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/adm/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ pub async fn get_tiles(
})?
}
};
metrics.count_with_tags(
"tiles.adm.response.tiles_count",
response.tiles.len() as i64,
Some(tags),
);
// Keep this counter to facilitate the "zero" ad fill rate monitoring
if response.tiles.is_empty() {
warn!("adm::get_tiles empty response {}", adm_url);
metrics.incr_with_tags("filter.adm.empty_response", Some(tags));
Expand Down
34 changes: 12 additions & 22 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ impl Metrics {
/// Duration is calculated when this timer is dropped.
pub fn start_timer(&mut self, label: &str, tags: Option<Tags>) {
let mut mtags = self.tags.clone().unwrap_or_default();
if let Some(t) = tags {
mtags.extend(t)
}
Extend::extend(&mut mtags, tags.into_iter());

trace!("⌚ Starting timer... {:?}", &label; &mtags);
self.timer = Some(MetricTimer {
Expand All @@ -140,13 +138,10 @@ impl Metrics {
if let Some(client) = self.client.as_ref() {
let mut tagged = client.incr_with_tags(label);
let mut mtags = self.tags.clone().unwrap_or_default();
if let Some(tags) = tags {
mtags.extend(tags.clone());
}
for key in mtags.tags.keys().clone() {
if let Some(val) = mtags.tags.get(key) {
tagged = tagged.with_tag(key, val.as_ref());
}
Extend::extend(&mut mtags, tags.into_iter());

for (key, val) in mtags.tags.iter() {
tagged = tagged.with_tag(key, val);
}
// Include any "hard coded" tags.
// incr = incr.with_tag("version", env!("CARGO_PKG_VERSION"));
Expand All @@ -166,23 +161,18 @@ impl Metrics {
}

/// increment by count with [crate::tags::Tags] information
pub fn count_with_tags(&self, label: &str, count: i64, tags: Option<Tags>) {
pub fn count_with_tags(&self, label: &str, count: i64, tags: Option<&Tags>) {
if let Some(client) = self.client.as_ref() {
let mut tagged = client.count_with_tags(label, count);
let mut mtags = self.tags.clone().unwrap_or_default();
if let Some(tags) = tags {
mtags.extend(tags);
}
for key in mtags.tags.keys().clone() {
if let Some(val) = mtags.tags.get(key) {
tagged = tagged.with_tag(key, val.as_ref());
}
Extend::extend(&mut mtags, tags.into_iter());

for (key, val) in mtags.tags.iter() {
tagged = tagged.with_tag(key, val);
}
// mix in the metric only tags.
for key in mtags.metric.keys().clone() {
if let Some(val) = mtags.metric.get(key) {
tagged = tagged.with_tag(key, val.as_ref())
}
for (key, val) in mtags.metric.iter() {
tagged = tagged.with_tag(key, val);
}
// Include any "hard coded" tags.
// incr = incr.with_tag("version", env!("CARGO_PKG_VERSION"));
Expand Down
22 changes: 22 additions & 0 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,28 @@ impl Tags {
}
}

impl Extend<Tags> for Tags {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = Tags>,
{
for tag in iter {
self.extend(tag);
}
}
}

impl<'a> Extend<&'a Tags> for Tags {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = &'a Tags>,
{
for tag in iter {
self.extend(tag.clone());
}
}
}

impl FromRequest for Tags {
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
Expand Down
11 changes: 8 additions & 3 deletions src/web/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,13 @@ async fn metrics() {
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let prefixes = &["contile.tiles.get:1", "contile.tiles.adm.request:1"];
let prefixes = &[
"contile.tiles.get:1",
"contile.tiles.adm.request:1",
"contile.tiles.adm.response.tiles_count:4",
];
let metrics = find_metrics(&spy, prefixes);
assert_eq!(metrics.len(), 2);
assert_eq!(metrics.len(), 3);
let get_metric = &metrics[0];
assert!(get_metric.contains("ua.form_factor:desktop"));
assert!(get_metric.contains("ua.os.family:windows"));
Expand All @@ -932,7 +936,7 @@ async fn metrics() {
assert_eq!(resp.status(), StatusCode::OK);

let metrics = find_metrics(&spy, prefixes);
assert_eq!(metrics.len(), 2);
assert_eq!(metrics.len(), 3);
let get_metric = &metrics[0];
assert!(get_metric.contains("ua.form_factor:phone"));
assert!(get_metric.contains("ua.os.family:ios"));
Expand Down Expand Up @@ -1118,6 +1122,7 @@ async fn fallback_on_error() {
vec![
"contile.tiles.get:1",
"contile.tiles.adm.request:1",
"contile.tiles.adm.response.tiles_count:4",
"contile.tiles.get:1",
"contile.tiles.adm.request:1",
"contile.tiles.get.error:1"
Expand Down

0 comments on commit 5bb21d2

Please sign in to comment.