Module: Honeycomb::Rack

Included in:
Middleware, Honeycomb::Rails::Middleware, Sinatra::Middleware
Defined in:
lib/honeycomb/integrations/rack.rb

Overview

Rack specific methods for building middleware

Defined Under Namespace

Classes: Middleware

Constant Summary collapse

RACK_FIELDS =
[
  ["REQUEST_METHOD", "request.method"],
  ["PATH_INFO", "request.path"],
  ["QUERY_STRING", "request.query_string"],
  ["HTTP_VERSION", "request.http_version"],
  ["HTTP_HOST", "request.host"],
  ["REMOTE_ADDR", "request.remote_addr"],
  ["HTTP_X_FORWARDED_FOR", "request.header.x_forwarded_for"],
  ["HTTP_X_FORWARDED_PROTO", "request.header.x_forwarded_proto"],
  ["HTTP_X_FORWARDED_PORT", "request.header.x_forwarded_port"],
  ["HTTP_ACCEPT", "request.header.accept"],
  ["HTTP_ACCEPT_ENCODING", "request.header.accept_encoding"],
  ["HTTP_ACCEPT_LANGUAGE", "request.header.accept_language"],
  ["CONTENT_TYPE", "request.header.content_type"],
  ["HTTP_USER_AGENT", "request.header.user_agent"],
  ["rack.url_scheme", "request.scheme"],
  ["HTTP_REFERER", "request.header.referer"],
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



28
29
30
# File 'lib/honeycomb/integrations/rack.rb', line 28

def app
  @app
end

#clientObject (readonly)

Returns the value of attribute client.



28
29
30
# File 'lib/honeycomb/integrations/rack.rb', line 28

def client
  @client
end

Instance Method Details

#add_package_information(_env) {|"meta.package", "rack"| ... } ⇒ Object

Yields:

  • ("meta.package", "rack")


66
67
68
69
# File 'lib/honeycomb/integrations/rack.rb', line 66

def add_package_information(_env)
  yield "meta.package", "rack"
  yield "meta.package_version", ::Rack.release
end

#call(env) ⇒ Object



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
# File 'lib/honeycomb/integrations/rack.rb', line 35

def call(env)
  req = ::Rack::Request.new(env)
  client.start_span(
    name: "http_request",
    serialized_trace: env,
  ) do |span|
    add_field = lambda do |key, value|
      unless value.nil? || (value.respond_to?(:empty?) && value.empty?)
        span.add_field(key, value)
      end
    end

    extract_fields(env, RACK_FIELDS, &add_field)

    span.add_field("request.secure", req.ssl?)
    span.add_field("request.xhr", req.xhr?)

    begin
      status, headers, body = call_with_hook(env, span, &add_field)
    ensure
      add_package_information(env, &add_field)
      extract_user_information(env, &add_field)
    end

    span.add_field("response.status_code", status)
    span.add_field("response.content_type", headers["Content-Type"])

    [status, headers, body]
  end
end

#extract_fields(env, fields) ⇒ Object



71
72
73
74
75
# File 'lib/honeycomb/integrations/rack.rb', line 71

def extract_fields(env, fields)
  fields.each do |key, value|
    yield value, env[key]
  end
end

#initialize(app, options) ⇒ Object



30
31
32
33
# File 'lib/honeycomb/integrations/rack.rb', line 30

def initialize(app, options)
  @app = app
  @client = options[:client]
end