Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get all client hints #119

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 148 additions & 148 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
url = "2.5.2"
uuid = { version = "1.9.1", features = ["v4", "serde"] }
wasmtime = "25.0.2"
wasmtime-wasi = "25.0.2"
wasmtime = "26.0.0"
wasmtime-wasi = "26.0.0"

[features]
bundled = [
Expand Down
2 changes: 1 addition & 1 deletion public/edgee.v1.1.0.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/sdk.js

Large diffs are not rendered by default.

26 changes: 21 additions & 5 deletions src/proxy/compute/data_collection/components/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl From<payload::Page> for provider::PageData {
path: value.path.unwrap_or_default(),
search: value.search.unwrap_or_default(),
referrer: value.referrer.unwrap_or_default(),
properties: convert_dict(value.properties.unwrap_or_default()),
properties: convert_dict(value.properties),
}
}
}
Expand All @@ -55,7 +55,7 @@ impl From<payload::User> for provider::UserData {
user_id: value.user_id.unwrap_or_default(),
anonymous_id: value.anonymous_id.unwrap_or_default(),
edgee_id: value.edgee_id,
properties: convert_dict(value.properties.unwrap_or_default()),
properties: convert_dict(value.properties),
}
}
}
Expand All @@ -64,7 +64,7 @@ impl From<payload::Track> for provider::TrackData {
fn from(value: payload::Track) -> Self {
Self {
name: value.name.unwrap_or_default(),
properties: convert_dict(value.properties.unwrap_or_default()),
properties: convert_dict(value.properties),
}
}
}
Expand Down Expand Up @@ -105,6 +105,7 @@ impl From<payload::Client> for provider::Client {
user_agent_architecture: value.user_agent_architecture.unwrap_or_default(),
user_agent_bitness: value.user_agent_bitness.unwrap_or_default(),
user_agent_full_version_list: value.user_agent_full_version_list.unwrap_or_default(),
user_agent_version_list: value.user_agent_version_list.unwrap_or_default(),
user_agent_mobile: value.user_agent_mobile.unwrap_or_default(),
user_agent_model: value.user_agent_model.unwrap_or_default(),
os_name: value.os_name.unwrap_or_default(),
Expand Down Expand Up @@ -134,8 +135,23 @@ impl From<payload::Session> for provider::Session {
}
}

fn convert_dict<T: ToString>(dict: HashMap<String, T>) -> Vec<(String, String)> {
fn convert_dict(dict: Option<HashMap<String, serde_json::Value>>) -> Vec<(String, String)> {
use serde_json::Value;

let Some(dict) = dict else {
return Vec::new();
};

dict.into_iter()
.map(|(key, value)| (key, value.to_string()))
.filter(|(_, value)| !(value.is_array() || value.is_object()))
.map(|(k, v)| {
let value = if let Value::String(s) = v {
s
} else {
v.to_string()
};

(k, value)
})
.collect()
}
113 changes: 100 additions & 13 deletions src/proxy/compute/data_collection/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;
use std::time::Duration;

use http::{HeaderMap, HeaderName, HeaderValue};
use http::{header, HeaderMap, HeaderName, HeaderValue};
use json_pretty::PrettyFormatter;
use tracing::{error, info, span, Instrument, Level};

Expand Down Expand Up @@ -109,11 +109,7 @@ pub async fn send_data_collection(events: &Vec<Event>) -> anyhow::Result<()> {
for (key, value) in request.headers.iter() {
headers.insert(HeaderName::from_str(key)?, HeaderValue::from_str(value)?);
}
headers.insert(
HeaderName::from_str("x-forwarded-for")?,
anonymized_client_ip.clone(),
);
headers.insert(http::header::USER_AGENT, user_agent.clone());
insert_expected_headers(&mut headers, event, &anonymized_client_ip, &user_agent)?;

let client = client.clone();

Expand All @@ -130,7 +126,7 @@ pub async fn send_data_collection(events: &Vec<Event>) -> anyhow::Result<()> {
url = request.url,
body = request.body
);
debug_request(&request, cfg.name.as_str());
debug_request(&request, &headers, cfg.name.as_str());

// spawn a separated async thread
tokio::spawn(
Expand Down Expand Up @@ -184,7 +180,7 @@ pub async fn send_data_collection(events: &Vec<Event>) -> anyhow::Result<()> {
error!(step = "response", status = "500", err = err.to_string());
debug_response(
cfg.name.as_str(),
"500",
"502",
timer_start,
"".to_string(),
err.to_string(),
Expand All @@ -199,7 +195,98 @@ pub async fn send_data_collection(events: &Vec<Event>) -> anyhow::Result<()> {
Ok(())
}

fn debug_request(request: &provider::EdgeeRequest, component_name: &str) {
fn insert_expected_headers(
headers: &mut HeaderMap,
event: &Event,
anonymized_client_ip: &HeaderValue,
user_agent: &HeaderValue,
) -> anyhow::Result<()> {
// Insert client ip in the x-forwarded-for header
headers.insert(
HeaderName::from_str("x-forwarded-for")?,
anonymized_client_ip.clone(),
);

// Insert User-Agent in the user-agent header
headers.insert(header::USER_AGENT, user_agent.clone());

// Insert referrer in the referer header
if event
.context
.as_ref()
.unwrap()
.page
.as_ref()
.unwrap()
.referrer
.is_some()
{
headers.insert(
header::REFERER,
HeaderValue::from_str(
event
.context
.as_ref()
.unwrap()
.page
.as_ref()
.unwrap()
.referrer
.as_ref()
.unwrap(),
)?,
);
} else {
// If referer is empty, insert the current page path in the referer header, like an analytics client-side collect does
headers.insert(
header::REFERER,
HeaderValue::from_str(
event
.context
.as_ref()
.unwrap()
.page
.as_ref()
.unwrap()
.url
.as_ref()
.unwrap(),
)?,
);
}

// Insert Accept-Language in the accept-language header
if event
.context
.as_ref()
.unwrap()
.client
.as_ref()
.unwrap()
.accept_language
.is_some()
{
headers.insert(
header::ACCEPT_LANGUAGE,
HeaderValue::from_str(
event
.context
.as_ref()
.unwrap()
.client
.as_ref()
.unwrap()
.accept_language
.as_ref()
.unwrap(),
)?,
);
}

Ok(())
}

fn debug_request(request: &provider::EdgeeRequest, headers: &HeaderMap, component_name: &str) {
let config = config::get();

let method_str = match request.method {
Expand All @@ -217,13 +304,13 @@ fn debug_request(request: &provider::EdgeeRequest, component_name: &str) {
println!("-----------\n");
println!("Method: {}", method_str);
println!("Url: {}", request.url);
if !request.headers.is_empty() {
if !headers.is_empty() {
print!("Headers: ");
for (i, (key, value)) in request.headers.iter().enumerate() {
for (i, (key, value)) in headers.iter().enumerate() {
if i == 0 {
println!("{}: {}", key, value);
println!("{}: {:?}", key, value);
} else {
println!(" {}: {}", key, value);
println!(" {}: {:?}", key, value);
}
}
} else {
Expand Down
Loading