Class: Rack::Honeycomb::Middleware

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (#call)
  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :writekey (String) — default: nil
  • :dataset (String) — default: nil
  • :api_host (String) — default: nil
  • :is_sinatra (Boolean) — default: false
  • :is_rails (Boolean) — default: false


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, options = {})
  @app, @options = app, options

  @logger = options.delete(:logger)
  @logger ||= ::Honeycomb.logger if defined?(::Honeycomb.logger)

  @is_sinatra = options.delete(:is_sinatra)
  debug 'Enabling Sinatra-specific fields' if @is_sinatra
  @is_rails = options.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 = options.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(options.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.message)
  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