Class: Librato::Sidekiq::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/librato-sidekiq/middleware.rb

Direct Known Subclasses

ClientMiddleware

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Middleware

Returns a new instance of Middleware.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/librato-sidekiq/middleware.rb', line 14

def initialize(options = {})
  # hard dependency on one or the other being present
  rails = !!defined?(Librato::Rails)
  rack = !!defined?(Librato::Rack)
  fail 'librato-sidekiq depends on having one of librato-rails or librato-rack installed' unless rails || rack

  # librato-rails >= 0.10 changes behavior of reporting agent
  if File.basename($PROGRAM_NAME) == 'sidekiq' && rails && Librato::Rails::VERSION.split('.')[1].to_i >= 10 && ENV['LIBRATO_AUTORUN'].nil?
    puts 'NOTICE: --------------------------------------------------------------------'
    puts 'NOTICE: THE REPORTING AGENT HAS NOT STARTED, AND NO METRICS WILL BE SENT'
    puts 'NOTICE: librato-rails >= 0.10 requires LIBRATO_AUTORUN=1 in your environment'
    puts 'NOTICE: --------------------------------------------------------------------'
  end
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



29
30
31
32
33
# File 'lib/librato-sidekiq/middleware.rb', line 29

def self.configure
  yield(self) if block_given?
  reconfigure
  new
end

.optionsObject



49
50
51
52
53
54
55
56
57
# File 'lib/librato-sidekiq/middleware.rb', line 49

def self.options
  {
    enabled: enabled,
    whitelist_queues: whitelist_queues,
    blacklist_queues: blacklist_queues,
    whitelist_classes: whitelist_classes,
    blacklist_classes: blacklist_classes
  }
end

.reconfigureObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/librato-sidekiq/middleware.rb', line 35

def self.reconfigure
  # puts "Reconfiguring with: #{options}"
  ::Sidekiq.configure_server do |config|
    config.client_middleware do |chain|
      chain.remove ClientMiddleware
      chain.add ClientMiddleware, options
    end
    config.server_middleware do |chain|
      chain.remove self
      chain.add self, options
    end
  end
end

Instance Method Details

#call(worker_instance, msg, queue, redis_pool = nil) ⇒ Object

redis_pool is needed for the sidekiq 3 upgrade github.com/mperham/sidekiq/blob/master/3.0-Upgrade.md



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/librato-sidekiq/middleware.rb', line 61

def call(worker_instance, msg, queue, redis_pool = nil)
  start_time = Time.now
  result = yield
  elapsed = (Time.now - start_time).to_f

  return result unless enabled
  # puts "#{worker_instance} #{queue}"

  stats = ::Sidekiq::Stats.new

  Librato.group 'sidekiq' do |sidekiq|
    track sidekiq, stats, worker_instance, msg, queue, elapsed
  end

  result
end