automated middleware instrumentation
This commit is contained in:
parent
831549a173
commit
decf338a38
|
@ -20,6 +20,7 @@ require 'metriks/librato_metrics_reporter'
|
||||||
require 'travis/support/log_subscriber/active_record_metrics'
|
require 'travis/support/log_subscriber/active_record_metrics'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'travis/api/v2/http'
|
require 'travis/api/v2/http'
|
||||||
|
require 'travis/api/stack_instrumentation'
|
||||||
|
|
||||||
# Rack class implementing the HTTP API.
|
# Rack class implementing the HTTP API.
|
||||||
# Instances respond to #call.
|
# Instances respond to #call.
|
||||||
|
@ -75,6 +76,7 @@ module Travis::Api
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@app = Rack::Builder.app do
|
@app = Rack::Builder.app do
|
||||||
|
extend StackInstrumentation
|
||||||
use(Rack::Config) { |env| env['metriks.request.start'] ||= Time.now.utc }
|
use(Rack::Config) { |env| env['metriks.request.start'] ||= Time.now.utc }
|
||||||
|
|
||||||
Rack::Utils::HTTP_STATUS_CODES[420] = "Enhance Your Calm"
|
Rack::Utils::HTTP_STATUS_CODES[420] = "Enhance Your Calm"
|
||||||
|
|
42
lib/travis/api/app/stack_instrumentation.rb
Normal file
42
lib/travis/api/app/stack_instrumentation.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
require 'travis/api/app'
|
||||||
|
|
||||||
|
class Travis::Api::App
|
||||||
|
module StackInstrumentation
|
||||||
|
class Middleware
|
||||||
|
def initialize(app, title = nil)
|
||||||
|
@app = app
|
||||||
|
@title = title || "Rack: use #{app.class.name}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
instrument { @app.call(env) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def instrument(&block)
|
||||||
|
return yield unless instrument?
|
||||||
|
::Skylight.instrument(title: title, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def instrument?
|
||||||
|
defined? ::Skylight
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def use(*)
|
||||||
|
super(Middleware)
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
|
def run(app)
|
||||||
|
super Middleware.new(app, "Rack: run %p" % app.class)
|
||||||
|
end
|
||||||
|
|
||||||
|
def map(path, &block)
|
||||||
|
super(path) do
|
||||||
|
use(Middleware, "Rack: map %p" % path)
|
||||||
|
extend StackInstrumentation
|
||||||
|
instance_eval(&block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user