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.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/appsignal/rack/event_handler.rb', line 119 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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/appsignal/rack/event_handler.rb', line 134 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_request_payload_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 # Describes the response on the transaction's span, which the semantic # conventions ask for whenever a response was sent. It can be set here # because the `process_request.rack` event was finished above, which # leaves the transaction's own span as the one attributes go on. # # Only a response the app actually produced counts. The 500 below # stands in for a status that was never sent, so it is reported as a # tag and a metric but not as this attribute. transaction.add_opentelemetry_attributes( Appsignal::OpenTelemetry::HttpResponse.attributes_for(response&.status) ) 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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, :opentelemetry_context => Appsignal::OpenTelemetry.extract_rack_context(request.env), :opentelemetry_scope => ["appsignal-ruby/rack", Appsignal::VERSION] ) # Describes the transaction's span as an incoming HTTP request. # Together with the SERVER span kind the transaction already carries, # this is what the trace timeline reads to recognize a web request. # # Set before the event below starts, because attributes go on # whichever span is open and these belong on the transaction's own # span. That event stays open until the response finishes, so there is # no later point in the request at which these could be set. transaction.add_opentelemetry_attributes( Appsignal::OpenTelemetry::HttpServerRequest.attributes_for( :method => Appsignal::Rack::Utils.request_method_from(request), :path => Appsignal::Rack::Utils.request_value_from(request, :path), :scheme => Appsignal::Rack::Utils.request_value_from(request, :scheme), :query => Appsignal::Rack::Utils.request_value_from(request, :query_string) ) ) transaction.start_event( :opentelemetry_scope => ["appsignal-ruby/rack", Appsignal::VERSION] ) 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 |