Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add action mailer instrumentation #1280

Merged
merged 24 commits into from
Aug 20, 2021

Conversation

ericmustin
Copy link
Contributor

This PR provides instrumentation support for Rails 5/6 ActionMailer.

This PR addresses #250 , instruments the process.action_mailer and deliver.action_mailer ActiveSupport::Notification Events for the Rails ActionMailer Module.

There's perhaps some additional metadata that could be collected as span tags but a lot of it (from , to subject body etc etc) seems like it would contain PII, so i've limited the tags to what's useful for measuring execution times and errors between specific mailers and actions.

I will try to update this PR shortly with an example trace for a small sample application

@ericmustin ericmustin requested a review from a team December 8, 2020 16:00
end

def span_type
# ActionMailer creates emails like a controller
Copy link
Member

Choose a reason for hiding this comment

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

Could you clarify this? Are these top-level spans then? Do they happen on the same execution context as, say, a Rack request?

Copy link
Contributor Author

@ericmustin ericmustin Dec 9, 2020

Choose a reason for hiding this comment

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

i believe they are not top level spans , but i am going to try to stress test this in a sample app, will report back when i have more. tl;dr i believe i've botched the span_type here and need to update, but want to confirm that and just generally get a bit more familiar with what these actions represent within the mailer lifecycle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, to update: So this integration is similar to active record or action view in that there's a few different spans which could all be "top level" in the sense that they're the 1st span for the service, but generally speaking they'll rarely be the root span of the trace, usualy they're a child of action_pack controller span or a job consumer (delayed job, sidekiq, etc) span.

Here's a few examples:

  1. async job queue (deliver_later)

Image 2021-07-29 at 4 01 30 PM

  1. syncronous, no job queue (deliver_now)

Image 2021-07-29 at 1 51 29 PM

I've tried to update the span_types to reflect existing standards that are closer to their more appropriate usage, marking them as either a template (which is what .process spans do, they render the mailer templates) or worker (which is what .deliver spans do, they send emails).

Anyway lmk what you think

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I like the traces you showed above, they all seem reasonable to me 👍.


RSpec.shared_context 'ActionMailer helpers' do
before(:each) do
if ActionMailer::Base.respond_to?(:delivery_method)
Copy link
Member

@marcotc marcotc Dec 8, 2020

Choose a reason for hiding this comment

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

What is this condition for? Different versions of Rails?

Unless it's a trivial explanation, it would be good to have it as a comment, so we can know the rationale in the future while browsing this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added a comment

Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

Looks very good! I asked a few clarification questions, but very minor ones.

@delner delner added feature Involves a product feature integrations Involves tracing integrations labels Apr 1, 2021
@delner delner added this to Long Term Priority in Planning Apr 21, 2021
@ericmustin ericmustin marked this pull request as draft July 29, 2021 15:38
@ericmustin ericmustin marked this pull request as ready for review August 5, 2021 17:01
@ericmustin ericmustin requested a review from marcotc August 5, 2021 17:25
@marcotc
Copy link
Member

marcotc commented Aug 6, 2021

Are the information about the email being sent (from, to, subject, payload size) not available in the ActiveSupport notification?
Even if it contains PII, it would be good to have it optionally captured (possibly behind a c.use :action_mailer, email_data: true flag).

@marcotc
Copy link
Member

marcotc commented Aug 6, 2021

The actual network operations also seem to be missing, but I think that the current instrumentation level provides enough value as it.
We can add network-level Net::SMTP instrumentation separately later on.

@codecov-commenter
Copy link

Codecov Report

Merging #1280 (c21540f) into master (80fb6e6) will decrease coverage by 0.00%.
The diff coverage is 97.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1280      +/-   ##
==========================================
- Coverage   98.31%   98.30%   -0.01%     
==========================================
  Files         913      924      +11     
  Lines       44247    44542     +295     
==========================================
+ Hits        43501    43789     +288     
- Misses        746      753       +7     
Impacted Files Coverage Δ
spec/ddtrace/contrib/action_mailer/helpers.rb 90.00% <90.00%> (ø)
lib/ddtrace/contrib/action_mailer/events.rb 93.33% <93.33%> (ø)
spec/ddtrace/contrib/action_mailer/patcher_spec.rb 95.89% <95.89%> (ø)
lib/ddtrace/contrib/action_mailer/event.rb 96.00% <96.00%> (ø)
lib/datadog/contrib.rb 100.00% <100.00%> (ø)
...ce/contrib/action_mailer/configuration/settings.rb 100.00% <100.00%> (ø)
...ib/ddtrace/contrib/action_mailer/events/deliver.rb 100.00% <100.00%> (ø)
...ib/ddtrace/contrib/action_mailer/events/process.rb 100.00% <100.00%> (ø)
lib/ddtrace/contrib/action_mailer/ext.rb 100.00% <100.00%> (ø)
lib/ddtrace/contrib/action_mailer/integration.rb 100.00% <100.00%> (ø)
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 80fb6e6...c21540f. Read the comment docs.

@ericmustin
Copy link
Contributor Author

@marcotc lmk what you think, sorry didnt realize this was still open

@@ -32,6 +32,19 @@ def process(span, event, _id, payload)
span.span_type = span_type
span.set_tag(Ext::TAG_MAILER, payload[:mailer])
span.set_tag(Ext::TAG_MSG_ID, payload[:message_id])

# Since email date can contain PII we disable by default
# Some of these fields can be either strings or arrays, so we try to normalize
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

🛤️🛫

@ericmustin ericmustin merged commit a3236dd into master Aug 20, 2021
@ericmustin ericmustin deleted the issue_250_actionmailer_instrumentation branch August 20, 2021 14:51
@github-actions github-actions bot added this to the 0.53.0 milestone Aug 20, 2021
@hale
Copy link

hale commented Oct 16, 2021

I figure you folks have this covered but just in case not, this isn't yet in the changelog for 0.53

c.f. https://github.com/DataDog/dd-trace-rb/pull/1711/files

@ivoanjo
Copy link
Member

ivoanjo commented Oct 18, 2021

Thanks @hale for the note! We usually filter down all items in the milestone when writing the release notes, and in this case we filtered a bit too much 😅 .

I'll open a PR to add this item to the 0.53.0 changelog, as I agree with you that this definitely deserves being there. Thanks @ericmustin for the great work ;)

ivoanjo added a commit that referenced this pull request Oct 18, 2021
This was added in #1280 and it's definitely relevant enough to include
in the changelog :)
ivoanjo added a commit that referenced this pull request Oct 18, 2021
This was added in #1280 and it's definitely relevant enough to include
in the changelog :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Involves a product feature integrations Involves tracing integrations
Projects
Planning
Long Term Priority
Development

Successfully merging this pull request may close these issues.

None yet

6 participants