-
Notifications
You must be signed in to change notification settings - Fork 226
feat: subscribe to process.action_mailer notifications #1185
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
Merged
kaylareopelle
merged 7 commits into
open-telemetry:main
from
mrsimo:instrumentation-actionmailer-process
Oct 22, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
305b421
feat: subscribe to process.action_mailer notifications
mrsimo 8bdc55e
doc: add payload table for `process.action_mailer` event
mrsimo 0c27760
doc: expand ActionMailer::Instrumentation class docs
mrsimo 132864f
tests: add test that execute an ActionMailer delivery
mrsimo 8fb167c
tests: disable warnings
mrsimo 341fb84
Merge branch 'main' into instrumentation-actionmailer-process
mrsimo e9457d2
Merge branch 'main' into instrumentation-actionmailer-process
kaylareopelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,24 +7,39 @@ | |
| module OpenTelemetry | ||
| module Instrumentation | ||
| module ActionMailer | ||
| SUBSCRIPTIONS = %w[ | ||
| deliver.action_mailer | ||
| ].freeze | ||
| DELIVER_SUBSCRIPTION = 'deliver.action_mailer' | ||
| PROCESS_SUBSCRIPTION = 'process.action_mailer' | ||
|
|
||
| # This Railtie sets up subscriptions to relevant ActionMailer notifications | ||
| class Railtie < ::Rails::Railtie | ||
| config.after_initialize do | ||
| ::OpenTelemetry::Instrumentation::ActiveSupport::Instrumentation.instance.install({}) | ||
| subscribe_to_deliver | ||
| subscribe_to_process | ||
| end | ||
|
|
||
| SUBSCRIPTIONS.each do |subscription_name| | ||
| config = ActionMailer::Instrumentation.instance.config | ||
| class << self | ||
| def subscribe_to_deliver | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved these bits into class methods that the railtie callback calls. This way I can call them from the tests to subscribe (and unsubscribe) with different configurations. |
||
| ::OpenTelemetry::Instrumentation::ActiveSupport.subscribe( | ||
| ActionMailer::Instrumentation.instance.tracer, | ||
| subscription_name, | ||
| DELIVER_SUBSCRIPTION, | ||
| config[:notification_payload_transform], | ||
| config[:disallowed_notification_payload_keys] | ||
| ) | ||
| end | ||
|
|
||
| def subscribe_to_process | ||
| ::OpenTelemetry::Instrumentation::ActiveSupport.subscribe( | ||
| ActionMailer::Instrumentation.instance.tracer, | ||
| PROCESS_SUBSCRIPTION, | ||
| config[:process_payload_transform], | ||
| config[:disallowed_process_payload_keys] | ||
| ) | ||
| end | ||
|
|
||
| def config | ||
| ActionMailer::Instrumentation.instance.config | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
142 changes: 142 additions & 0 deletions
142
...ation/action_mailer/test/opentelemetry/instrumentation/action_mailer/subscription_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require 'test_helper' | ||
| require 'opentelemetry-instrumentation-active_support' | ||
|
|
||
| describe OpenTelemetry::Instrumentation::ActionMailer do | ||
| let(:exporter) { EXPORTER } | ||
| let(:spans) { exporter.finished_spans } | ||
| let(:instrumentation) { OpenTelemetry::Instrumentation::ActionMailer::Instrumentation.instance } | ||
|
|
||
| before do | ||
| exporter.reset | ||
| end | ||
|
|
||
| describe 'deliver.action_mailer' do | ||
| describe 'with default configuration' do | ||
| it 'generates a deliver span' do | ||
| subscribing_to_deliver do | ||
| TestMailer.hello_world.deliver_now | ||
| end | ||
|
|
||
| _(spans.length).must_equal(1) | ||
| span = spans.find { |s| s.name == 'deliver.action_mailer' } | ||
|
|
||
| _(span).wont_be_nil | ||
|
|
||
| _(span.attributes['email.x_mailer']).must_equal('TestMailer') | ||
| _(span.attributes['email.subject']).must_equal('Hello world') | ||
| _(span.attributes['email.message_id']).wont_be_empty | ||
| end | ||
| end | ||
|
|
||
| describe 'with custom configuration' do | ||
| it 'with email_address: :include' do | ||
| with_configuration(email_address: :include, disallowed_notification_payload_keys: []) do | ||
| subscribing_to_deliver do | ||
| TestMailer.hello_world.deliver_now | ||
| end | ||
| end | ||
|
|
||
| _(spans.length).must_equal(1) | ||
| span = spans.find { |s| s.name == 'deliver.action_mailer' } | ||
|
|
||
| _(span).wont_be_nil | ||
|
|
||
| _(span.attributes['email.x_mailer']).must_equal('TestMailer') | ||
| _(span.attributes['email.subject']).must_equal('Hello world') | ||
| _(span.attributes['email.message_id']).wont_be_empty | ||
| _(span.attributes['email.to.address']).must_equal(['[email protected]']) | ||
| _(span.attributes['email.from.address']).must_equal(['[email protected]']) | ||
| _(span.attributes['email.cc.address']).must_equal(['[email protected]']) | ||
| _(span.attributes['email.bcc.address']).must_equal(['[email protected]']) | ||
| end | ||
|
|
||
| it 'with a custom transform proc' do | ||
| transform = ->(payload) { payload.transform_keys(&:upcase) } | ||
| with_configuration(notification_payload_transform: transform) do | ||
| instrumentation.send(:ecs_mail_convention) | ||
| subscribing_to_deliver do | ||
| TestMailer.hello_world.deliver_now | ||
| end | ||
| end | ||
|
|
||
| _(spans.length).must_equal(1) | ||
| span = spans.find { |s| s.name == 'deliver.action_mailer' } | ||
|
|
||
| _(span).wont_be_nil | ||
|
|
||
| _(span.attributes['EMAIL.X_MAILER']).must_equal('TestMailer') | ||
| _(span.attributes['EMAIL.SUBJECT']).must_equal('Hello world') | ||
| _(span.attributes['EMAIL.MESSAGE_ID']).wont_be_empty | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'process.action_mailer' do | ||
| describe 'with default configuration' do | ||
| it 'generates a process span' do | ||
| transform = ->(payload) { payload.transform_keys(&:upcase) } | ||
| with_configuration(disallowed_process_payload_keys: [:ARGS], process_payload_transform: transform) do | ||
| subscribing_to_process do | ||
| TestMailer.hello_world('Hola mundo').deliver_now | ||
| end | ||
| end | ||
|
|
||
| _(spans.length).must_equal(1) | ||
| span = spans.find { |s| s.name == 'process.action_mailer' } | ||
|
|
||
| _(span).wont_be_nil | ||
|
|
||
| _(span.attributes['MAILER']).must_equal('TestMailer') | ||
| _(span.attributes['ACTION']).must_equal('hello_world') | ||
| _(span.attributes['ARGS']).must_be_nil | ||
| end | ||
| end | ||
|
|
||
| describe 'with custom configuration' do | ||
| it 'generates a process span' do | ||
| subscribing_to_process do | ||
| TestMailer.hello_world('Hola mundo').deliver_now | ||
| end | ||
|
|
||
| _(spans.length).must_equal(1) | ||
| span = spans.find { |s| s.name == 'process.action_mailer' } | ||
|
|
||
| _(span).wont_be_nil | ||
|
|
||
| _(span.attributes['mailer']).must_equal('TestMailer') | ||
| _(span.attributes['action']).must_equal('hello_world') | ||
| _(span.attributes['args']).must_equal(['Hola mundo']) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def with_configuration(values, &block) | ||
| original_config = instrumentation.instance_variable_get(:@config) | ||
| modified_config = original_config.merge(values) | ||
| instrumentation.instance_variable_set(:@config, modified_config) | ||
|
|
||
| yield | ||
|
|
||
| instrumentation.instance_variable_set(:@config, original_config) | ||
| end | ||
|
|
||
| def subscribing_to_deliver(&block) | ||
| subscription = OpenTelemetry::Instrumentation::ActionMailer::Railtie.subscribe_to_deliver | ||
| yield | ||
| ensure | ||
| ActiveSupport::Notifications.unsubscribe(subscription) | ||
| end | ||
|
|
||
| def subscribing_to_process(&block) | ||
| subscription = OpenTelemetry::Instrumentation::ActionMailer::Railtie.subscribe_to_process | ||
| yield | ||
| ensure | ||
| ActiveSupport::Notifications.unsubscribe(subscription) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,23 @@ | |
| c.use 'OpenTelemetry::Instrumentation::ActionMailer' | ||
| c.add_span_processor span_processor | ||
| end | ||
|
|
||
| OpenTelemetry::Instrumentation::ActiveSupport::Instrumentation.instance.install({}) | ||
| OpenTelemetry::Instrumentation::ActionMailer::Instrumentation.instance.install({}) | ||
|
|
||
| ActionMailer::Base.delivery_method = :test | ||
|
|
||
| class TestMailer < ActionMailer::Base | ||
| FROM = '[email protected]' | ||
| TO = '[email protected]' | ||
| CC = '[email protected]' | ||
| BCC = '[email protected]' | ||
|
|
||
| def hello_world(message = 'Hello world') | ||
| @message = message | ||
| mail from: FROM, to: TO, cc: CC, bcc: BCC do |format| | ||
| format.html { render inline: '<h1><%= @message %></h1>' } | ||
| format.text { render inline: '<%= @message %>' } | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty of adding this, when adding the new tests that do ActionMailer deliveries the
mailgems generates lots of warnings. I saw other instrumentation libraries already had it.