Skip to content

Instantly share code, notes, and snippets.

@bkayser
Created May 10, 2012 00:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkayser/2649928 to your computer and use it in GitHub Desktop.
Save bkayser/2649928 to your computer and use it in GitHub Desktop.
Example of monitoring a ruby background job with New Relic
class Account
include NewRelic::Agent::MethodTracer
def check_usage
end
def send_invoice
end
add_method_tracer :check_usage
add_method_tracer :send_invoice
end
require File.expand_path('../../config/application', __FILE__)
JobsExample::Application.initialize!
require 'optparse'
require_dependency 'script/task_instrumentation'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on("-v", "--verbose", "Run with extra logging") { options[:verbose] = true }
end
class Job
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
def initialize(options={})
@verbose = options[:verbose]
end
def run
Account.all.each do | account |
process_account(account)
end
end
private
def process_account
account.check_usage
account.send_invoice
end
add_transaction_tracer :process_account,
:name => 'process_one_account', # Change the label from "process_account" to "process_one_account"
:category => "OtherTransaction/nightly" # Put in the background tasks under "Nightly"
end
# Start the New Relic agent synchronously
NewRelic::Agent.manual_start :app_name => 'Background Jobs', :transaction_tracer => { :transaction_threshold => 1.5 }
status = Job.new(options).run
exit status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment