-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 1: Instrument controller actions
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
lib/active_monitoring/core_extensions/action_controller.rb
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,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 |
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,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 |
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,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 |