Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat: search tracing (#1386)
gbirman5aa098ec32c6
diff --git a/rust/cloud-storage/search_service/src/api/search/enrich.rs b/rust/cloud-storage/search_service/src/api/search/enrich.rsindex 2ad9c79bfe..6bf08aca7a 100644--- a/rust/cloud-storage/search_service/src/api/search/enrich.rs+++ b/rust/cloud-storage/search_service/src/api/search/enrich.rs@@ -13,6 +13,7 @@ use crate::api::{};/// Enriches search results with metadat and converts to UnifiedSearchResponseItem+#[tracing::instrument(skip(ctx, results), fields(result_count = results.len()), err)]pub async fn enrich_search_response(ctx: &SearchHandlerState,user_id: &str,diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rsindex 2d2f8877c0..4275a459fc 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs@@ -8,6 +8,7 @@ pub(in crate::api::search) struct FilterChannelResponse {pub channel_ids: Vec<sqlx::types::Uuid>,}+#[tracing::instrument(skip(ctx, filters), err)]pub(in crate::api::search) async fn filter_channels(ctx: &SearchHandlerState,user_id: &str,diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rsindex b81e8695e7..1c8ba725cd 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_chat.rs@@ -13,6 +13,7 @@ pub(in crate::api::search) struct FilterChatResponse {pub ids_only: bool,}+#[tracing::instrument(skip(ctx, filters), err)]pub(in crate::api::search) async fn filter_chats(ctx: &SearchHandlerState,user_id: &str,diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rsindex 5a2e63ff74..2088cc3272 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_document.rs@@ -13,6 +13,7 @@ pub(in crate::api::search) struct FilterDocumentResponse {pub ids_only: bool,}+#[tracing::instrument(skip(ctx, filters), err)]pub(in crate::api::search) async fn filter_documents(ctx: &SearchHandlerState,user_id: &str,diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_project.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_project.rsindex c14410f975..0a03874dee 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_project.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_project.rs@@ -13,6 +13,7 @@ pub(in crate::api::search) struct FilterProjectResponse {pub ids_only: bool,}+#[tracing::instrument(skip(ctx, filters), err)]pub(in crate::api::search) async fn filter_projects(ctx: &SearchHandlerState,user_id: &str,diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rsindex 428a62a5e3..319cdd9a8f 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_unified.rs@@ -413,143 +413,169 @@ pub(in crate::api::search) async fn perform_unified_search(let project_name_count = project_hits.len();let content_count = content_hits.len();- // Wrap results with source tags- let mut combined: Vec<TaggedSearchHit> = Vec::new();- combined.extend(doc_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::DocumentName,- }));- combined.extend(chat_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::ChatName,- }));- combined.extend(email_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::EmailSubject,- }));- combined.extend(email_contact_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::EmailContact,- }));- combined.extend(project_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::ProjectName,- }));- combined.extend(content_hits.into_iter().map(|hit| TaggedSearchHit {- hit,- source: SearchSource::Content,- }));-- // Sort: updated_at DESC (None to bottom), entity_id DESC as tiebreaker- combined.sort_by(|a, b| match (&b.hit.updated_at, &a.hit.updated_at) {- (Some(b_ts), Some(a_ts)) => b_ts- .cmp(a_ts)- .then_with(|| b.hit.entity_id.cmp(&a.hit.entity_id)),- (Some(_), None) => std::cmp::Ordering::Less,- (None, Some(_)) => std::cmp::Ordering::Greater,- (None, None) => b.hit.entity_id.cmp(&a.hit.entity_id),- });-- // Take only page_size results- let page_size_usize = page_size as usize;- let final_tagged: Vec<TaggedSearchHit> = combined.into_iter().take(page_size_usize).collect();-- // Count included results by source- let included_doc_names = final_tagged- .iter()- .filter(|h| h.source == SearchSource::DocumentName)- .count();- let included_chat_names = final_tagged- .iter()- .filter(|h| h.source == SearchSource::ChatName)- .count();- let included_email_subjects = final_tagged- .iter()- .filter(|h| h.source == SearchSource::EmailSubject)- .count();- let included_email_contacts = final_tagged- .iter()- .filter(|h| h.source == SearchSource::EmailContact)- .count();- let included_project_names = final_tagged- .iter()- .filter(|h| h.source == SearchSource::ProjectName)- .count();- let included_content = final_tagged- .iter()- .filter(|h| h.source == SearchSource::Content)- .count();- // Content cursor deferred - always None for now-- // Generate new cursors using helper function- let new_doc_cursor = compute_next_cursor(- &doc_next_cursor,- included_doc_names,- doc_name_count,- find_last_of_source(&final_tagged, SearchSource::DocumentName),- &document_cursor,- );-- let new_chat_cursor = compute_next_cursor(- &chat_next_cursor,- included_chat_names,- chat_name_count,- find_last_of_source(&final_tagged, SearchSource::ChatName),- &chat_cursor,- );-- let new_email_cursor = compute_next_cursor(- &email_next_cursor,- included_email_subjects,- email_subject_count,- find_last_of_source(&final_tagged, SearchSource::EmailSubject),- &email_cursor,- );-- let new_email_contact_cursor = compute_next_cursor(- &email_contact_next_cursor,- included_email_contacts,- email_contact_count,- find_last_of_source(&final_tagged, SearchSource::EmailContact),- &email_contact_cursor,- );-- let new_project_cursor = compute_next_cursor(- &project_next_cursor,- included_project_names,- project_name_count,- find_last_of_source(&final_tagged, SearchSource::ProjectName),- &project_cursor,- );-- let new_content_cursor = compute_next_cursor(- &content_next_cursor,- included_content,- content_count,- find_last_of_source(&final_tagged, SearchSource::Content),- &content_cursor,- );+ let final_tagged = {+ let _span = tracing::info_span!(+ "combine_and_sort_results",+ doc_name_count,+ chat_name_count,+ email_subject_count,+ email_contact_count,+ project_name_count,+ content_count+ )+ .entered();++ // Wrap results with source tags+ let mut combined: Vec<TaggedSearchHit> = Vec::new();+ combined.extend(doc_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::DocumentName,+ }));+ combined.extend(chat_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::ChatName,+ }));+ combined.extend(email_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::EmailSubject,+ }));+ combined.extend(email_contact_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::EmailContact,+ }));+ combined.extend(project_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::ProjectName,+ }));+ combined.extend(content_hits.into_iter().map(|hit| TaggedSearchHit {+ hit,+ source: SearchSource::Content,+ }));++ tracing::debug!(total_combined = combined.len(), "combined all results");++ // Sort: updated_at DESC (None to bottom), entity_id DESC as tiebreaker+ combined.sort_by(|a, b| match (&b.hit.updated_at, &a.hit.updated_at) {+ (Some(b_ts), Some(a_ts)) => b_ts+ .cmp(a_ts)+ .then_with(|| b.hit.entity_id.cmp(&a.hit.entity_id)),+ (Some(_), None) => std::cmp::Ordering::Less,+ (None, Some(_)) => std::cmp::Ordering::Greater,+ (None, None) => b.hit.entity_id.cmp(&a.hit.entity_id),+ });++ // Take only page_size results+ let page_size_usize = page_size as usize;+ let final_tagged: Vec<TaggedSearchHit> =+ combined.into_iter().take(page_size_usize).collect();++ tracing::debug!(+ final_count = final_tagged.len(),+ "final results after pagination"+ );++ final_tagged+ };- // Build next cursor if any source has more results- let has_more = new_doc_cursor.has_more()- || new_chat_cursor.has_more()- || new_email_cursor.has_more()- || new_email_contact_cursor.has_more()- || new_project_cursor.has_more()- || new_content_cursor.has_more();-- let next_cursor = if has_more {- let cursor = SearchCursor {- document_name_cursor: new_doc_cursor,- chat_name_cursor: new_chat_cursor,- content_cursor: new_content_cursor,- email_subject_cursor: new_email_cursor,- email_contact_cursor: new_email_contact_cursor,- project_name_cursor: new_project_cursor,- };- cursor.encode()- } else {- None+ let next_cursor = {+ let _span = tracing::info_span!("compute_pagination_cursors").entered();++ // Count included results by source+ let included_doc_names = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::DocumentName)+ .count();+ let included_chat_names = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::ChatName)+ .count();+ let included_email_subjects = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::EmailSubject)+ .count();+ let included_email_contacts = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::EmailContact)+ .count();+ let included_project_names = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::ProjectName)+ .count();+ let included_content = final_tagged+ .iter()+ .filter(|h| h.source == SearchSource::Content)+ .count();++ // Generate new cursors using helper function+ let new_doc_cursor = compute_next_cursor(+ &doc_next_cursor,+ included_doc_names,+ doc_name_count,+ find_last_of_source(&final_tagged, SearchSource::DocumentName),+ &document_cursor,+ );++ let new_chat_cursor = compute_next_cursor(+ &chat_next_cursor,+ included_chat_names,+ chat_name_count,+ find_last_of_source(&final_tagged, SearchSource::ChatName),+ &chat_cursor,+ );++ let new_email_cursor = compute_next_cursor(+ &email_next_cursor,+ included_email_subjects,+ email_subject_count,+ find_last_of_source(&final_tagged, SearchSource::EmailSubject),+ &email_cursor,+ );++ let new_email_contact_cursor = compute_next_cursor(+ &email_contact_next_cursor,+ included_email_contacts,+ email_contact_count,+ find_last_of_source(&final_tagged, SearchSource::EmailContact),+ &email_contact_cursor,+ );++ let new_project_cursor = compute_next_cursor(+ &project_next_cursor,+ included_project_names,+ project_name_count,+ find_last_of_source(&final_tagged, SearchSource::ProjectName),+ &project_cursor,+ );++ let new_content_cursor = compute_next_cursor(+ &content_next_cursor,+ included_content,+ content_count,+ find_last_of_source(&final_tagged, SearchSource::Content),+ &content_cursor,+ );++ // Build next cursor if any source has more results+ let has_more = new_doc_cursor.has_more()+ || new_chat_cursor.has_more()+ || new_email_cursor.has_more()+ || new_email_contact_cursor.has_more()+ || new_project_cursor.has_more()+ || new_content_cursor.has_more();++ if has_more {+ let cursor = SearchCursor {+ document_name_cursor: new_doc_cursor,+ chat_name_cursor: new_chat_cursor,+ content_cursor: new_content_cursor,+ email_subject_cursor: new_email_cursor,+ email_contact_cursor: new_email_contact_cursor,+ project_name_cursor: new_project_cursor,+ };+ cursor.encode()+ } else {+ None+ }};// Extract final SearchHits from tagged resultsdiff --git a/rust/cloud-storage/search_service/src/api/search/unified.rs b/rust/cloud-storage/search_service/src/api/search/unified.rsindex 30a1dc0f8e..55e8ab6222 100644--- a/rust/cloud-storage/search_service/src/api/search/unified.rs+++ b/rust/cloud-storage/search_service/src/api/search/unified.rs@@ -55,7 +55,10 @@ pub async fn handler(document,email,project,- } = results.into_iter().split_search_response();+ } = {+ let _span = tracing::info_span!("split_search_response_by_type").entered();+ results.into_iter().split_search_response()+ };let (enriched_document_results,@@ -97,15 +100,19 @@ pub async fn handler().map_err(|e| SearchError::InternalError(anyhow::anyhow!("tokio error: {:?}", e)))?;- let mut results = vec![];+ let results = {+ let _span = tracing::info_span!("combine_and_sort_enriched_results").entered();- results.extend(enriched_document_results);- results.extend(enriched_chat_results);- results.extend(enriched_channel_results);- results.extend(enriched_project_results);- results.extend(enriched_email_results);+ let mut results = vec![];- results = sort_unified_search_results(results);+ results.extend(enriched_document_results);+ results.extend(enriched_chat_results);+ results.extend(enriched_channel_results);+ results.extend(enriched_project_results);+ results.extend(enriched_email_results);++ sort_unified_search_results(results)+ };Ok(Json(UnifiedSearchResponse {results,@@ -115,6 +122,7 @@ pub async fn handler(/// Sorts the unified results/// This method is so we can more easily test sorting+#[tracing::instrument(skip(results), fields(count = results.len()))]fn sort_unified_search_results(mut results: Vec<UnifiedSearchResponseItem>,) -> Vec<UnifiedSearchResponseItem> {