diff --git a/.gitignore b/.gitignore index e5dba86d..68072775 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config/travis.yml .yardoc log/ vendor +config/skylight.yml diff --git a/Gemfile b/Gemfile index aa41780b..1b0280e0 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ gem 'pry' gem 'metriks', '0.9.9.6' gem 'metriks-librato_metrics', github: 'eric/metriks-librato_metrics' gem 'micro_migrations' +gem 'skylight', '~> 0.6.0.beta.1' group :test do gem 'rspec', '~> 2.13' diff --git a/Gemfile.lock b/Gemfile.lock index f85ae2cf..c12faada 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,7 +81,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-yaml.git - revision: f3aa306016a08b66a487f966eb8aa3a60ee9b319 + revision: 1630d576d221aea2340615e87f402df7889b5176 specs: travis-yaml (0.2.0) @@ -302,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) @@ -367,6 +369,7 @@ DEPENDENCIES sentry-raven! sinatra sinatra-contrib + skylight (~> 0.6.0.beta.1) travis-api! travis-config (~> 0.1.0) travis-core! diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb index 1934ede2..b1aea36b 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/conditional_skylight' # Rack class implementing the HTTP API. # Instances respond to #call. diff --git a/lib/travis/api/conditional_skylight.rb b/lib/travis/api/conditional_skylight.rb new file mode 100644 index 00000000..773a316d --- /dev/null +++ b/lib/travis/api/conditional_skylight.rb @@ -0,0 +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