Skip to content

Commit ea436ac

Browse files
committed
tests: add test that execute an ActionMailer delivery
1 parent 0c27760 commit ea436ac

File tree

3 files changed

+191
-13
lines changed

3 files changed

+191
-13
lines changed

instrumentation/action_mailer/lib/opentelemetry/instrumentation/action_mailer/railtie.rb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@ module ActionMailer
1414
class Railtie < ::Rails::Railtie
1515
config.after_initialize do
1616
::OpenTelemetry::Instrumentation::ActiveSupport::Instrumentation.instance.install({})
17+
subscribe_to_deliver
18+
subscribe_to_process
19+
end
1720

18-
config = ActionMailer::Instrumentation.instance.config
21+
class << self
22+
def subscribe_to_deliver
23+
::OpenTelemetry::Instrumentation::ActiveSupport.subscribe(
24+
ActionMailer::Instrumentation.instance.tracer,
25+
DELIVER_SUBSCRIPTION,
26+
config[:notification_payload_transform],
27+
config[:disallowed_notification_payload_keys]
28+
)
29+
end
1930

20-
::OpenTelemetry::Instrumentation::ActiveSupport.subscribe(
21-
ActionMailer::Instrumentation.instance.tracer,
22-
DELIVER_SUBSCRIPTION,
23-
config[:notification_payload_transform],
24-
config[:disallowed_notification_payload_keys]
25-
)
31+
def subscribe_to_process
32+
::OpenTelemetry::Instrumentation::ActiveSupport.subscribe(
33+
ActionMailer::Instrumentation.instance.tracer,
34+
PROCESS_SUBSCRIPTION,
35+
config[:process_payload_transform],
36+
config[:disallowed_process_payload_keys]
37+
)
38+
end
2639

