Skip to content

Commit

Permalink
Chapter 1: Instrument controller actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Apr 4, 2020
1 parent 32c32dc commit ef200ae
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/active_monitoring/core_extensions/action_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ActiveMonitoring
module CoreExtensions
module ActionController
module Instrumentation
def process_action(*)
payload = {
controller: self.class.name,
action: action_name,
request_id: request.uuid
}
::ActiveMonitoring::Notifications.instrument("start_processing.action_controller", payload)
::ActiveMonitoring::Notifications.instrument("process_action.action_controller", payload) do
super
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/active_monitoring/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require_relative "core_extensions/action_controller"
module ActiveMonitoring
class Engine < ::Rails::Engine
isolate_namespace ActiveMonitoring

ActiveSupport.on_load(:action_controller) do
ActionController::Base.prepend(::ActiveMonitoring::CoreExtensions::ActionController::Instrumentation)
end
end
end
47 changes: 45 additions & 2 deletions spec/requests/metrics_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
require "rails_helper"

RSpec.describe "ActionController metrics", type: :request do
it "works" do
scenario "is succesful" do
post books_path

expect(response.status).to be 200
expect(response).to be_successful
expect(response.body).to eq("ok")
end

scenario "Instruments start_processing.action_controller" do
travel_to(Time.zone.local(2020, 1, 1))
events = []
ActiveMonitoring::Notifications.subscribe("start_processing.action_controller") do |name, start, finish, id, payload|
events << { name: name, start: start, finish: finish, id: id, payload: payload }
end

post books_path

expect(events).to include(
a_hash_including(
name: "start_processing.action_controller",
start: Time.zone.local(2020, 1, 1),
finish: Time.zone.local(2020, 1, 1),
payload: a_hash_including(
request_id: response.headers["X-Request-Id"]
)
)
)
end

scenario "Instruments process_action.action_controller" do
travel_to(Time.zone.local(2020, 1, 1))
events = []
ActiveMonitoring::Notifications.subscribe("process_action.action_controller") do |name, start, finish, id, payload|
events << { name: name, start: start, finish: finish, id: id, payload: payload }
end

post books_path

expect(events).to include(
a_hash_including(
name: "process_action.action_controller",
start: Time.zone.local(2020, 1, 1),
finish: Time.zone.local(2020, 1, 1),
payload: a_hash_including(
request_id: response.headers["X-Request-Id"]
)
)
)
end
end

0 comments on commit ef200ae

Please sign in to comment.