Skip to content

Conversation

@pietroalbini
Copy link
Member

This PR does three things:

  • Refactors custom request metadata to use a thread local instead of a request extension. This was needed to avoid passing &mut dyn RequestExt to the email methods (as that's not really clean and caused borrow checker issues).
  • Logs the email backend, the message ID (if successful) and the error (if failed) in each request that sends emails.
  • Panics if non-SMTP backends are used in production.

This PR is best reviewed commit-by-commit.

Fixes #4596
Fixes #4597

Before this commit, custom metadata was stored as part of a request
extension, which required the mutable request object to be passed to
every place which needed to log something.

This commit changes the implementation to use a thread local that is
cleared before a request is processed. This removes the dependency on
the mutable request object.

Note that when migrating the crates.io codebase to async the thread
local will need to be replaced with an async equivalent.
@pietroalbini
Copy link
Member Author

Note that I don't know whether Mailgun will override the message ID with its own or not. I tested the change locally with a different mail server I have access to, but I don't know how Mailgun behaves in regards to this.

@Turbo87 Turbo87 added A-backend ⚙️ C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear labels Mar 10, 2022
Copy link
Member

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to r=me

server_error("Failed to send the email")
})?;

add_custom_metadata("email_id", message_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason for adding the email_id only after the email error was handled? wouldn't it be better to set it as soon as we generated it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no use for the Message-ID if Mailgun returned an error: in that case, the mail wouldn't have been sent and wouldn't show up in the Mailgun console, so having the ID offers no practical benefit.

@pietroalbini
Copy link
Member Author

@bors r=Turbo87

@bors
Copy link
Contributor

bors commented Mar 10, 2022

📌 Commit b7a75f5 has been approved by Turbo87

@bors
Copy link
Contributor

bors commented Mar 10, 2022

⌛ Testing commit b7a75f5 with merge e5ed648...

@bors
Copy link
Contributor

bors commented Mar 10, 2022

☀️ Test successful - checks-actions
Approved by: Turbo87
Pushing e5ed648 to master...

@bors bors merged commit e5ed648 into rust-lang:master Mar 10, 2022
@pietroalbini pietroalbini deleted the pa-log-emails branch March 15, 2022 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-backend ⚙️ C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add more logging around email sending In production, panic if email environment variables aren't set

3 participants