-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathsidekiq.rb
239 lines (196 loc) · 8.29 KB
/
sidekiq.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require 'honeybadger/instrumentation_helper'
require 'honeybadger/plugin'
require 'honeybadger/ruby'
module Honeybadger
module Plugins
module Sidekiq
class Middleware
def call(_worker, _msg, _queue)
Honeybadger.clear!
yield
end
end
class ServerMiddlewareInstrumentation
include Honeybadger::InstrumentationHelper
def call(worker, msg, queue, &block)
if msg["wrapped"]
context = {
jid: msg["jid"],
worker: msg["wrapped"],
queue: queue
}
else
context = {
jid: msg["jid"],
worker: msg["class"],
queue: queue
}
end
begin
duration = Honeybadger.instrumentation.monotonic_timer { block.call }[0]
status = 'success'
rescue Exception => e
status = 'failure'
raise
ensure
context.merge!(duration: duration, status: status)
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
Honeybadger.event('perform.sidekiq', context)
end
if Honeybadger.config.load_plugin_insights_metrics?(:sidekiq)
metric_source 'sidekiq'
gauge 'perform', context.slice(:worker, :queue, :duration)
end
end
end
end
class ClientMiddlewareInstrumentation
include Honeybadger::InstrumentationHelper
def call(worker, msg, queue, _redis)
context = {
worker: msg["wrapped"] || msg["class"],
queue: queue
}
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
Honeybadger.event('enqueue.sidekiq', context)
end
yield
end
end
Plugin.register :sidekiq do
leader_checker = nil
requirement { defined?(::Sidekiq) }
execution do
if Honeybadger.config[:'exceptions.enabled']
::Sidekiq.configure_server do |sidekiq|
sidekiq.server_middleware do |chain|
chain.prepend Middleware
end
end
if defined?(::Sidekiq::VERSION) && ::Sidekiq::VERSION > '3'
::Sidekiq.configure_server do |sidekiq|
sidekiq_default_configuration = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration : Class.new
sidekiq.error_handlers << lambda { |ex, sidekiq_params, sidekiq_config = sidekiq_default_configuration|
params = sidekiq_params.dup
if defined?(::Sidekiq::Config)
if params[:_config].is_a?(::Sidekiq::Config) # Sidekiq > 6 and < 7.1.5
params[:_config] = params[:_config].instance_variable_get(:@options)
else # Sidekiq >= 7.1.5
params[:_config] = sidekiq_config.instance_variable_get(:@options)
end
end
job = params[:job] || params
job_retry = job['retry'.freeze]
if (threshold = config[:'sidekiq.attempt_threshold'].to_i) > 0 && job_retry
# We calculate the job attempts to determine the need to
# skip. Sidekiq's first job execution will have nil for the
# 'retry_count' job key. The first retry will have 0 set for
# the 'retry_count' key, incrementing on each execution
# afterwards.
retry_count = job['retry_count'.freeze]
attempt = retry_count ? retry_count + 1 : 0
max_retries = (::Sidekiq::VERSION > '7') ?
::Sidekiq.default_configuration[:max_retries] : sidekiq.options[:max_retries]
# Ensure we account for modified max_retries setting
default_max_retry_attempts = defined?(::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS) ? ::Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS : 25
retry_limit = job_retry == true ? (max_retries || default_max_retry_attempts) : job_retry.to_i
limit = [retry_limit, threshold].min
return if attempt < limit
end
opts = { parameters: params }
if config[:'sidekiq.use_component']
opts[:component] = job['wrapped'.freeze] || job['class'.freeze]
opts[:action] = 'perform' if opts[:component]
end
Honeybadger.notify(ex, opts)
}
end
end
end
if config.load_plugin_insights?(:sidekiq)
require "sidekiq"
require "sidekiq/api"
if Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new('6.5')
require "sidekiq/component"
class SidekiqClusterCollectionChecker
include ::Sidekiq::Component
def initialize(config)
@config = config
end
def collect?
return true unless defined?(::Sidekiq::Enterprise)
leader?
end
end
end
::Sidekiq.configure_server do |config|
config.server_middleware { |chain| chain.add(ServerMiddlewareInstrumentation) }
config.client_middleware { |chain| chain.add(ClientMiddlewareInstrumentation) }
if defined?(SidekiqClusterCollectionChecker)
config.on(:startup) do
leader_checker = SidekiqClusterCollectionChecker.new(config)
end
end
end
::Sidekiq.configure_client do |config|
config.client_middleware { |chain| chain.add(ClientMiddlewareInstrumentation) }
end
end
end
collect_sidekiq_stats = -> do
stats = ::Sidekiq::Stats.new
data = stats.as_json
data[:queues] = {}
::Sidekiq::Queue.all.each do |queue|
data[:queues][queue.name] ||= {}
data[:queues][queue.name][:latency] = (queue.latency * 1000).ceil
data[:queues][queue.name][:depth] = queue.size
end
Hash.new(0).tap do |busy_counts|
::Sidekiq::Workers.new.each do |_pid, _tid, work|
payload = work.respond_to?(:payload) ? work.payload : work["payload"]
payload = JSON.parse(payload) if payload.is_a?(String)
busy_counts[payload["queue"]] += 1
end
end.each do |queue_name, busy_count|
data[:queues][queue_name] ||= {}
data[:queues][queue_name][:busy] = busy_count
end
processes = ::Sidekiq::ProcessSet.new.to_enum(:each).to_a
data[:capacity] = processes.map { |process| process["concurrency"] }.sum
process_utilizations = processes.map do |process|
next unless process["concurrency"].to_f > 0
process["busy"] / process["concurrency"].to_f
end.compact
if process_utilizations.any?
utilization = process_utilizations.sum / process_utilizations.length.to_f
data[:utilization] = utilization
end
data
end
collect do
if config.cluster_collection?(:sidekiq) && (leader_checker.nil? || leader_checker.collect?)
stats = collect_sidekiq_stats.call
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
Honeybadger.event('stats.sidekiq', stats.except('stats').merge(stats['stats']))
end
if Honeybadger.config.load_plugin_insights_metrics?(:sidekiq)
metric_source 'sidekiq'
stats['stats'].each do |name, value|
gauge name, value: value
end
stats[:queues].each do |queue_name, data|
data.each do |key, value|
gauge "queue_#{key}", queue: queue_name, value: value
end
end
gauge 'capacity', value: stats[:capacity] if stats[:capacity]
gauge 'utilization', value: stats[:utilization] if stats[:utilization]
end
end
end
end
end
end
end