Module: Upkeep::Rails::ControllerRuntime
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/upkeep/rails/controller_runtime.rb
Constant Summary collapse
- SUPPRESS_KEY =
:upkeep_rails_controller_runtime_suppressed
Class Method Summary collapse
- .install ⇒ Object
- .installed? ⇒ Boolean
- .measure_phase(payload, key) ⇒ Object
- .record_capture_payload(payload, capture) ⇒ Object
- .request_capture_profile? ⇒ Boolean
- .reset! ⇒ Object
- .suppress ⇒ Object
- .suppressed? ⇒ Boolean
- .upkeep_capture_request(&action) ⇒ Object
- .upkeep_capture_request_with_timing(action, payload) ⇒ Object
- .upkeep_subscription_request? ⇒ Boolean
Instance Method Summary collapse
-
#upkeep_reactive_request? ⇒ Boolean
Public override point for host controllers.
Class Method Details
.install ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 28 def install return if @installed return unless defined?(::ActionController::Base) ::ActionController::Base.include(self) @installed = true end |
.installed? ⇒ Boolean
36 37 38 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 36 def installed? !!@installed end |
.measure_phase(payload, key) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 138 def measure_phase(payload, key) started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield ensure payload[key] = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000.0).round(3) end |
.record_capture_payload(payload, capture) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 121 def record_capture_payload(payload, capture) payload[:response_status] = capture.response_status payload[:response_content_type] = capture.response_content_type payload[:response_media_type] = capture.response_media_type payload[:html_response] = capture.html_response? payload[:response_successful] = capture.successful? payload[:html_bytes] = capture.html.bytesize payload[:graph_frames] = capture.recorder.graph.frame_nodes.size payload[:graph_dependencies] = capture.recorder.graph.dependency_nodes.size capture.timings.each do |phase, ms| payload[:"capture_#{phase}"] = ms end capture.counters.each do |counter, value| payload[:"capture_#{counter}"] = value end end |
.request_capture_profile? ⇒ Boolean
149 150 151 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 149 def request_capture_profile? ActiveSupport::Notifications.notifier.listening?(Upkeep::Rails::REQUEST_CAPTURE) end |
.reset! ⇒ Object
40 41 42 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 40 def reset! @installed = false end |
.suppress ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 44 def suppress previous = Thread.current[SUPPRESS_KEY] Thread.current[SUPPRESS_KEY] = true yield ensure Thread.current[SUPPRESS_KEY] = previous end |
.suppressed? ⇒ Boolean
52 53 54 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 52 def suppressed? Thread.current[SUPPRESS_KEY] end |
.upkeep_capture_request(&action) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 58 def upkeep_capture_request(&action) return action.call if ControllerRuntime.suppressed? return action.call if Upkeep::Runtime::Observation.recorder payload = { controller: self.class.name, action: action_name, method: request.request_method, path: request.fullpath, subscription_request: upkeep_subscription_request? } ActiveSupport::Notifications.instrument(Upkeep::Rails::REQUEST_CAPTURE, payload) do upkeep_capture_request_with_timing(action, payload) end end |
.upkeep_capture_request_with_timing(action, payload) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 74 def upkeep_capture_request_with_timing(action, payload) measure_phase(payload, :deliver_pending_ms) { Upkeep::Rails.deliver_changes_now! } result = nil capture = nil changes = [] measure_phase(payload, :change_capture_ms) do _captured, changes = Upkeep::Runtime::ChangeLog.capture do if payload.fetch(:subscription_request) capture = Upkeep::Capture::Request.call(self, profile: request_capture_profile?) { action.call } result = capture.action_result else measure_phase(payload, :action_ms) { result = action.call } end end end record_capture_payload(payload, capture) if capture measure_phase(payload, :deliver_changes_ms) do if capture Upkeep::Rails.deliver_changes_now!(changes) else Upkeep::Rails.deliver_changes!(changes) end end registration = nil if capture measure_phase(payload, :register_ms) do registration = Upkeep::Rails.register_controller_subscription(self, capture) end end payload[:registered] = !!registration if capture && registration measure_phase(payload, :inject_ms) do response.body = Upkeep::Rails::ClientSubscription.inject( capture.html, identity: registration.identity, subscription: registration.subscription ) end payload[:subscription_id] = registration.subscription.id end result end |
.upkeep_subscription_request? ⇒ Boolean
145 146 147 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 145 def upkeep_subscription_request? upkeep_reactive_request? && (request.get? || request.head?) end |
Instance Method Details
#upkeep_reactive_request? ⇒ Boolean
Public override point for host controllers. Return false to opt a request out of Upkeep capture and live registration: the action still runs and the page renders normally, but no subscription is recorded, no source is injected, and no boundary is analyzed (so an opaque relation on that request neither raises nor warns). Use for render paths Upkeep cannot prove and that should not be made reactive, e.g. full-text search results backed by raw tsvector SQL.
22 23 24 |
# File 'lib/upkeep/rails/controller_runtime.rb', line 22 def upkeep_reactive_request? true end |