Skip to content

Commit

Permalink
Merge pull request #129 from hennevogel/feature/action_mailer
Browse files Browse the repository at this point in the history
Introduces ActionMailer subscriber
  • Loading branch information
hennevogel authored Sep 12, 2020
2 parents 8ade32a + 3d34e2a commit bf05e17
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 22 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ rvm:
- "2.7"
- "2.6"
- "2.5"
- "2.4"
gemfile:
- gemfiles/Gemfile.rails-6.0.x
- gemfiles/Gemfile.rails-5.2.x
- gemfiles/Gemfile.rails-5.1.x
- gemfiles/Gemfile.rails-5.0.x
env:
- TEST_TASK=spec
matrix:
allow_failures:
- rvm: ruby-head
include:
- { rvm: "2.6", gemfile: "Gemfile", env: [TEST_TASK=rubocop] }
exclude:
# Rails > 5 not on MRI 2.4+
- { rvm: "2.4", gemfile: "gemfiles/Gemfile.rails-6.0.x" }
addons:
apt:
packages:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

For the full commit log, [see here](https://github.com/influxdata/influxdb-rails/commits/master).

## v1.0.1.beta2
- Implement [`deliver.action_mailer`](https://guides.rubyonrails.org/active_support_instrumentation.html#deliver-action-mailer) subscriber
- Add missing Active Job documentation
- Drop support for Ruby 2.4
- Drop support for Rails < 5.2

## v1.0.1.beta1, released 2020-08-21
- Drop support for Ruby 2.3
- Drop support for Rails 4.x
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ Reported tags:
*Note*: Only the measurements with the hook `perform` report a duration in the value.
The other hooks are counters and always report a value of `1`.

### Action Mailer

Reported ActiveSupport instrumentation hooks:

- [deliver.action\_mailer](https://guides.rubyonrails.org/active_support_instrumentation.html#deliver-action-mailer)

Reported values:

```ruby
value: 1
```

Reported tags:

```ruby
hook: "deliver",
mailer: "SomeMailerClassName"
```

*Note*: The hook is just a counter and always report a value of `1`.

## Configuration

The only setting you actually need to configure is the name of the database
Expand Down
9 changes: 0 additions & 9 deletions gemfiles/Gemfile.rails-5.0.x

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/Gemfile.rails-5.1.x

This file was deleted.

1 change: 1 addition & 0 deletions influxdb-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "influxdb", "~> 0.6", ">= 0.6.4"
spec.add_runtime_dependency "railties", ">= 5.0"

spec.add_development_dependency "actionmailer"
spec.add_development_dependency "activejob"
spec.add_development_dependency "activerecord"
spec.add_development_dependency "bundler", ">= 1.0.0"
Expand Down
1 change: 1 addition & 0 deletions lib/influxdb-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "influxdb/rails/middleware/sql_subscriber"
require "influxdb/rails/middleware/active_record_subscriber"
require "influxdb/rails/middleware/active_job_subscriber"
require "influxdb/rails/middleware/action_mailer_subscriber"
require "influxdb/rails/sql/query"
require "influxdb/rails/version"
require "influxdb/rails/configuration"
Expand Down
22 changes: 22 additions & 0 deletions lib/influxdb/rails/middleware/action_mailer_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "influxdb/rails/middleware/subscriber"

module InfluxDB
module Rails
module Middleware
class ActionMailerSubscriber < Subscriber # :nodoc:
private

def values
{ value: 1 }
end

def tags
{
hook: "deliver",
mailer: payload[:mailer],
}
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/influxdb/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Railtie < ::Rails::Railtie # :nodoc:
"enqueue.active_job" => Middleware::ActiveJobSubscriber,
"perform_start.active_job" => Middleware::ActiveJobSubscriber,
"perform.active_job" => Middleware::ActiveJobSubscriber,
"deliver.action_mailer" => Middleware::ActionMailerSubscriber,
"block_instrumentation.influxdb_rails" => Middleware::BlockInstrumentationSubscriber,
}.each do |hook_name, subscriber|
ActiveSupport::Notifications.subscribe(hook_name, subscriber)
Expand Down
49 changes: 49 additions & 0 deletions spec/requests/action_mailer_deliver_metrics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require File.dirname(__FILE__) + "/../spec_helper"

RSpec.describe "ActionMailer deliver metrics", type: :request do
let(:tags_middleware) do
lambda do |tags|
tags.merge(tags_middleware: :tags_middleware)
end
end
before do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_environments).and_return(%w[development])
allow_any_instance_of(ActionDispatch::Request).to receive(:request_id).and_return(:request_id)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:application_name).and_return(:app_name)
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:tags_middleware).and_return(tags_middleware)
end

