From ea4a1c5bc4ee35284715e0f93b3e3ed8f8a2bc90 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Wed, 14 Jan 2015 13:41:31 +0100 Subject: [PATCH] use skylight without their sinatra code --- Gemfile | 2 +- Gemfile.lock | 12 +-- lib/travis/api/app.rb | 1 + lib/travis/api/app/middleware/skylight.rb | 5 + .../api/app/middleware/skylight/actual.rb | 26 +++++ .../api/app/middleware/skylight/dummy.rb | 7 ++ lib/travis/api/conditional_skylight.rb | 100 +++++++++--------- 7 files changed, 93 insertions(+), 60 deletions(-) create mode 100644 lib/travis/api/app/middleware/skylight.rb create mode 100644 lib/travis/api/app/middleware/skylight/actual.rb create mode 100644 lib/travis/api/app/middleware/skylight/dummy.rb diff --git a/Gemfile b/Gemfile index 8bd41c72..e433a477 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ gem 'pry' gem 'metriks', '0.9.9.6' gem 'metriks-librato_metrics', github: 'eric/metriks-librato_metrics' gem 'micro_migrations' -gem 'skylight', github: 'rkh/skylight-ruby', branch: 'rkh-script-name-sinatra' +gem 'skylight' group :test do gem 'rspec', '~> 2.13' diff --git a/Gemfile.lock b/Gemfile.lock index ec0c0a9d..3f38a26c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,14 +20,6 @@ GIT rack-contrib (1.2.0) rack (>= 0.9.1) -GIT - remote: git://github.com/rkh/skylight-ruby.git - revision: 287673533ee47e10fd75adb78bae1e732b8f3c30 - branch: rkh-script-name-sinatra - specs: - skylight (0.6.0.beta.1) - activesupport (>= 3.0.0) - GIT remote: git://github.com/rkh/yard-sinatra.git revision: 00774d355123617ff0faa7e0ebd54c4cdcfcdf93 @@ -310,6 +302,8 @@ GEM rack-test sinatra (~> 1.4.0) tilt (~> 1.3) + skylight (0.6.0.beta.1) + activesupport (>= 3.0.0) slop (3.6.0) sprockets (2.2.3) hike (~> 1.2) @@ -375,7 +369,7 @@ DEPENDENCIES sentry-raven! sinatra sinatra-contrib - skylight! + skylight travis-api! travis-config (~> 0.1.0) travis-core! diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb index b1aea36b..cf4e3bc5 100644 --- a/lib/travis/api/app.rb +++ b/lib/travis/api/app.rb @@ -113,6 +113,7 @@ module Travis::Api use Travis::Api::App::Middleware::ScopeCheck use Travis::Api::App::Middleware::Logging + use Travis::Api::App::Middleware::Skylight use Travis::Api::App::Middleware::Metriks use Travis::Api::App::Middleware::Rewrite use Travis::Api::App::Middleware::UserAgentTracker diff --git a/lib/travis/api/app/middleware/skylight.rb b/lib/travis/api/app/middleware/skylight.rb new file mode 100644 index 00000000..ccd2c527 --- /dev/null +++ b/lib/travis/api/app/middleware/skylight.rb @@ -0,0 +1,5 @@ +if ENV['SKYLIGHT_AUTHENTICATION'] + require_relative 'skylight/actual' +else + require_relative 'skylight/dummy' +end diff --git a/lib/travis/api/app/middleware/skylight/actual.rb b/lib/travis/api/app/middleware/skylight/actual.rb new file mode 100644 index 00000000..a7a59696 --- /dev/null +++ b/lib/travis/api/app/middleware/skylight/actual.rb @@ -0,0 +1,26 @@ +require 'travis/api/app' +require 'skylight' +require 'skylight/probes/tilt' +require 'skylight/probes/redis' + +class Travis::Api::App + class Middleware + class Skylight < Middleware + set(:setup) { ::Skylight.start! } + use ::Skylight::Middleware + + after do + instrumenter = Skylight::Instrumenter.instance + trace = instrumenter.current_trace if instrumenter + trace.endpoint = endpoint if trace and endpoint + end + + def endpoint + return @endpoint if defined? @endpoint and @endpoint + return unless headers['X-Pattern'].present? and headers['X-Endpoint'].present? + @endpoint = Object.const_get(headers['X-Endpoint']) + headers['X-Pattern'] + rescue NameError + end + end + end +end diff --git a/lib/travis/api/app/middleware/skylight/dummy.rb b/lib/travis/api/app/middleware/skylight/dummy.rb new file mode 100644 index 00000000..d4309f23 --- /dev/null +++ b/lib/travis/api/app/middleware/skylight/dummy.rb @@ -0,0 +1,7 @@ +class Travis::Api::App + class Middleware + module Skylight + def self.new(app) app end + end + end +end diff --git a/lib/travis/api/conditional_skylight.rb b/lib/travis/api/conditional_skylight.rb index 773a316d..d4a2de37 100644 --- a/lib/travis/api/conditional_skylight.rb +++ b/lib/travis/api/conditional_skylight.rb @@ -1,50 +1,50 @@ -if ENV['SKYLIGHT_AUTHENTICATION'] - - require 'skylight/sinatra' - require 'tool/thread_local' - Skylight.start! - - module Travis - module Api - module ConditionalSkylight - FEATURES = Tool::ThreadLocal.new - CHECK_FREQUENCY = 120 - NOT_JSON = %r(\.(xml|png|txt|atom|svg)$) - - module Middleware - ::Skylight::Middleware.send(:prepend, self) - def call(env) - if ConditionalSkylight.track?(env) - super(env) - else - t { "skipping middleware (condition not met)".freeze } - @app.call(env) - end - end - end - - extend self - - def track?(env) - return false unless feature_active? :skylight - return false if feature_active? :skylight_json_only and env['PATH_INFO'.freeze] =~ NOT_JSON - true - end - - def feature_active?(feature) - last_clear = Time.now.to_i - FEATURES[:last_clear].to_i - - if last_clear > CHECK_FREQUENCY - FEATURES.clear - FEATURES[:last_clear] = Time.now.to_i - end - - FEATURES.fetch(feature) { FEATURES[feature] = Travis::Features.feature_active?(feature) } - end - end - end - end - -else - Travis.logger.info('SKYLIGHT_AUTHENTICATION not set, skipping Skylight.') -end +# if ENV['SKYLIGHT_AUTHENTICATION'] +# +# # require 'skylight/sinatra' +# # require 'tool/thread_local' +# # Skylight.start! +# # +# # module Travis +# # module Api +# # module ConditionalSkylight +# # FEATURES = Tool::ThreadLocal.new +# # CHECK_FREQUENCY = 120 +# # NOT_JSON = %r(\.(xml|png|txt|atom|svg)$) +# # +# # module Middleware +# # ::Skylight::Middleware.send(:prepend, self) +# # def call(env) +# # if ConditionalSkylight.track?(env) +# # super(env) +# # else +# # t { "skipping middleware (condition not met)".freeze } +# # @app.call(env) +# # end +# # end +# # end +# # +# # extend self +# # +# # def track?(env) +# # return false unless feature_active? :skylight +# # return false if feature_active? :skylight_json_only and env['PATH_INFO'.freeze] =~ NOT_JSON +# # true +# # end +# # +# # def feature_active?(feature) +# # last_clear = Time.now.to_i - FEATURES[:last_clear].to_i +# # +# # if last_clear > CHECK_FREQUENCY +# # FEATURES.clear +# # FEATURES[:last_clear] = Time.now.to_i +# # end +# # +# # FEATURES.fetch(feature) { FEATURES[feature] = Travis::Features.feature_active?(feature) } +# # end +# # end +# # end +# # end +# +# else +# Travis.logger.info('SKYLIGHT_AUTHENTICATION not set, skipping Skylight.') +# end