Class: Appsignal::Rack::EventHandler
- Includes:
- Rack::Events::Abstract
- Defined in:
- lib/appsignal/rack/event_handler.rb
Overview
Instrumentation event handler for ‘Rack::Events`.
Using this middleware directly with ‘Rack::Events` is deprecated. We recommend using EventMiddleware instead, which is compatible with streaming bodies, in combination with the InstrumentationMiddleware.
This middleware will report the response status code as the ‘response_status` tag on the sample. It will also report the response status as the `response_status` metric.
This middleware will ensure the AppSignal transaction is always completed for every request.
Instance Attribute Summary collapse
- #id ⇒ Object readonly private
- #using_appsignal_event_middleware ⇒ Object writeonly
Class Method Summary collapse
- .safe_execution(name) ⇒ Object private
Instance Method Summary collapse
-
#initialize ⇒ EventHandler
constructor
private
A new instance of EventHandler.
- #on_error(request, _response, error) ⇒ Object private
- #on_finish(request, response) ⇒ Object private
- #on_start(request, _response) ⇒ Object private
- #request_handler?(given_id) ⇒ Boolean private
Constructor Details
#initialize ⇒ EventHandler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of EventHandler.
45 46 47 48 |
# File 'lib/appsignal/rack/event_handler.rb', line 45 def initialize @id = SecureRandom.uuid @using_appsignal_event_middleware = false end |
Instance Attribute Details
#id ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
41 42 43 |
# File 'lib/appsignal/rack/event_handler.rb', line 41 def id @id end |
#using_appsignal_event_middleware=(value) ⇒ Object (writeonly)
42 43 44 |
# File 'lib/appsignal/rack/event_handler.rb', line 42 def using_appsignal_event_middleware=(value) @using_appsignal_event_middleware = value end |
Class Method Details
.safe_execution(name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 35 36 37 38 |
# File 'lib/appsignal/rack/event_handler.rb', line 32 def self.safe_execution(name) yield rescue => e Appsignal.internal_logger.error( "Error occurred in #{name}: #{e.class}: #{e}: #{e.backtrace}" ) end |
Instance Method Details
#on_error(request, _response, error) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/appsignal/rack/event_handler.rb', line 97 def on_error(request, _response, error) return unless Appsignal.active? self.class.safe_execution("Appsignal::Rack::EventHandler#on_error") do return unless request_handler?(request.env[APPSIGNAL_EVENT_HANDLER_ID]) transaction = request.env[APPSIGNAL_TRANSACTION] return unless transaction request.env[APPSIGNAL_EVENT_HANDLER_HAS_ERROR] = true transaction.set_error(error) end end |
#on_finish(request, response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/appsignal/rack/event_handler.rb', line 112 def on_finish(request, response) return unless Appsignal.active? return unless request_handler?(request.env[APPSIGNAL_EVENT_HANDLER_ID]) transaction = request.env[APPSIGNAL_TRANSACTION] return unless transaction self.class.safe_execution("Appsignal::Rack::EventHandler#on_finish") do transaction.finish_event("process_request.rack", "callback: on_finish", "") transaction.add_params_if_nil { request.params } transaction.add_headers_if_nil { request.env } transaction.add_session_data_if_nil do request.session if request.respond_to?(:session) end queue_start = Appsignal::Rack::Utils.queue_start_from(request.env) transaction.set_queue_start(queue_start) if queue_start response_status = if response response.status elsif request.env[APPSIGNAL_EVENT_HANDLER_HAS_ERROR] == true 500 end if response_status transaction.(:response_status => response_status) Appsignal.increment_counter( :response_status, 1, :status => response_status, :namespace => format_namespace(transaction.namespace) ) end end # Make sure the current transaction is always closed when the request # is finished transaction.complete Appsignal::Transaction.clear_current_transaction! end |
#on_start(request, _response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/appsignal/rack/event_handler.rb', line 56 def on_start(request, _response) emit_warning_once return unless Appsignal.active? event_handler = self self.class.safe_execution("Appsignal::Rack::EventHandler#on_start") do request.env[APPSIGNAL_EVENT_HANDLER_ID] ||= id return unless request_handler?(request.env[APPSIGNAL_EVENT_HANDLER_ID]) transaction = Appsignal::Transaction.create(Appsignal::Transaction::HTTP_REQUEST) transaction.start_event request.env[APPSIGNAL_TRANSACTION] = transaction request.env[RACK_AFTER_REPLY] ||= [] request.env[RACK_AFTER_REPLY] << proc do next unless event_handler.request_handler?(request.env[APPSIGNAL_EVENT_HANDLER_ID]) Appsignal::Rack::EventHandler .safe_execution("Appsignal::Rack::EventHandler's after_reply") do transaction.finish_event("process_request.rack", "callback: after_reply", "") queue_start = Appsignal::Rack::Utils.queue_start_from(request.env) transaction.set_queue_start(queue_start) if queue_start end # Make sure the current transaction is always closed when the request # is finished. This is a fallback for in case the `on_finish` # callback is not called. This is supported by servers like Puma and # Unicorn. # # The EventHandler.on_finish callback should be called first, this is # just a fallback if that doesn't get called. # # One such scenario is when a Puma "lowlevel_error" occurs. transaction.complete Appsignal::Transaction.clear_current_transaction! end end end |
#request_handler?(given_id) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
51 52 53 |
# File 'lib/appsignal/rack/event_handler.rb', line 51 def request_handler?(given_id) id == given_id end |