This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
newrelic_sidekiq_agent
executable file
·71 lines (56 loc) · 2.47 KB
/
newrelic_sidekiq_agent
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
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'newrelic_plugin'
require 'sidekiq'
require 'sidekiq/api'
require 'redis'
module NewRelic::Sidekiq
class Agent < NewRelic::Plugin::Agent::Base
agent_guid 'com.eksoverzero.newrelic-sidekiq-agent'
agent_config_options :name, :uri, :password, :namespace
agent_version '1.1.1'
agent_human_labels('Sidekiq') { name }
attr_reader :sidekiq_stats, :sidekiq_workers
def initialize(context, options = {})
raise ArgumentError, "Redis connection URL missing" if options[:uri].nil?
super
setup_sidekiq
end
def setup_metrics
@jobs_failed = NewRelic::Processor::EpochCounter.new
@jobs_processed = NewRelic::Processor::EpochCounter.new
end
def setup_sidekiq
redis_configuration = { url: uri }
redis_configuration.merge!(password: password) if password
redis_configuration.merge!(namespace: namespace) if namespace
Sidekiq.configure_client { |config| config.redis = redis_configuration }
@sidekiq_stats = Sidekiq::Stats.new
@sidekiq_workers = Sidekiq::Workers.new
end
def poll_cycle
report_metric 'Workers/Working', 'Workers', sidekiq_workers.size || 0
report_metric 'Jobs/Count/Pending', 'Jobs', sidekiq_stats.enqueued || 0
report_metric 'Jobs/Count/Processed', 'Jobs', sidekiq_stats.processed || 0
report_metric 'Jobs/Count/Failed', 'Jobs', sidekiq_stats.failed || 0
report_metric 'Jobs/Count/Scheduled', 'Jobs', sidekiq_stats.scheduled_size || 0
report_metric 'Jobs/Count/Retries', 'Jobs', sidekiq_stats.retry_size || 0
report_metric 'Jobs/Rate/Processed', 'Jobs/sec', @jobs_processed.process(stats.processed || 0)
report_metric 'Jobs/Rate/Failed', 'Jobs/sec', @jobs_failed.process(stats.failed || 0)
stats.queues.each do |name, enqueued|
report_metric "Queues/#{name}", 'Enqueued', enqueued || 0
report_metric "Queues/#{name}", 'Latency', Sidekiq::Queue.new(name).latency || 0
end
rescue Redis::TimeoutError
raise 'Redis server timeout'
rescue Redis::CannotConnectError, Redis::ConnectionError
raise 'Could not connect to redis'
rescue Exception => e
raise "#{e.class}: #{e.message}"
end
end
NewRelic::Plugin::Config.config_file = File.dirname(__FILE__) + '/config/newrelic_plugin.yml'
NewRelic::Plugin::Setup.install_agent :sidekiq, self
NewRelic::Plugin::Run.setup_and_run
end