Skip to content

Commit

Permalink
tests: add test that execute an ActionMailer delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimo committed Oct 3, 2024
1 parent 0c27760 commit de43da5
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@ module ActionMailer
class Railtie < ::Rails::Railtie
config.after_initialize do
::OpenTelemetry::Instrumentation::ActiveSupport::Instrumentation.instance.install({})
subscribe_to_deliver
subscribe_to_process
end

config = ActionMailer::Instrumentation.instance.config
private

def subscribe_to_deliver
::OpenTelemetry::Instrumentation::ActiveSupport.subscribe(
ActionMailer::Instrumentation.instance.tracer,
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'
require 'opentelemetry-instrumentation-active_support'

class Proc
def inspect
'A proc'
end
end

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.instance.send(:subscribe_to_deliver)
yield
ensure
ActiveSupport::Notifications.unsubscribe(subscription)
end

def subscribing_to_process(&block)
subscription = OpenTelemetry::Instrumentation::ActionMailer::Railtie.instance.send(:subscribe_to_process)
yield
ensure
ActiveSupport::Notifications.unsubscribe(subscription)
end
end
20 changes: 20 additions & 0 deletions instrumentation/action_mailer/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de43da5

Please sign in to comment.