use skylight without their sinatra code

This commit is contained in:
Konstantin Haase 2015-01-14 13:41:31 +01:00
parent 0aebede82d
commit ea4a1c5bc4
7 changed files with 93 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
if ENV['SKYLIGHT_AUTHENTICATION']
require_relative 'skylight/actual'
else
require_relative 'skylight/dummy'
end

View File

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

View File

@ -0,0 +1,7 @@
class Travis::Api::App
class Middleware
module Skylight
def self.new(app) app end
end
end
end

View File

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