Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active March 28, 2023 00:00
Show Gist options
  • Save fallwith/84a957acfff6497363968ebd3ec9c281 to your computer and use it in GitHub Desktop.
Save fallwith/84a957acfff6497363968ebd3ec9c281 to your computer and use it in GitHub Desktop.
New Relic Ruby agent demo: set an error group callback used for noticed errors
#!/usr/bin/env ruby
# frozen_string_literal: true
# INSTRUCTIONS:
# - Place this file somewhere. It can be called something like `reporter.rb`
#
# - Either place a newrelic.yml file in the same directory as this file OR
# export the NEW_RELIC_LICENSE_KEY and NEW_RELIC_HOST environment variables.
# If using a newrelic.yml file, it should look like this:
# # newrelic.yml
# development:
# license_key: <YOUR KEY>
# host: <YOUR ENVIRONMENT>-collector.newrelic.com
#
# - Optionally update the ERROR_GROUP_NAMES constant below. At least 1 error
# per group name will be created.
#
# - Run this script with `ruby reporter.rb`
# (or make it executable and use `./reporter.rb`)
require 'bundler/inline'
puts 'Checking out the Ruby agent with Bundler...'
gemfile do
source 'https://rubygems.org'
gem 'newrelic_rpm', git: 'https://github.com/newrelic/newrelic-ruby-agent',
branch: 'dev'
end
#
# ErrorReporter - A standalone error reporter demonstrator
#
class ErrorReporter
ERROR_GROUP_NAMES = %w[MyCoolGroup MyTroublesomeGroup MyFamousGroup].freeze
SHUTDOWN_SLEEP = 60
LOG_FILE = 'log/newrelic_agent.log'
def go
wait_for_connection
register_error_group_callback
create_some_errors
graceful_shutdown
check_log
end
private
def wait_for_connection
puts 'Waiting for New Relic agent to connect...'
puts "NOTE: If the connection process takes long, kill the script and check #{LOG_FILE} for WARN/ERROR"
sleep 1 until NewRelic::Agent.agent.connected?
end
def register_error_group_callback
puts 'Registering error group callback proc...'
NewRelic::Agent.set_error_group_callback(callback_proc)
end
def callback_proc
proc { |hash| Regexp.last_match(1) if hash[:error].message =~ /(#{ERROR_GROUP_NAMES.join('|')})/ }
end
def create_some_errors
puts 'Creating some errors...'
ERROR_GROUP_NAMES.each do |name|
rand(1..5).times do
error = StandardError.new("Here's a #{name} type error!")
NewRelic::Agent.notice_error(error)
end
end
end
def graceful_shutdown
puts "Giving the agent harvester time to send data up to the collector (sleep = #{SHUTDOWN_SLEEP} secs)..."
sleep SHUTDOWN_SLEEP
puts 'Shutting down New Relic agent...'
NewRelic::Agent.shutdown
end
def check_log
if log_data =~ %r{INFO : Reporting to: (https://.*?)(\s+|$)}
puts "Data was submitted to #{Regexp.last_match(1)}"
else
puts "Couldn't glean a URL from the #{LOG_FILE} file. Check that file for WARN/ERROR"
end
end
def log_data
return File.read(LOG_FILE) if File.exist?(LOG_FILE)
puts "The agent log file #{LOG_FILE} didn't appear to get created."
''
end
end
ErrorReporter.new.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment