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

Commit

Permalink
feat: Add local server hostname to tags (#234)
Browse files Browse the repository at this point in the history
* feat: Add local server hostname to tags
* move `srv.hostname` to metric only tag.

Closes #200 

Co-authored-by: Raphael Pierzina <raphael@hackebrot.de>
  • Loading branch information
jrconlin and hackebrot authored Oct 7, 2021
1 parent c761902 commit ba04b24
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cloud-storage = { git = "https://github.com/mozilla-services/cloud-storage-rs",
config = "0.11"
dashmap = "4.0.2"
futures = "0.3"
gethostname = "0.2.1"
hex = "0.4"
hostname = "0.3"
image = "0.23"
Expand Down
9 changes: 9 additions & 0 deletions src/adm/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ pub async fn get_tiles(
.map_err(|e| HandlerError::internal(&e.to_string()))?;
let adm_url = adm_url.as_str();

// To reduce cardinality, only add this tag when fetching data from
// the partner. (This tag is only for metrics.)
tags.add_metric(
"srv.hostname",
&gethostname::gethostname()
.into_string()
.unwrap_or_else(|_| "Unkwnown".to_owned()),
);

info!("adm::get_tiles GET {}", adm_url);
metrics.incr("tiles.adm.request");
let response: AdmTileResponse = if state.settings.test_mode {
Expand Down
18 changes: 10 additions & 8 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ impl Metrics {
tagged = tagged.with_tag(key, val.as_ref());
}
}
// 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())
}
}
// Include any "hard coded" tags.
// incr = incr.with_tag("version", env!("CARGO_PKG_VERSION"));
match tagged.try_send() {
Expand Down Expand Up @@ -235,7 +241,6 @@ mod tests {
fn test_tags() {
use actix_web::dev::RequestHead;
use actix_web::http::{header, uri::Uri};
use std::collections::HashMap;

let mut rh = RequestHead::default();
let settings = Settings::default();
Expand All @@ -250,13 +255,10 @@ mod tests {

let tags = Tags::from_head(&rh, &settings);

let mut result = HashMap::<String, String>::new();
result.insert("ua.os.ver".to_owned(), "NT 10.0".to_owned());
result.insert("ua.os.family".to_owned(), "Windows".to_owned());
result.insert("ua.browser.ver".to_owned(), "72.0".to_owned());
result.insert("uri.method".to_owned(), "GET".to_owned());

assert_eq!(tags.tags, result)
assert_eq!(tags.tags.get("ua.os.ver"), Some(&"NT 10.0".to_owned()));
assert_eq!(tags.tags.get("ua.os.family"), Some(&"Windows".to_owned()));
assert_eq!(tags.tags.get("ua.browser.ver"), Some(&"72.0".to_owned()));
assert_eq!(tags.tags.get("uri.method"), Some(&"GET".to_owned()));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/server/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TilesCache {
let metrics = Metrics::from(&metrics);
actix_rt::spawn(async move {
loop {
tiles_cache_periodic_reporter(&cache, &metrics).await;
tiles_cache_garbage_collect(&cache, &metrics).await;
actix_rt::time::delay_for(interval).await;
}
});
Expand Down Expand Up @@ -135,8 +135,8 @@ impl TilesContent {
}
}

async fn tiles_cache_periodic_reporter(cache: &TilesCache, metrics: &Metrics) {
trace!("tiles_cache_periodic_reporter");
async fn tiles_cache_garbage_collect(cache: &TilesCache, metrics: &Metrics) {
trace!("tiles_cache_garbage_collect");
// calculate the size and GC (for seldomly used Tiles) while we're at it
let mut cache_count = 0;
let mut cache_size = 0;
Expand Down
24 changes: 23 additions & 1 deletion src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,20 @@ pub fn parse_user_agent(agent: &str) -> (WootheeResult<'_>, &str) {
/// `extra` are not searchable, but may not be sent to [crate::metrics::Metrics].
#[derive(Clone, Debug)]
pub struct Tags {
// All tags (both metric and sentry)
pub tags: HashMap<String, String>,
// Sentry only "extra" data.
pub extra: HashMap<String, String>,
// metric only supplemental tags.
pub metric: HashMap<String, String>,
}

impl Default for Tags {
fn default() -> Tags {
Tags {
tags: HashMap::new(),
extra: HashMap::new(),
metric: HashMap::new(),
}
}
}
Expand Down Expand Up @@ -142,7 +147,11 @@ impl Tags {
// `uri.path` causes too much cardinality for influx but keep it in
// extra for sentry
extra.insert("uri.path".to_owned(), req_head.uri.to_string());
Tags { tags, extra }
Tags {
tags,
extra,
metric: HashMap::new(),
}
}
}

Expand All @@ -166,6 +175,7 @@ impl Tags {
Self {
tags: HashMap::new(),
extra,
metric: HashMap::new(),
}
}
}
Expand All @@ -189,6 +199,7 @@ impl Tags {
Tags {
tags,
extra: HashMap::new(),
metric: HashMap::new(),
}
}

Expand All @@ -213,6 +224,16 @@ impl Tags {
}
}

/// Add an element to the "extra" data.
///
/// Extra data is non-key storage used by sentry. It is not
/// distributed to metrics.
pub fn add_metric(&mut self, key: &str, value: &str) {
if !value.is_empty() {
self.metric.insert(key.to_owned(), value.to_owned());
}
}

/// Get a tag value.
pub fn get(&self, label: &str) -> String {
let none = "None".to_owned();
Expand All @@ -225,6 +246,7 @@ impl Tags {
pub fn extend(&mut self, tags: Self) {
self.tags.extend(tags.tags);
self.extra.extend(tags.extra);
self.metric.extend(tags.metric);
}

/// Convert tag hash to a Binary Tree map (used by cadence and sentry)
Expand Down

0 comments on commit ba04b24

Please sign in to comment.