This project uses the New Relic Ruby agent to demonstrate the reporting of code level metrics (CLM).
There is a Ruby on Rails 7 based web application contained in the rails7
directory. This application is designed to perform
CRUD
operations for fictitious future New Relic language agents.
By running the demo, the Ruby on Rails 7 application will be launched and
daemonized and a tester
shell script will perform curl
commands that generate web traffic to exercise all traced Ruby methods.
The following Rails class files define methods that will be invoked and produce code level metrics.
app/controllers/agents_controller.rb
: A fairly standard Rails 7 controller class whose methods will automatically store CLM data in segments when calledapp/controllers/admin/settings_controller.rb
: This controller exists to demonstrate directory nesting.lib/custom_helpers.rb
: A demonstration class file featuring 2 class (static) methods and 2 instance methods that will produce CLM data because of the transaction and method tracers that are applied to themlib/which_is_which.rb
: A class that bears both aself.samename
(class method) andsamename
instance method.app/jobs/notifier_job.rb
: An ActiveJob (background job processing) class featuring aperform
method.
The rails7/app/controllers/agents_controller.rb
file defines the
AgentsController
class. This class has 3 instance methods that are exercised
by the demo, create
, destroy
, and show
.
These 3 methods produce the following New Relic trace names:
Controller/agents/create
Controller/agents/destroy
Controller/agents/show
The rails7/app/controllers/admin/settings_controller.rb
file defines the
Admin::SettingsController
class. The file demonstrates a namespaced
controller class located in a nested directory beneath controllers
. The
controller class defines a single instance method, index
.
The index
method produces the following New Relic trace name:
Controller/admin/settings/index
The rails7/app/jobs/notifier_job.rb
file defines the NotifierJob
class.
This ActiveJob based class has the standard perform
instance method that
serves as the single point of public entry into the job class.
CLM spans for this class will appear in New Relic as "non-web". The perform
method produces the following New Relic trace name:
OtherTransaction/ActiveJob::Async/NotifierJob/execute
NOTE: The "Async" part of the trace name can be different between instances of Rails applications. Starting with Ruby on Rails version 5, "Async" is the default ActiveJob queue adapter, but it should not be used for production traffic. In production it is likely that another background job adapter will be in play. See the complete list of adapters for details.
NOTE: The "execute" part of the trace name is hardcoded and will not change between jobs.
The rails7/lib/custom_helpers.rb
file defines the Custom::Helpers
class.
This generic Ruby class does not contain any Rails or other non standard library
code. In a non demo app, this type of file could be stored anywhere the Rails
app has read access to.
The custom helpers file defines 2 class methods, custom_class_method
and
custom_class_method_too
, and 2 instance methods, custom_instance_method
,
and custom_instance_method_too
Each of the 2 methods without "too" in the name invoke the 2 methods with "too" in the name, so the metrics for the "too" methods will be nested by New Relic accordingly.
The 4 methods produce the following New Relic trace names:
OtherTransaction/Background/Custom::Helpers/custom_class_method
Custom/CLMtesting/ClassMethod
(forcustom_class_method_too
)OtherTransaction/Background/Custom::Helpers/custom_instance_method
Custom/CLMtesting/InstanceMethod
(forcustom_instance_method_too
)
The rails7/lib/which_is_which.rb
file defines the WhichIsWhich
class.
This class exists solely to demonstrate a Ruby class that has both a class
method and an instance method with the same name. The class possesses a
self.samesame
class method an a samesame
instance method.
Due to the way the New Relic Ruby agent currently traces Ruby methods, both
of these methods will use the same trace name. The code.function
and
code.lineno
CLM attributes will serve to differentiate the traces. The common
trace name is:
OtherTransaction/Background/WhichIsWhich/samename
- Clone this repository
- Launch a terminal emulator and change to the directory of the repository clone
- Set your New Relic license key via
export NEW_RELIC_LICENSE_KEY=the_key
- Optionally set a non-production New Relic host via
export NEW_RELIC_HOST=the_host
- Start Docker Compose via
make up
ordocker-compose up -d --build
- Inspect the Rails and New Relic agent logs that appear in the
logs
subdirectory - Leave the demo running for as long as you'd like
- Stop Docker Compose via
make down
ordocker-compose down
- Optionally delete old logs and other temporary content via
make clean
- Optionally discard the Docker image via
make rmi
- If the Ruby agent was successful in posting data to New Relic, the data
should soon be available in the web UI. The URL can be obtained via
make url
.
- Optionally run
make clean
and/orgit clean
to remove unwanted content - Update your local copy of the demo git clone
- Make sure
rails7/Gemfile.lock
does not exist (ifmake clean
was not used) - Remove any old demo Docker images with either
make rmi
ordocker
- Proceed to step 3 of the demo instructions
- With Ruby version 2.7.0 or newer, the Rails 7
app in the
rails7
subdirectory can be ran without Docker - To run the Rails 7 app in console mode, change to the
rails7
subdirectory, runbundle
and then runbundle exec rails console
- To test the Docker image with a Bash shell, override the entrypoint like so:
docker run --rm -it --entrypoint bash ruby-agent-clm-demo
- The SQLite based database is seeded with content
in the
rails7/db/seeds.rb
file - The database schema is defined in
rails7/db/schema.rb
- The
rails7/tester
script uses Bash, curl, and jq and can be ran locally if you have all of those installed - puts statements can be added to any of
the
.rb
files inolved in the web application's CRUD operations to print output to the Rails log file