add conditional skylight tracking

This commit is contained in:
Konstantin Haase 2015-01-14 12:33:23 +01:00
parent 371d67f900
commit 396a0f756c
5 changed files with 57 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ config/travis.yml
.yardoc
log/
vendor
config/skylight.yml

View File

@ -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'

View File

@ -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!

View File

@ -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.

View File

@ -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