-
-
Notifications
You must be signed in to change notification settings - Fork 19
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 #167 from ardecvz/instrumentation
Add instrumentation
- Loading branch information
Showing
11 changed files
with
193 additions
and
20 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 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,33 @@ | ||
require "active_support/notifications" | ||
|
||
module Futurism | ||
module Resolver | ||
class Controller | ||
class Instrumentation < SimpleDelegator | ||
PARAMETERS_KEY = ActionDispatch::Http::Parameters::PARAMETERS_KEY | ||
|
||
def render(*args) | ||
ActiveSupport::Notifications.instrument( | ||
"render.futurism", | ||
channel: get_param(:channel), | ||
controller: get_param(:controller), | ||
action: get_param(:action), | ||
partial: extract_partial_name(*args) | ||
) do | ||
super(*args) | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_param(key) | ||
__getobj__.instance_variable_get(:@env).dig(PARAMETERS_KEY, key) | ||
end | ||
|
||
def extract_partial_name(opts_or_model, *args) | ||
opts_or_model.is_a?(Hash) ? opts_or_model[:partial] : opts_or_model.to_partial_path | ||
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
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,69 @@ | ||
require "test_helper" | ||
|
||
class DummyController < ActionController::Base | ||
def name_helper | ||
"FUTURISM".freeze | ||
end | ||
helper_method :name_helper | ||
|
||
def controller_and_action_helper | ||
[params["controller"], params["action"]].join(":") | ||
end | ||
helper_method :controller_and_action_helper | ||
|
||
def name_from_params_helper | ||
params["name"] | ||
end | ||
helper_method :name_from_params_helper | ||
end | ||
|
||
def dummy_connection | ||
connection = Minitest::Mock.new | ||
connection.expect(:env, {"HTTP_VAR" => "HTTP_VAR_VALUE"}) | ||
connection | ||
end | ||
|
||
class Futurism::Resolver::Controller::InstrumentationTest < ActiveSupport::TestCase | ||
test "invokes ActiveSupport instrumentation on the Futurism render" do | ||
swap Futurism, instrumentation: true do | ||
events = [] | ||
ActiveSupport::Notifications.subscribe("render.futurism") do |*args| | ||
events << ActiveSupport::Notifications::Event.new(*args) | ||
end | ||
|
||
renderer = Futurism::Resolver::Controller::Renderer.for( | ||
controller: DummyController, | ||
connection: dummy_connection, | ||
url: "posts/1", | ||
params: {channel: "Futurism::Channel"} | ||
) | ||
post = Post.create title: "Lorem" | ||
renderer.render(partial: "posts/card", locals: {post: post}) | ||
|
||
assert_equal 1, events.size | ||
assert_equal "render.futurism", events.last.name | ||
assert_equal "Futurism::Channel", events.last.payload[:channel] | ||
assert_equal "posts", events.last.payload[:controller] | ||
assert_equal "show", events.last.payload[:action] | ||
assert_equal "posts/card", events.last.payload[:partial] | ||
end | ||
end | ||
|
||
test "does not invoke ActiveSupport instrumentation by default" do | ||
events = [] | ||
ActiveSupport::Notifications.subscribe("render.futurism") do |*args| | ||
events << ActiveSupport::Notifications::Event.new(*args) | ||
end | ||
|
||
renderer = Futurism::Resolver::Controller::Renderer.for( | ||
controller: DummyController, | ||
connection: dummy_connection, | ||
url: "posts/1", | ||
params: {channel: "Futurism::Channel"} | ||
) | ||
post = Post.create title: "Lorem" | ||
renderer.render(partial: "posts/card", locals: {post: post}) | ||
|
||
assert_empty events | ||
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,18 @@ | ||
require "active_support/test_case" | ||
|
||
class ActiveSupport::TestCase | ||
# Execute the block setting the given values and restoring old values after | ||
# the block is executed. | ||
def swap(object, new_values) | ||
old_values = {} | ||
new_values.each do |key, value| | ||
old_values[key] = object.public_send(key) | ||
object.public_send(:"#{key}=", value) | ||
end | ||
yield | ||
ensure | ||
old_values.each do |key, value| | ||
object.public_send(:"#{key}=", value) | ||
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