|
| 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 |
0 commit comments