27-
::OpenTelemetry::Instrumentation::ActiveSupport.subscribe(
28-
ActionMailer::Instrumentation.instance.tracer,
29-
PROCESS_SUBSCRIPTION,
30-
config[:process_payload_transform],
31-
config[:disallowed_process_payload_keys]
32-
)
40+
def config
41+
ActionMailer::Instrumentation.instance.config
42+
end
3343
end
3444
end
3545
end
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'test_helper'
8+
require 'opentelemetry-instrumentation-active_support'
9+
10+
class Proc
11+
def inspect
12+
'A proc'
13+
end
14+
end
15+
16+
describe OpenTelemetry::Instrumentation::ActionMailer do
17+
let(:exporter) { EXPORTER }
18+
let(:spans) { exporter.finished_spans }
19+
let(:instrumentation) { OpenTelemetry::Instrumentation::ActionMailer::Instrumentation.instance }
20+
21+
before do
22+
exporter.reset
23+
end
24+
25+
describe 'deliver.action_mailer' do
26+
describe 'with default configuration' do
27+
it 'generates a deliver span' do
28+
subscribing_to_deliver do
29+
TestMailer.hello_world.deliver_now
30+
end
31+
32+
_(spans.length).must_equal(1)
33+
span = spans.find { |s| s.name == 'deliver.action_mailer' }
34+
35+
_(span).wont_be_nil
36+
37+
_(span.attributes['email.x_mailer']).must_equal('TestMailer')
38+
_(span.attributes['email.subject']).must_equal('Hello world')
39+
_(span.attributes['email.message_id']).wont_be_empty
40+
end
41+
end
42+
43+
describe 'with custom configuration' do
44+
it 'with email_address: :include' do
45+
with_configuration(email_address: :include, disallowed_notification_payload_keys: []) do
46+
subscribing_to_deliver do
47+
TestMailer.hello_world.deliver_now
48+
end
49+
end
50+
51+
_(spans.length).must_equal(1)
52+
span = spans.find { |s| s.name == 'deliver.action_mailer' }
53+
54+
_(span).wont_be_nil
55+
56+
_(span.attributes['email.x_mailer']).must_equal('TestMailer')
57+
_(span.attributes['email.subject']).must_equal('Hello world')
58+
_(span.attributes['email.message_id']).wont_be_empty
59+
_(span.attributes['email.to.address']).must_equal(['[email protected]'])
60+
_(span.attributes['email.from.address']).must_equal(['[email protected]'])
61+
_(span.attributes['email.cc.address']).must_equal(['[email protected]'])
62+
_(span.attributes['email.bcc.address']).must_equal(['[email protected]'])
63+
end
64+
65+
it 'with a custom transform proc' do
66+
transform = ->(payload) { payload.transform_keys(&:upcase) }
67+
with_configuration(notification_payload_transform: transform) do
68+
instrumentation.send(:ecs_mail_convention)
69+
subscribing_to_deliver do
70+
TestMailer.hello_world.deliver_now
71+
end
72+
end
73+
74+
_(spans.length).must_equal(1)
75+
span = spans.find { |s| s.name == 'deliver.action_mailer' }
76+
77+
_(span).wont_be_nil
78+
79+
_(span.attributes['EMAIL.X_MAILER']).must_equal('TestMailer')
80+
_(span.attributes['EMAIL.SUBJECT']).must_equal('Hello world')
81+
_(span.attributes['EMAIL.MESSAGE_ID']).wont_be_empty
82+
end
83+
end
84+
end
85+
86+
describe 'process.action_mailer' do
87+
describe 'with default configuration' do
88+
it 'generates a process span' do
89+
transform = ->(payload) { payload.transform_keys(&:upcase) }
90+
with_configuration(disallowed_process_payload_keys: [:ARGS], process_payload_transform: transform) do
91+
subscribing_to_process do
92+
TestMailer.hello_world('Hola mundo').deliver_now
93+
end
94+
end
95+
96+
_(spans.length).must_equal(1)
97+
span = spans.find { |s| s.name == 'process.action_mailer' }
98+
99+
_(span).wont_be_nil
100+
101+
_(span.attributes['MAILER']).must_equal('TestMailer')
102+
_(span.attributes['ACTION']).must_equal('hello_world')
103+
_(span.attributes['ARGS']).must_be_nil
104+
end
105+
end
106+
107+
describe 'with custom configuration' do
108+
it 'generates a process span' do
109+
subscribing_to_process do
110+
TestMailer.hello_world('Hola mundo').deliver_now
111+
end
112+
113+
_(spans.length).must_equal(1)
114+
span = spans.find { |s| s.name == 'process.action_mailer' }
115+
116+
_(span).wont_be_nil
117+
118+
_(span.attributes['mailer']).must_equal('TestMailer')
119+
_(span.attributes['action']).must_equal('hello_world')
120+
_(span.attributes['args']).must_equal(['Hola mundo'])
121+
end
122+
end
123+
end
124+
125+
def with_configuration(values, &block)
126+
original_config = instrumentation.instance_variable_get(:@config)
127+
modified_config = original_config.merge(values)
128+
instrumentation.instance_variable_set(:@config, modified_config)
129+
130+
yield
131+
132+
instrumentation.instance_variable_set(:@config, original_config)
133+
end
134+
135+
def subscribing_to_deliver(&block)
136+
subscription = OpenTelemetry::Instrumentation::ActionMailer::Railtie.subscribe_to_deliver
137+
yield
138+
ensure
139+
ActiveSupport::Notifications.unsubscribe(subscription)
140+
end
141+
142+
def subscribing_to_process(&block)
143+
subscription = OpenTelemetry::Instrumentation::ActionMailer::Railtie.subscribe_to_process
144+
yield
145+
ensure
146+
ActiveSupport::Notifications.unsubscribe(subscription)
147+
end
148+
end

instrumentation/action_mailer/test/test_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@
2222
c.use 'OpenTelemetry::Instrumentation::ActionMailer'
2323
c.add_span_processor span_processor
2424
end
25+
26+
OpenTelemetry::Instrumentation::ActiveSupport::Instrumentation.instance.install({})
27+
OpenTelemetry::Instrumentation::ActionMailer::Instrumentation.instance.install({})
28+
29+
ActionMailer::Base.delivery_method = :test
30+
31+
class TestMailer < ActionMailer::Base
32+
33+
34+
35+
36+
37+
def hello_world(message = 'Hello world')
38+
@message = message
39+
mail from: FROM, to: TO, cc: CC, bcc: BCC do |format|
40+
format.html { render inline: '<h1><%= @message %></h1>' }
41+
format.text { render inline: '<%= @message %>' }
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)