Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active July 14, 2023 19:30
Show Gist options
  • Save fallwith/6dcec95aceda7aa1447d5005ddea515a to your computer and use it in GitHub Desktop.
Save fallwith/6dcec95aceda7aa1447d5005ddea515a to your computer and use it in GitHub Desktop.
New Relic Ruby agent Sidekiq demonatration

New Relic Ruby agent Sidekiq demonstration

This guide will demonstrate using the New Relic Ruby agent to report metrics for Sidekiq jobs being performed by workers. A minimal Ruby on Rails app with a minimal Sidekiq job class will be created.

Prerequisites

required

  • Ruby
  • Redis

optional

  • timeout
  • grep

You will need a machine with a Ruby installation and a running Redis server.

This demonstration was originally tested with Ruby 2.7.8 and Redis 7.0.12. Additionally Ruby on Rails 6.1.7.4 and Sidekiq 6.5.9 will be automatically downloaded. Feel free to modify those version numbers in the demo script below.

Make sure the Redis server is running and that Ruby's bundle binary is in your PATH before continuing.

Walkthrough

  1. Make a copy of the demo script below
  2. Edit your copy of the script to replace the "(YOUR NEW RELIC LICENSE KEY)" text with your New Relic license key. Be careful not to expose this key.
  3. If your system does not contain the timeout binary, remove the timeout 60 part of the demo script and manually hit CTRL-c to stop Sidekiq after 1 minute.
  4. Open a unix terminal prompt and paste your modified script at the prompt.

If all goes well, you will end up with grep output that demonstates that metrics related to the An::ExampleJob Sidekiq job were sent to New Relic.

If that output does not appear...

  • Check your New Relic license key
  • Inspect the newly created test/demo/log/newrelic_agent.log
  • Inspect the newly created test/demo/log/newrelic_audit.log

Demo Script

export NEW_RELIC_LICENSE_KEY=(YOUR NEW RELIC LICENSE KEY)
export NEW_RELIC_AUDIT_LOG_ENABLED=true
mkdir test
cd test
bundle init
bundle add rails --version 6.1.7.4
bundle exec rails new demo --api --minimal --skip-git \
  --skip-action-mailer --skip-action-cable \
  --skip-javascript --skip-test  
cd demo
bundle add sidekiq --version 6.5.9
bundle add newrelic_rpm
bundle exec rails generate sidekiq:job an/example
bundle exec rails runner "An::ExampleJob.perform_async('testing')"
# an alternative to using the timeout wrapper is to simply let Sidekiq
# run for 1 min before you use CTRL-c to stop it
timeout 60 bundle exec sidekiq
grep ExampleJob log/newrelic_audit.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment