Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active January 22, 2025 23:38
Show Gist options
  • Save fallwith/de08b2d55e29e9413fc318a8ebf9fe6c to your computer and use it in GitHub Desktop.
Save fallwith/de08b2d55e29e9413fc318a8ebf9fe6c to your computer and use it in GitHub Desktop.
Using the New Relic Ruby agent with a standalone Ruby script

Instructions

  1. Download the standalone.rb file to your computer. Place it in an empty directory by itself. The file does not need to be made executable. The first "shebang" line of the file will be ignored, so the path is not important.
  2. Create a newrelic.yml file or copy an existing one from an existing project to the directory containing standalone.rb
  3. At a comand prompt in the directory, run ruby standalone.rb
  4. If the standalone test script is successful, its output will look like this:
Starting New Relic agent...
Waiting for New Relic agent to connect...
Looping application routine 5 time(s)...
Giving the New Relic agent harvester time to send data up (sleep = 60 secs)...
Shutting down New Relic agent...
  1. After the script finishes running, the directory should now contain a new log subdirectory along with two new log files, log/newrelic_agent.log and log/newrelic_audit.log

Troubleshooting

  1. If the console/terminal output looks different than the example shown above, please let New Relic know what the output was.
  2. Look for any occurrences of WARN or ERROR (in all caps) in the log/newrelic_agent.log file and let New Relic know about them.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'newrelic_rpm', require: false
end
require 'new_relic/agent'
require 'tasks/newrelic'
module The
class Example
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
include ::NewRelic::Agent::MethodTracer
def self.class_method; end
class << self
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
include ::NewRelic::Agent::MethodTracer
add_transaction_tracer :class_method, category: :task
end
def instance_method; end
add_transaction_tracer :instance_method, category: :task
end
end
puts 'Starting New Relic agent...'
NewRelic::Agent.manual_start
puts 'Waiting for New Relic agent to connect...'
sleep 1 until NewRelic::Agent.agent.connected?
loops = (ARGV.first || 5).to_i
puts "Looping application routine #{loops} time(s)..."
loops.times do
The::Example.class_method
The::Example.new.instance_method
end
sleep_time = 60
puts "Giving the New Relic agent harvester time to send data up (sleep = #{sleep_time} secs)..."
sleep sleep_time
puts 'Shutting down New Relic agent...'
::NewRelic::Agent.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment