diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb index 1bf44abe..b092aee8 100644 --- a/lib/travis/api/app.rb +++ b/lib/travis/api/app.rb @@ -20,6 +20,7 @@ require 'metriks/librato_metrics_reporter' require 'travis/support/log_subscriber/active_record_metrics' require 'fileutils' require 'travis/api/v2/http' +require 'travis/api/stack_instrumentation' # Rack class implementing the HTTP API. # Instances respond to #call. @@ -75,6 +76,7 @@ module Travis::Api def initialize @app = Rack::Builder.app do + extend StackInstrumentation use(Rack::Config) { |env| env['metriks.request.start'] ||= Time.now.utc } Rack::Utils::HTTP_STATUS_CODES[420] = "Enhance Your Calm" diff --git a/lib/travis/api/app/stack_instrumentation.rb b/lib/travis/api/app/stack_instrumentation.rb new file mode 100644 index 00000000..f6fdec78 --- /dev/null +++ b/lib/travis/api/app/stack_instrumentation.rb @@ -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