Skip to content

Commit

Permalink
Chapter 3: Implement current attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Apr 4, 2020
1 parent 156d8f7 commit b1f1847
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
31 changes: 31 additions & 0 deletions lib/active_monitoring/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative "core_extensions/action_controller"
require_relative "core_extensions/active_record"
require_relative "sql_query"
require_relative "current"

module ActiveMonitoring
class Current
class << self
def request_id
store[:request_id]
end

def request_id=(value)
store[:request_id] = value
end

def location
store[:location]
end

def location=(value)
store[:location] = value
end

def store
Thread.current[:active_monitoring_store] ||= {}
Thread.current[:active_monitoring_store]
end
end
end
end
8 changes: 8 additions & 0 deletions lib/active_monitoring/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative "core_extensions/action_controller"
require_relative "core_extensions/active_record"
require_relative "sql_query"
require_relative "current"

module ActiveMonitoring
class Engine < ::Rails::Engine
Expand All @@ -13,6 +14,11 @@ class Engine < ::Rails::Engine
ActiveSupport.on_load(:action_controller) do
ActionController::Base.prepend(::ActiveMonitoring::CoreExtensions::ActionController::Instrumentation)

ActiveMonitoring::Notifications.subscribe("start_processing.action_controller") do |_, _, _, _, payload|
Current.request_id = payload[:request_id]
Current.location = "#{payload[:controller]}##{payload[:action]}"
end

ActiveMonitoring::Notifications.subscribe("process_action.action_controller") do |name, start, finish, _id, payload|
Metric.create(
name: name,
Expand All @@ -28,7 +34,9 @@ class Engine < ::Rails::Engine
Metric.create(
name: name,
value: finish - start,
request_id: Current.request_id,
sql_query: payload[:sql],
location: Current.location,
created_at: finish
)
end
Expand Down
25 changes: 3 additions & 22 deletions spec/requests/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@
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))

Expand All @@ -55,7 +34,9 @@
name: "sql.active_record",
value: 0,
created_at: Time.zone.local(2020, 1, 1),
sql_query: %(INSERT INTO "books" ("name", "created_at", "updated_at") VALUES (?, ?, ?))
sql_query: %(INSERT INTO "books" ("name", "created_at", "updated_at") VALUES (?, ?, ?)),
request_id: response.headers["X-Request-Id"],
location: "BooksController#create"
)
)
end
Expand Down

0 comments on commit b1f1847

Please sign in to comment.