Class: Gitlab::Sherlock::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/sherlock/middleware.rb

Overview

Rack middleware used for tracking request metrics.

Constant Summary collapse

CONTENT_TYPES =
/text\/html|application\/json/i
IGNORE_PATHS =
%r{^/sherlock}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware


9
10
11
# File 'lib/gitlab/sherlock/middleware.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

env - A Hash containing Rack environment details.


14
15
16
17
18
19
20
# File 'lib/gitlab/sherlock/middleware.rb', line 14

def call(env)
  if instrument?(env)
    call_with_instrumentation(env)
  else
    @app.call(env)
  end
end

#call_with_instrumentation(env) ⇒ Object


22
23
24
25
26
27
28
29
# File 'lib/gitlab/sherlock/middleware.rb', line 22

def call_with_instrumentation(env)
  trans = transaction_from_env(env)
  retval = trans.run { @app.call(env) }

  Sherlock.collection.add(trans)

  retval
end

#instrument?(env) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
34
# File 'lib/gitlab/sherlock/middleware.rb', line 31

def instrument?(env)
  !!(env['HTTP_ACCEPT'] =~ CONTENT_TYPES &&
     env['REQUEST_URI'] !~ IGNORE_PATHS)
end

#transaction_from_env(env) ⇒ Object


36
37
38
# File 'lib/gitlab/sherlock/middleware.rb', line 36

def transaction_from_env(env)
  Transaction.new(env['REQUEST_METHOD'], env['REQUEST_URI'])
end