Skip to content

Commit e9ecf65

Browse files
committed
Add mode to dynamicly set agents up, one per namespace
1 parent c60b735 commit e9ecf65

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

newrelic_resque_agent/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
The New Relic Resque Plugin enables monitoring of Resque, a library for processing background jobs, reporting the following data for a specified instance:
44

5+
* Dynamicly monitors all namespaces on redis server OR select only a few.
56
* Number of working workers
67
* Pending jobs number
78
* Total failed jobs number
@@ -32,6 +33,8 @@ The Resque monitoring Plugin for New Relic requires the following:
3233

3334
3.2. add the Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db'
3435

36+
3.3 If you would like the plugin to monitor all the namespaces on a particular redis server, set the agent name to "dynamic"
37+
3538
4. Execute
3639

3740
`newrelic_resque_agent run`

newrelic_resque_agent/bin/newrelic_resque_agent

100644100755
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,53 @@ $stdout.sync = true
44
$LOAD_PATH.unshift File.expand_path "../../lib", __FILE__
55
require "newrelic_resque_agent"
66
require 'optparse'
7+
require 'resque'
8+
require 'redis'
9+
require 'yaml'
710

11+
12+
# Register and run the agent
813
NewRelic::Plugin::Config.config_file = "/etc/newrelic/newrelic_resque_agent.yml"
914

15+
def make_dynamic_file
16+
agents = NewRelic::Plugin::Config.config.agents.collect do |agent_key,agent|
17+
18+
if agent_key == "dynamic"
19+
20+
if agent['redis'].nil?
21+
raise "Redis connection URL "
22+
end
23+
24+
Resque.redis = agent['redis']
25+
26+
# get a list of namespaces
27+
if agent['namespace'].nil?
28+
resque_namespaces = Resque.redis.keys("*:queues").map { |key| key.gsub(/:queues$/, '').prepend("resque:") }
29+
else
30+
resque_namespaces = [agent['namespace']].flatten
31+
end
32+
33+
34+
resque_namespaces.each_with_object({}) do |rn, hsh|
35+
clone = agent.clone
36+
clone['namespace'] = rn
37+
rn = rn.match(/resque:(.*):staging/)[1].downcase
38+
hsh[rn] = clone
39+
end
40+
41+
else
42+
{agent_key => agent}
43+
end
44+
end
45+
46+
new_agents = agents.reduce({},:merge)
47+
new_agents.delete('dynamic')
48+
new_config = NewRelic::Plugin::Config.config.options
49+
new_config['agents'] = new_agents
50+
YAML.dump(new_config)
51+
end
52+
53+
1054
options = OptionParser.new do |opts|
1155
opts.banner = <<-EOF
1256
Usage:
@@ -42,6 +86,11 @@ if args.first == "run"
4286
puts "Run 'sudo newrelic_resque_agent install' for setup config"
4387
exit 1
4488
end
89+
90+
res = make_dynamic_file.to_s
91+
NewRelic::Plugin::Config.config_file = nil
92+
NewRelic::Plugin::Config.config_yaml = res
93+
4594
NewRelicResqueAgent.run
4695
elsif args.first == "install"
4796
config_file = File.read(File.expand_path("../../config/newrelic_plugin.yml.example", __FILE__))

newrelic_resque_agent/config/newrelic_plugin.yml.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ newrelic:
1818
verbose: 0
1919
#
2020
# Agent Configuration:
21-
#
21+
# setting the agent name to "dynamic" will make one agent per namespace.
22+
2223
agents:
23-
my_resque_1:
24+
my_resque_1: # <= this agent name could be dynamic
2425
# Redis connection string: 'hostname:port' or 'hostname:port:db' or 'redis://user:password@hostname:port:db'
2526
redis: localhost:6379
2627
# Resque namespace

newrelic_resque_agent/lib/newrelic_resque_agent.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
module NewRelicResqueAgent
1111

12-
VERSION = '1.0.1'
12+
VERSION = '1.0.2'
1313

1414
class Agent < NewRelic::Plugin::Agent::Base
1515

@@ -42,8 +42,8 @@ def poll_cycle
4242
report_metric "Jobs/Rate/Failed", "Jobs/Second", @total_failed.process(info[:failed])
4343
report_metric "Queues", "Queues", info[:queues]
4444
report_metric "Jobs/Failed", "Jobs", info[:failed] || 0
45-
46-
45+
46+
4747

4848
rescue Redis::TimeoutError
4949
raise 'Redis server timeout'

newrelic_resque_agent/newrelic_resque_agent.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
1313
## If your rubyforge_project name is different, then edit it and comment out
1414
## the sub! line in the Rakefile
1515
s.name = 'newrelic_resque_agent'
16-
s.version = '1.0.1'
16+
s.version = '1.0.2'
1717
s.date = '2013-07-01'
1818
# s.rubyforge_project = 'newrelic_resque_agent'
1919

0 commit comments

Comments
 (0)