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

feat: trim distinct_id to 200 chars #61

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions capture-server/tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,44 @@ async fn it_does_not_partition_limit_different_ids() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn it_trims_distinct_id() -> Result<()> {
setup_tracing();
let token = random_string("token", 16);
let distinct_id1 = random_string("id", 200 - 3);
let distinct_id2 = random_string("id", 200);
let (trimmed_distinct_id2, _) = distinct_id2.split_at(200); // works because ascii chars

let topic = EphemeralTopic::new().await;
let server = ServerHandle::for_topic(&topic);

let event = json!([{
"token": token,
"event": "event1",
"distinct_id": distinct_id1
},{
"token": token,
"event": "event2",
"distinct_id": distinct_id2
}]);
let res = server.capture_events(event.to_string()).await;
assert_eq!(StatusCode::OK, res.status());

assert_json_include!(
actual: topic.next_event()?,
expected: json!({
"token": token,
"distinct_id": distinct_id1
})
);
assert_json_include!(
actual: topic.next_event()?,
expected: json!({
"token": token,
"distinct_id": trimmed_distinct_id2
})
);

Ok(())
}
8 changes: 7 additions & 1 deletion capture/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ pub fn process_single_event(
_ => return Err(CaptureError::MissingDistinctId),
},
};
// Limit the size of distinct_id to 200 chars
let distinct_id: String = match distinct_id.len() {
0..=200 => distinct_id.to_owned(),
_ => distinct_id.chars().take(200).collect(),
};

if event.event.is_empty() {
return Err(CaptureError::MissingEventName);
}
Expand All @@ -183,7 +189,7 @@ pub fn process_single_event(

Ok(ProcessedEvent {
uuid: event.uuid.unwrap_or_else(uuid_v7),
distinct_id: distinct_id.to_string(),
distinct_id,
ip: context.client_ip.clone(),
data,
now: context.now.clone(),
Expand Down