emails: include logging information in email headers.

Log the following information in email headers:

* X-Danbooru-User: the user's name and ID.
* X-Danbooru-IP: the user's IP.
* X-Danbooru-Session: the users' session ID.
* X-Danbooru-URL: the page that triggered the email.
* X-Danbooru-Job-Id: the ID of the background job that sent the email.
* X-Danbooru-Enqueued-At: when the email was queued as a background job.
* X-Danbooru-Dmail: for Dmail notifications, the link to the Dmail.
* X-Request-Id: the request ID of the HTTP request that triggered the email.

Also make it so we log an event in the APM when we send an email.
This commit is contained in:
evazion
2022-09-28 20:16:50 -05:00
parent ed9986def6
commit b94cb7d824
14 changed files with 93 additions and 11 deletions

View File

@@ -34,7 +34,7 @@ class ApplicationJob < ActiveJob::Base
PruneRateLimitsJob, ProcessUploadJob, RegeneratePostCountsJob,
RegeneratePostJob, RetireTagRelationshipsJob, VacuumDatabaseJob,
DiscordNotificationJob, BigqueryExportJob, ProcessBulkUpdateRequestJob,
PruneJobsJob, ActionMailer::MailDeliveryJob
PruneJobsJob, MailDeliveryJob
]
end
end

View File

@@ -0,0 +1,22 @@
# frozen_string_literal: true
# A replacement for the default ActionMailer::MailDeliveryJob that inherits from ApplicationJob, so
# it inherits the same behavior as other jobs. It also inserts the job ID into the mail headers
# for logging purposes.
#
# @see https://github.com/rails/rails/blob/main/actionmailer/lib/action_mailer/mail_delivery_job.rb
# @see https://guides.rubyonrails.org/configuring.html#config-action-mailer-delivery-job
# @see config/application.rb (config.action_mailer.delivery_job = "MailDeliveryJob")
class MailDeliveryJob < ApplicationJob
def perform(mailer, mail_method, delivery_method, args:, kwargs: nil, params: nil)
mailer_class = mailer.constantize.with(params.to_h) # mailer_class = UserMailer.with(params)
mail = mailer_class.public_send(mail_method, *args, **kwargs.to_h) # mail = UserMailer.welcome_user(user)
mail.headers(
"X-Danbooru-Job-Id": job_id,
"X-Danbooru-Enqueued-At": enqueued_at,
)
mail.send(delivery_method) # mail.deliver_now
end
end