-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from hennevogel/feature/action_mailer
Introduces ActionMailer subscriber
- Loading branch information
Showing
14 changed files
with
131 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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: "[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 | ||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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: "[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 | ||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= yield %> |