move the user-agent out of the context in every request

This commit is contained in:
Tamo 2021-10-28 12:52:53 +02:00 committed by marin postma
parent c5164c01c0
commit 7c39fab453
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9

View File

@ -297,25 +297,27 @@ impl SegmentAnalytics {
} }
impl super::Analytics for SegmentAnalytics { impl super::Analytics for SegmentAnalytics {
fn publish(&'static self, event_name: String, send: Value, request: Option<&HttpRequest>) { fn publish(&'static self, event_name: String, mut send: Value, request: Option<&HttpRequest>) {
let content_type = request let user_agent = request
.map(|req| req.headers().get(USER_AGENT)) .map(|req| req.headers().get(USER_AGENT))
.flatten() .flatten()
.map(|header| header.to_str().unwrap_or("unknown").to_string()); .map(|header| header.to_str().unwrap_or("unknown"))
.map(|s| s.split(';').map(str::trim).collect::<Vec<&str>>());
send["user-agent"] = json!(user_agent);
tokio::spawn(async move { tokio::spawn(async move {
let _ = self let _ = self
.batcher .batcher
.lock() .lock()
.await .await
.push(Track { .push(Track {
user: self.user.clone(), user: self.user.clone(),
event: event_name.clone(), event: event_name.clone(),
context: content_type.map(|user_agent| json!({ "user-agent": user_agent.split(";").map(str::trim).collect::<Vec<&str>>() })), properties: send,
properties: send, ..Default::default()
..Default::default() })
}) .await;
.await;
}); });
} }
@ -419,11 +421,11 @@ impl SearchBatcher {
if self.total_received == 0 { if self.total_received == 0 {
None None
} else { } else {
let context = Some(json!({ "user-agent": self.user_agents}));
let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.; let percentile_99th = 0.99 * (self.total_succeeded as f64 - 1.) + 1.;
self.time_spent.drain(percentile_99th as usize..); self.time_spent.drain(percentile_99th as usize..);
let properties = json!({ let properties = json!({
"user-agent": self.user_agents,
"requests": { "requests": {
"99th_response_time": format!("{:.2}", self.time_spent.iter().sum::<usize>() as f64 / self.time_spent.len() as f64), "99th_response_time": format!("{:.2}", self.time_spent.iter().sum::<usize>() as f64 / self.time_spent.len() as f64),
"total_succeeded": self.total_succeeded, "total_succeeded": self.total_succeeded,
@ -451,7 +453,6 @@ impl SearchBatcher {
Some(Track { Some(Track {
user: user.clone(), user: user.clone(),
event: event_name.to_string(), event: event_name.to_string(),
context,
properties, properties,
..Default::default() ..Default::default()
}) })
@ -477,9 +478,8 @@ impl DocumentsBatcher {
if !self.updated { if !self.updated {
None None
} else { } else {
let context = Some(json!({ "user-agent": self.user_agents}));
let properties = json!({ let properties = json!({
"user-agent": self.user_agents,
"payload_type": self.content_types, "payload_type": self.content_types,
"primary_key": self.primary_keys, "primary_key": self.primary_keys,
"index_creation": self.index_creation, "index_creation": self.index_creation,
@ -488,7 +488,6 @@ impl DocumentsBatcher {
Some(Track { Some(Track {
user: user.clone(), user: user.clone(),
event: event_name.to_string(), event: event_name.to_string(),
context,
properties, properties,
..Default::default() ..Default::default()
}) })