Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4623

How to pass the current request id to ActiveJob and ActionMailer for tagged logging?

$
0
0

I want to improve the logging of my application. I use tagged logging with the request_id. Basically this is rails standard for production environments.

I'd like to know from where log lines from sidekiq, actionmailer and activejob origin from.

I use Rails Semantic Logger to get structured logs.

For sidekiq, I found to use job tags which are being picked up by rails semantic logger and are now being logged which help to identify log lines which belong together.

This looks something as follows

21:15:30 worker.1    | 2024-10-22 21:15:30.273383 I [71200:sidekiq.default/processor] {jid: 32abadde984a6b6c5a95e1ed, tags: ["my-request-id"], queue: default} (44.6ms) AppointmentCompletionMissingReminderJob -- Completed #perform

This is probably not the best solution but at least it does the job.

Now I want to have the request id in logs created by actionmailer and activejob as well. One could isolate the problem to activejob with the assumption of using a custom deliveryjob and not the default ActionMailer::MailDeliveryJob.

Currently I have logs as follows:

21:06:52 web.1       | 2024-10-22 21:06:52.433809 I [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] Rails -- Performing ActionMailer::MailDeliveryJob (Job ID: 3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1) to Async(high) -- { :event_name => "perform_start.active_job", :adapter => "Async", :queue => "high", :job_class => "ActionMailer::MailDeliveryJob", :job_id => "3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1", :provider_job_id => "fcab8cb1-e661-4821-916a-695e5e42e70f", :arguments => "[\n  \"UserMailer\",\n  \"magic_link\",\n  \"deliver_now\",\n  {\n    \"params\": {\n      \"user\": \"gid://xxx/User/1\",\n      \"redirect_path\": \"\"\n    },\n    \"args\": [\n\n    ]\n  }\n]" }21:06:52 web.1       | 2024-10-22 21:06:52.434135 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] UserMailer -- Sending magic link to user21:06:52 web.1       | 2024-10-22 21:06:52.446786 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] ActionView -- Rendering layout -- { :template => "layouts/mailer.haml" }21:06:52 web.1       | 2024-10-22 21:06:52.446812 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] ActionView -- Rendering -- { :template => "user_mailer/magic_link.html.erb" }21:06:52 web.1       | 2024-10-22 21:06:52.470154 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] (23.3ms) ActionView -- Rendered -- { :template => "user_mailer/magic_link.html.erb", :within => "layouts/mailer", :allocations => 33255 }21:06:52 web.1       | 2024-10-22 21:06:52.472222 I [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] ActionMailer::Base -- UserMailer#magic_link: processed outbound mail in 38.1ms -- { :event_name => "process.action_mailer", :mailer => "UserMailer", :action => :magic_link, :message_id => nil, :perform_deliveries => nil, :subject => nil, :to => nil, :from => nil, :bcc => nil, :cc => nil, :date => nil, :args => nil }

where 3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1 is the job id created by activejob. I'd like to have another log tag containing the request id.

I use ActiveSupport::CurrentAttributes to provide it with Current.request_id everywhere in my application.

The question is, how can I inject the request id to the job when it gets created, e.g. in my sign-in controller?

The result should be something like this:

21:06:52 web.1       | 2024-10-22 21:06:52.433809 I [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] Rails ...21:06:52 web.1       | 2024-10-22 21:06:52.434135 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] UserMailer ...21:06:52 web.1       | 2024-10-22 21:06:52.446786 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] ActionView ...21:06:52 web.1       | 2024-10-22 21:06:52.446812 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] ActionView ...21:06:52 web.1       | 2024-10-22 21:06:52.470154 D [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] (23.3ms) ActionView ...21:06:52 web.1       | 2024-10-22 21:06:52.472222 I [71197:worker-1] [ActionMailer::MailDeliveryJob] [3821d8b0-d6fe-4f12-8e6a-a5c3d152d2e1] [my-request-id] ActionMailer::Base ...

Viewing all articles
Browse latest Browse all 4623


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>