diff --git a/.travis.yml b/.travis.yml index 3524135..2edc8ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,9 @@ 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: @@ -23,9 +20,6 @@ matrix: - 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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ffdb9..8d4e372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ddb96f0..fe25339 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/gemfiles/Gemfile.rails-5.0.x b/gemfiles/Gemfile.rails-5.0.x deleted file mode 100644 index cca77eb..0000000 --- a/gemfiles/Gemfile.rails-5.0.x +++ /dev/null @@ -1,9 +0,0 @@ -source "https://rubygems.org" - -gem 'actionpack', '~> 5.0.0' -gem 'activesupport', '~> 5.0.0' -gem 'activemodel', '~> 5.0.0' -gem 'sqlite3', '1.3.13' - - -gemspec :path => '../' diff --git a/gemfiles/Gemfile.rails-5.1.x b/gemfiles/Gemfile.rails-5.1.x deleted file mode 100644 index 167e894..0000000 --- a/gemfiles/Gemfile.rails-5.1.x +++ /dev/null @@ -1,7 +0,0 @@ -source "https://rubygems.org" - -gem 'actionpack', '~> 5.1.0' -gem 'activesupport', '~> 5.1.0' -gem 'activemodel', '~> 5.1.0' - -gemspec :path => '../' diff --git a/influxdb-rails.gemspec b/influxdb-rails.gemspec index 3d54277..630850b 100644 --- a/influxdb-rails.gemspec +++ b/influxdb-rails.gemspec @@ -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" diff --git a/lib/influxdb-rails.rb b/lib/influxdb-rails.rb index 8348781..54d06e9 100644 --- a/lib/influxdb-rails.rb +++ b/lib/influxdb-rails.rb @@ -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" diff --git a/lib/influxdb/rails/middleware/action_mailer_subscriber.rb b/lib/influxdb/rails/middleware/action_mailer_subscriber.rb new file mode 100644 index 0000000..8100c2e --- /dev/null +++ b/lib/influxdb/rails/middleware/action_mailer_subscriber.rb @@ -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 diff --git a/lib/influxdb/rails/railtie.rb b/lib/influxdb/rails/railtie.rb index 17677a5..cd7ab6f 100644 --- a/lib/influxdb/rails/railtie.rb +++ b/lib/influxdb/rails/railtie.rb @@ -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) diff --git a/spec/requests/action_mailer_deliver_metrics_spec.rb b/spec/requests/action_mailer_deliver_metrics_spec.rb new file mode 100644 index 0000000..dec0cfa --- /dev/null +++ b/spec/requests/action_mailer_deliver_metrics_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e688f33..2dfd6b1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -35,5 +35,6 @@ config.include ActiveSupport::Testing::TimeHelpers config.include ActiveJob::TestHelper + config.include InfluxDB::Rails::BrokenClient end diff --git a/spec/support/rails5/app.rb b/spec/support/rails5/app.rb index f061bf6..013b4ce 100644 --- a/spec/support/rails5/app.rb +++ b/spec/support/rails5/app.rb @@ -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" @@ -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 @@ -38,6 +40,17 @@ def perform end end +class MetricMailer < ActionMailer::Base + default from: "from@example.com" + layout "mailer" + + def welcome_mail + mail(to: "eisendieter@werder.de", 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 @@ -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 diff --git a/spec/support/rails6/app.rb b/spec/support/rails6/app.rb index f061bf6..013b4ce 100644 --- a/spec/support/rails6/app.rb +++ b/spec/support/rails6/app.rb @@ -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" @@ -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 @@ -38,6 +40,17 @@ def perform end end +class MetricMailer < ActionMailer::Base + default from: "from@example.com" + layout "mailer" + + def welcome_mail + mail(to: "eisendieter@werder.de", 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 @@ -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 diff --git a/spec/support/views/layouts/mailer.txt.erb b/spec/support/views/layouts/mailer.txt.erb new file mode 100644 index 0000000..37f0bdd --- /dev/null +++ b/spec/support/views/layouts/mailer.txt.erb @@ -0,0 +1 @@ +<%= yield %>