Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

chore(email-service): Better logging for init and gmail client (#2111)

Evan Hutnikf4324febb712
diff --git a/rust/cloud-storage/email_service/src/api/email/init.rs b/rust/cloud-storage/email_service/src/api/email/init.rs
index 4e293a4d73..2db6136246 100644
--- a/rust/cloud-storage/email_service/src/api/email/init.rs
+++ b/rust/cloud-storage/email_service/src/api/email/init.rs
@@ -38,6 +38,9 @@ pub enum InitError {
#[error("Database query error")]
DatabaseError(#[from] anyhow::Error),
+ #[error("Gmail API error")]
+ GmailError(#[from] models_email::gmail::error::GmailError),
+
#[error("Bad request")]
BadRequest(String),
@@ -52,7 +55,7 @@ impl IntoResponse for InitError {
StatusCode::BAD_REQUEST
}
InitError::TooManyJobs => StatusCode::TOO_MANY_REQUESTS,
- InitError::EnqueueError | InitError::DatabaseError(_) => {
+ InitError::EnqueueError | InitError::DatabaseError(_) | InitError::GmailError(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
};
@@ -208,11 +211,7 @@ pub async fn enable_gmail_sync(
};
// Register watch with Gmail
- let watch_response = ctx
- .gmail_client
- .register_watch(&token)
- .await
- .context("Gmail call to register watch failed")?;
+ let watch_response = ctx.gmail_client.register_watch(&token).await?;
let email = extract_email_with_response(&user_context.user_id)
.map_err(|_| InitError::BadRequest("Failed to extract email".to_string()))?;
diff --git a/rust/cloud-storage/gmail_client/src/attachments.rs b/rust/cloud-storage/gmail_client/src/attachments.rs
index 02ff249587..954ff6a40a 100644
--- a/rust/cloud-storage/gmail_client/src/attachments.rs
+++ b/rust/cloud-storage/gmail_client/src/attachments.rs
@@ -4,7 +4,7 @@ use base64::Engine;
use base64::engine::general_purpose::URL_SAFE;
use models_email::gmail::AttachmentGetResponse;
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub async fn get_attachment_data(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/contacts.rs b/rust/cloud-storage/gmail_client/src/contacts.rs
index d08b07c5b5..7c010f5053 100644
--- a/rust/cloud-storage/gmail_client/src/contacts.rs
+++ b/rust/cloud-storage/gmail_client/src/contacts.rs
@@ -4,7 +4,7 @@ use models_email::gmail::contacts::{ConnectionsResponse, OtherContactsResponse,
/// Get the user's own connection.
/// Returns raw Gmail PersonResource - callers should map to service layer Contact.
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_self_connection(
client: &GmailClient,
access_token: &str,
@@ -39,7 +39,7 @@ pub(crate) async fn get_self_connection(
/// If a sync_token is provided, it fetches only the changes since the last sync.
/// Otherwise, it performs a full sync of all contacts.
/// Returns raw Gmail PersonResource objects and the next sync token - callers should map to service layer Contacts.
-#[tracing::instrument(skip(client, access_token, sync_token))]
+#[tracing::instrument(skip(client, access_token, sync_token), err)]
pub(crate) async fn list_connections(
client: &GmailClient,
access_token: &str,
@@ -112,7 +112,7 @@ pub(crate) async fn list_connections(
/// If a sync_token is provided, it fetches only the changes since the last sync.
/// Otherwise, it performs a full sync of all "Other Contacts".
/// Returns raw Gmail PersonResource objects and the next sync token - callers should map to service layer Contacts.
-#[tracing::instrument(skip(client, access_token, sync_token))]
+#[tracing::instrument(skip(client, access_token, sync_token), err)]
pub(crate) async fn list_other_contacts(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/history.rs b/rust/cloud-storage/gmail_client/src/history.rs
index a4afc7d927..52272ab3fe 100644
--- a/rust/cloud-storage/gmail_client/src/history.rs
+++ b/rust/cloud-storage/gmail_client/src/history.rs
@@ -6,7 +6,7 @@ const MAX_RESULTS: usize = 500;
/// Gets the changes to a user's inbox that have occurred since start_history_id.
/// Returns raw HistoryListResponse - callers should map to InboxChanges using convert module.
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_history(
client: &GmailClient,
access_token: &str,
@@ -79,6 +79,7 @@ pub(crate) async fn get_history(
}
/// returns the current history id for the user's inbox using the /profile endpoint
+#[tracing::instrument(skip(client, access_token), err)]
pub async fn get_current_history_id(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/labels.rs b/rust/cloud-storage/gmail_client/src/labels.rs
index ffc027c9e3..e90ccbda7b 100644
--- a/rust/cloud-storage/gmail_client/src/labels.rs
+++ b/rust/cloud-storage/gmail_client/src/labels.rs
@@ -12,7 +12,8 @@ use uuid::Uuid;
#[tracing::instrument(
skip(client, access_token),
fields(provider_message_id = %provider_message_id),
- level = "debug"
+ level = "debug",
+ err
)]
pub async fn modify_message_labels(
client: &GmailClient,
@@ -116,7 +117,7 @@ pub async fn batch_modify_labels(
(successful_msg_ids, failed_msg_ids)
}
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub async fn fetch_user_labels(
client: &GmailClient,
access_token: &str,
@@ -150,7 +151,7 @@ pub async fn fetch_user_labels(
Ok(service_labels)
}
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub async fn create_label(
client: &GmailClient,
access_token: &str,
@@ -204,7 +205,8 @@ pub async fn create_label(
#[tracing::instrument(
skip(client, access_token),
fields(label_id = %label_id),
- level = "info"
+ level = "info",
+ err
)]
pub async fn delete_gmail_label(
client: &GmailClient,
diff --git a/rust/cloud-storage/gmail_client/src/lib.rs b/rust/cloud-storage/gmail_client/src/lib.rs
index 7ac15903dc..1793127b3d 100644
--- a/rust/cloud-storage/gmail_client/src/lib.rs
+++ b/rust/cloud-storage/gmail_client/src/lib.rs
@@ -63,7 +63,7 @@ impl GmailClient {
}
/// Lists the num_threads most recent threads for the user
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn list_threads(
&self,
access_token: &str,
@@ -74,7 +74,7 @@ impl GmailClient {
}
// Returns a list containing the message ids belonging to the thread.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_message_ids_for_thread(
&self,
access_token: &str,
@@ -85,7 +85,7 @@ impl GmailClient {
/// Fetches the threads and messages for the given thread_ids.
/// Returns raw Gmail ThreadResource objects - callers should map to service layer structs.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_threads(
&self,
access_token: &str,
@@ -96,7 +96,7 @@ impl GmailClient {
/// Gets the changes to a user's inbox that have occurred since start_history_id.
/// Returns raw HistoryListResponse - callers should map to InboxChanges using convert module.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_history(
&self,
access_token: &str,
@@ -106,20 +106,20 @@ impl GmailClient {
}
/// Returns the current history id for the user's inbox
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_current_history_id(&self, access_token: &str) -> anyhow::Result<String> {
history::get_current_history_id(self, access_token).await
}
/// Fetches Google's public JWKS keys used for verifying OAuth 2.0 tokens
- #[tracing::instrument(skip(self))]
+ #[tracing::instrument(skip(self), err)]
pub async fn get_google_public_keys(&self) -> anyhow::Result<GooglePublicKeys> {
fetch_google_public_keys(self).await
}
/// Verifies a Google JWT token against the provided public keys
/// Validates the token's signature, issuer, audience, and expiration time
- #[tracing::instrument(skip(self, token, public_keys))]
+ #[tracing::instrument(skip(self, token, public_keys), err)]
pub fn verify_google_token(
&self,
token: &str,
@@ -130,7 +130,7 @@ impl GmailClient {
/// Registers a push notification watch on the user's inbox
/// This will cause notifications to be sent to the subscription_topic
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn register_watch(
&self,
access_token: &str,
@@ -139,7 +139,7 @@ impl GmailClient {
}
/// Stops push notifications by revoking the notification watch
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn stop_watch(&self, access_token: &str) -> anyhow::Result<()> {
watch::stop_watch(self, access_token).await
}
@@ -148,7 +148,8 @@ impl GmailClient {
#[tracing::instrument(
skip(self, access_token),
fields(provider_message_id = %provider_message_id),
- level = "debug"
+ level = "debug",
+ err
)]
pub async fn modify_message_labels(
&self,
@@ -193,7 +194,7 @@ impl GmailClient {
/// Fetches a specific message from Gmail by its provider ID.
/// Returns raw Gmail MessageResource - callers should map to service layer structs.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_message(
&self,
access_token: &str,
@@ -203,7 +204,7 @@ impl GmailClient {
}
/// Fetches a specific message's thread ID from Gmail by its provider ID
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_message_thread_id(
&self,
access_token: &str,
@@ -212,7 +213,7 @@ impl GmailClient {
get_message_thread_id(self, access_token, message_provider_id).await
}
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_message_label_ids(
&self,
access_token: &str,
@@ -222,7 +223,7 @@ impl GmailClient {
}
/// Sends a new email message
- #[tracing::instrument(skip(self, access_token, message))]
+ #[tracing::instrument(skip(self, access_token, message), err)]
pub async fn send_message(
&self,
access_token: &str,
@@ -242,13 +243,13 @@ impl GmailClient {
.await
}
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_profile_threads_total(&self, access_token: &str) -> anyhow::Result<i32> {
profile::get_profile_threads_total(self, access_token).await
}
/// Fetches an attachment from Gmail by its provider ID
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_attachment_data(
&self,
access_token: &str,
@@ -259,7 +260,7 @@ impl GmailClient {
}
/// Fetches user's Gmail labels
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn fetch_user_labels(
&self,
access_token: &str,
@@ -269,7 +270,7 @@ impl GmailClient {
}
/// Creates a new Gmail label for the user
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn create_label(
&self,
access_token: &str,
@@ -280,14 +281,14 @@ impl GmailClient {
}
/// Deletes a Gmail label by its ID
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn delete_label(&self, access_token: &str, label_id: &str) -> Result<(), GmailError> {
delete_gmail_label(self, access_token, label_id).await
}
/// Fetches the user's own contact information.
/// Returns raw Gmail PersonResource - callers should map to service layer Contact.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_self_contact(&self, access_token: &str) -> anyhow::Result<PersonResource> {
get_self_connection(self, access_token).await
}
@@ -295,7 +296,7 @@ impl GmailClient {
/// Fetches all of the user's main contacts, handling pagination.
/// Returns raw Gmail PersonResource objects and a sync token for future incremental updates.
/// Callers should map PersonResource to service layer Contact.
- #[tracing::instrument(skip(self, access_token, sync_token))]
+ #[tracing::instrument(skip(self, access_token, sync_token), err)]
pub async fn get_contacts(
&self,
access_token: &str,
@@ -308,7 +309,7 @@ impl GmailClient {
/// These are typically contacts auto-created from interactions.
/// Returns raw Gmail PersonResource objects and a sync token.
/// Callers should map PersonResource to service layer Contact.
- #[tracing::instrument(skip(self, access_token, sync_token))]
+ #[tracing::instrument(skip(self, access_token, sync_token), err)]
pub async fn get_other_contacts(
&self,
access_token: &str,
@@ -318,7 +319,7 @@ impl GmailClient {
}
/// Gets the email signature for a specific email address
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_email_signature(
&self,
access_token: &str,
@@ -329,7 +330,7 @@ impl GmailClient {
/// Blocks a sender by creating a filter that sends their emails to SPAM.
/// This replicates the "Block Sender" functionality in the Gmail UI.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn block_sender(
&self,
access_token: &str,
@@ -339,7 +340,7 @@ impl GmailClient {
}
/// Creates a new Gmail filter.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn create_filter(
&self,
access_token: &str,
@@ -349,13 +350,13 @@ impl GmailClient {
}
/// Lists all filters for the user.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn list_filters(&self, access_token: &str) -> Result<Vec<Filter>, GmailError> {
filters::list_filters(self, access_token).await
}
/// Gets a specific filter by ID.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn get_filter(
&self,
access_token: &str,
@@ -365,7 +366,7 @@ impl GmailClient {
}
/// Deletes a filter by ID.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn delete_filter(
&self,
access_token: &str,
@@ -376,7 +377,7 @@ impl GmailClient {
/// Finds and returns any existing "block" filters for a specific email address.
/// This can be used to check if a user is already blocked.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn find_block_filter_for_sender(
&self,
access_token: &str,
@@ -387,7 +388,7 @@ impl GmailClient {
/// Unblocks a sender by finding and deleting their block filter.
/// Returns true if a filter was found and deleted, false if no filter existed.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn unblock_sender(
&self,
access_token: &str,
@@ -398,7 +399,7 @@ impl GmailClient {
/// Lists all blocked senders by finding filters that send emails to TRASH.
/// Returns a list of email addresses that are currently blocked.
- #[tracing::instrument(skip(self, access_token))]
+ #[tracing::instrument(skip(self, access_token), err)]
pub async fn list_blocked_senders(
&self,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/messages.rs b/rust/cloud-storage/gmail_client/src/messages.rs
index f1e803bde3..de0e56bbd6 100644
--- a/rust/cloud-storage/gmail_client/src/messages.rs
+++ b/rust/cloud-storage/gmail_client/src/messages.rs
@@ -9,7 +9,7 @@ use models_email::gmail::{
MessageResource, MinimalMessageResource, SendMessagePayload, SentMessageResource,
};
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_message(
client: &GmailClient,
access_token: &str,
@@ -55,7 +55,7 @@ pub(crate) async fn get_message(
}
// gets a message without the body using ?format=minimal in the gmail api call
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_message_thread_id(
client: &GmailClient,
access_token: &str,
@@ -100,7 +100,7 @@ pub(crate) async fn get_message_thread_id(
}
// gets a message without the body using ?format=minimal in the gmail api call
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_message_label_ids(
client: &GmailClient,
access_token: &str,
@@ -145,7 +145,7 @@ pub(crate) async fn get_message_label_ids(
}
/// sends a message
-#[tracing::instrument(skip(client, access_token, message), fields(link_id=%message.link_id))]
+#[tracing::instrument(skip(client, access_token, message), fields(link_id=%message.link_id), err)]
pub(crate) async fn send_message(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/profile.rs b/rust/cloud-storage/gmail_client/src/profile.rs
index c9d7aa3ac9..3e77dadddd 100644
--- a/rust/cloud-storage/gmail_client/src/profile.rs
+++ b/rust/cloud-storage/gmail_client/src/profile.rs
@@ -2,7 +2,7 @@ use crate::GmailClient;
use anyhow::Context;
use models_email::gmail::GmailUserProfile;
-#[tracing::instrument(skip(client, access_token), level = "debug")]
+#[tracing::instrument(skip(client, access_token), level = "debug", err)]
pub async fn get_profile_threads_total(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/threads.rs b/rust/cloud-storage/gmail_client/src/threads.rs
index 6f2d3cdbe8..e58f314241 100644
--- a/rust/cloud-storage/gmail_client/src/threads.rs
+++ b/rust/cloud-storage/gmail_client/src/threads.rs
@@ -19,7 +19,7 @@ const MAX_RETRY_ATTEMPTS: usize = 5;
const INITIAL_RETRY_DELAY_MS: u64 = 1000;
/// lists thread provider ids up to the requested number, or all if none specified
-#[tracing::instrument(skip(client, access_token, next_page_token))]
+#[tracing::instrument(skip(client, access_token, next_page_token), err)]
pub(crate) async fn list_threads(
client: &GmailClient,
access_token: &str,
@@ -82,7 +82,7 @@ pub(crate) async fn list_threads(
/// Wrapper for get_threads that retries on GmailBatchError::RateLimitExceeded,
/// reducing batch size each time with exponential backoff.
-#[tracing::instrument(skip(client, access_token, thread_ids), level = "info")]
+#[tracing::instrument(skip(client, access_token, thread_ids), level = "info", err)]
pub async fn get_threads_with_retry(
client: &GmailClient,
access_token: &str,
@@ -142,7 +142,7 @@ pub async fn get_threads_with_retry(
/// The Gmail batch API accepts a multipart/mixed request containing multiple individual requests consolidated into one.
/// It returns a multipart/mixed response that needs to be parsed into individual thread responses.
#[tracing::instrument(skip(client, access_token, thread_ids),
- fields(thread_ids = ?thread_ids, batch_size = batch_size))]
+ fields(thread_ids = ?thread_ids, batch_size = batch_size), err)]
pub(crate) async fn get_threads(
client: &GmailClient,
access_token: &str,
@@ -219,7 +219,7 @@ pub(crate) async fn get_threads(
/// Gets all message IDs for a specific thread using the minimal format
/// to reduce data transfer and processing time
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn get_message_ids_for_thread(
client: &GmailClient,
access_token: &str,
diff --git a/rust/cloud-storage/gmail_client/src/watch.rs b/rust/cloud-storage/gmail_client/src/watch.rs
index ae82c5ba7a..139440a4b1 100644
--- a/rust/cloud-storage/gmail_client/src/watch.rs
+++ b/rust/cloud-storage/gmail_client/src/watch.rs
@@ -5,7 +5,7 @@ use models_email::gmail::error::GmailError;
use models_email::gmail::history::WatchResponse;
/// Registers a user for Gmail push notifications to the specified Pub/Sub topic
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn register_watch(
client: &GmailClient,
access_token: &str,
@@ -55,7 +55,7 @@ pub(crate) async fn register_watch(
}
/// Stops push notifications for a user's Gmail inbox
-#[tracing::instrument(skip(client, access_token))]
+#[tracing::instrument(skip(client, access_token), err)]
pub(crate) async fn stop_watch(client: &GmailClient, access_token: &str) -> anyhow::Result<()> {
let url = format!("{}/users/me/stop", client.base_url);