it "writes metric" do
get "/metrics"

expect_metric(
tags: a_hash_including(
hook: "deliver",
mailer: "MetricMailer",
location: "MetricsController#index",
additional_tag: :value,
server: Socket.gethostname,
app_name: :app_name,
tags_middleware: :tags_middleware
),
values: a_hash_including(
additional_value: :value,
request_id: :request_id,
value: 1
)
)
end

it "does not write metric when hook is ignored" do
allow_any_instance_of(InfluxDB::Rails::Configuration).to receive(:ignored_hooks).and_return(["deliver.action_mailer"])

get "/metrics"

expect_no_metric(
tags: a_hash_including(
hook: "deliver",
mailer: "MetricMailer"
)
)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@

config.include ActiveSupport::Testing::TimeHelpers
config.include ActiveJob::TestHelper

config.include InfluxDB::Rails::BrokenClient
end
14 changes: 14 additions & 0 deletions spec/support/rails5/app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "action_controller/railtie"
require "active_record/railtie"
require "active_job"
require "action_mailer"

app = Class.new(Rails::Application)
app.config.secret_key_base = "1234567890abcdef1234567890abcdef"
Expand All @@ -11,6 +12,7 @@
app.config.root = __dir__
Rails.backtrace_cleaner.remove_silencers!
ActiveJob::Base.logger = Rails.logger
ActionMailer::Base.delivery_method = :test
app.initialize!

app.routes.draw do
Expand Down Expand Up @@ -38,6 +40,17 @@ def perform
end
end

class MetricMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"

def welcome_mail
mail(to: "[email protected]", subject: "Welcome to metrics!") do |format|
format.text { render plain: "Hello Dieter!" }
end
end
end

class Metric < ActiveRecord::Base; end
class ApplicationController < ActionController::Base; end
class MetricsController < ApplicationController
Expand All @@ -53,6 +66,7 @@ def index
1 + 1
end
MetricJob.perform_later
MetricMailer.with(user: "eisendieter").welcome_mail.deliver_now
Metric.create!(name: "name")
end

Expand Down
14 changes: 14 additions & 0 deletions spec/support/rails6/app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "action_controller/railtie"
require "active_record/railtie"
require "active_job"
require "action_mailer"

app = Class.new(Rails::Application)
app.config.secret_key_base = "1234567890abcdef1234567890abcdef"
Expand All @@ -11,6 +12,7 @@
app.config.root = __dir__
Rails.backtrace_cleaner.remove_silencers!
ActiveJob::Base.logger = Rails.logger
ActionMailer::Base.delivery_method = :test
app.initialize!

app.routes.draw do
Expand Down Expand Up @@ -38,6 +40,17 @@ def perform
end
end

class MetricMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"

def welcome_mail
mail(to: "[email protected]", subject: "Welcome to metrics!") do |format|
format.text { render plain: "Hello Dieter!" }
end
end
end

class Metric < ActiveRecord::Base; end
class ApplicationController < ActionController::Base; end
class MetricsController < ApplicationController
Expand All @@ -53,6 +66,7 @@ def index
1 + 1
end
MetricJob.perform_later
MetricMailer.with(user: "eisendieter").welcome_mail.deliver_now
Metric.create!(name: "name")
end

Expand Down
1 change: 1 addition & 0 deletions spec/support/views/layouts/mailer.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>

0 comments on commit bf05e17

Please sign in to comment.