Class: Rack::Honeycomb::Middleware
- Inherits:
-
Object
- Object
- Rack::Honeycomb::Middleware
- Defined in:
- lib/rack/honeycomb/middleware.rb
Constant Summary collapse
- ENV_REGEX =
/^#{ Regexp.escape ENV_PREFIX }/
- USER_AGENT_SUFFIX =
"rack-honeycomb/#{VERSION}"
- EVENT_TYPE =
'http_server'.freeze
- RAILS_SPECIAL_PARAMS =
%w(controller action).freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rack/honeycomb/middleware.rb', line 33 def initialize(app, = {}) @app, @options = app, @logger = .delete(:logger) @logger ||= ::Honeycomb.logger if defined?(::Honeycomb.logger) @is_sinatra = .delete(:is_sinatra) debug 'Enabling Sinatra-specific fields' if @is_sinatra @is_rails = .delete(:is_rails) debug 'Enabling Rails-specific fields' if @is_rails # report meta.package = rack only if we have no better information package = 'rack' package_version = RACK_VERSION if @is_rails package = 'rails' package_version = ::Rails::VERSION::STRING elsif @is_sinatra package = 'sinatra' package_version = ::Sinatra::VERSION end honeycomb = if client = .delete(:client) debug "initialized with #{client.class.name} via :client option" client elsif defined?(::Honeycomb.client) debug "initialized with #{::Honeycomb.client.class.name} from honeycomb-beeline" ::Honeycomb.client else debug "initializing new Libhoney::Client" Libhoney::Client.new(.merge(user_agent_addition: USER_AGENT_SUFFIX)) end @builder = honeycomb.builder. add( 'meta.package' => package, 'meta.package_version' => package_version, 'type' => EVENT_TYPE, 'meta.local_hostname' => Socket.gethostname, ) end |
Instance Method Details
#call(env) ⇒ 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 |
# File 'lib/rack/honeycomb/middleware.rb', line 74 def call(env) ev = @builder.event add_request_fields(ev, env) start = Time.now status, headers, body = with_tracing_if_available(ev, env) do @app.call(env) end add_response_fields(ev, status, headers, body) [status, headers, body] rescue Exception => e if ev ev.add_field('request.error', e.class.name) ev.add_field('request.error_detail', e.) end raise ensure if ev && start finish = Time.now ev.add_field('duration_ms', (finish - start) * 1000) add_sinatra_fields(ev, env) if @is_sinatra add_rails_fields(ev, env) if @is_rails add_app_fields(ev, env) ev.send end end |