TOTD #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?
Gem can easily run both Rails and Merb application. Using href="http://jruby.org/">JRuby, as opposed to MRI,
allows you to use standard Java monitoring framework and tools. href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">Java
Management Extensions (JMX) is a standard
management and monitoring solution for Java platform. This style="font-weight: bold;">Tip style="font-weight: bold;">Of style="font-weight: bold;">The style="font-weight: bold;">Day
shows how to apply JMX techniques to monitor Rails/Merb applications.
The blog uses a trivial Rails/Merb application to demonstrate
monitoring but the
techniques can be applied to any Rails or Merb application.
#52 shows to create a simple Merb application and href="http://blogs.sun.com/arungupta/entry/totd_28_getting_started_with">TOTD
#28 shows how to create a simple Rails scaffold.
This blog will focus on local monitoring, i.e. monitor a Rails/Merb
running on the same machine as monitoring tool. A subsequent blog will
demonstrate remote monitoring, i.e. monitor a Rails/Merb application
from a different machine.
There is no extra configuration required to enable monitoring if your
Rails/Merb application is running on GlassFish gem. You just need to
launch an additional tool to monitor the already running application.
is a CLI bundled with JDK and launches a graphical tool to monitor and
manage Java applications on a local or remote machine. jconsole can be
used to monitor local application (those running on the same systems as
jconsole) and remote applications (systems other than running
Invoke "jconsole" in a shell as:
It shows the following window:
The window shows different Java process that can be managed/monitored
by jconsole. "org.jruby.Main -S glassfish" is the process corresponding
to our Rails/Merb application running using GlassFish Gem. Click on
that process (6179 in this case) and click "Connect".
interface explains the different tabs and meaning of
information displayed on each tab. Here is a snapshot of different tabs
for a trivial Rails application.
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-overview.png"> style="border: 0px solid ; width: 700px; height: 547px;" alt=""
It shows heap memory usage, threads, classes loaded, and CPU usage over
a period of time. The "Memory" tab tracks different href="http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html#gc">GC
pools and shows their dynamic growth. Clicking on "Perform
GC" on top-right corner performs a garbage collection (more on this
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-memory.png"> style="border: 0px solid ; width: 700px; height: 577px;" alt=""
The "Threads" tab shows the highest number of threads since JVM started
and current number of live daemon and non-daemon threads. Clicking on a
particular thread shows it's current state and stack trace if it's
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-threads.png"> style="border: 0px solid ; width: 700px; height: 544px;" alt=""
MBeans tab shows different managed beans registered with the platform
MBean server, i.e. MBean server available in the JDK. As you can see
there are several standard MBeans displayed on the left-hand side from
java.lang and java.util.logging domains.
JRuby runtime also exposes 4 different MBeans - ClassCache, Config,
JITCompiler and ParserStats - all in "org.jruby" domain.
each MBean shows detailed information collected by that MBean as shown
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-config-mbean-view.png"> style="border: 0px solid ; width: 700px; height: 544px;" alt=""
Clicking on "Attributes" node shows the current snapshot of all the
attributes exposed by the MBean as shown below:
href="http://blogs.sun.com/arungupta/resource/ror/jconsol-rails-monit-config-attributes-view.png"> style="border: 0px solid ; width: 700px; height: 539px;" alt=""
Clicking on "Refresh" button will read the fresh values from the MBean.
Some MBeans expose functions as well. Such MBeans have "Operations"
node and clicking on them show the functions exposed as shown below:
These functions can be invoked by clicking on the button in right-hand
These snapshots are captured without even a single invocation of the
application. Accessing "http://localhost:3000/" in a browser updates
heap usage screenshot as shown below:
And permgen is increased because new classes are JITed by JRuby as
After a Garbage Collection is performed (by clicking on "Perform GC"
button in "Memory" tab), the updated screenshot looks like:
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-1request-postgc-overview.png"> style="border: 0px solid ; width: 700px; height: 503px;" alt=""
As the snapshot indicates the "Heap Memory Usage" is back to normal
(which indicates no memory leak) and there is a spike in "CPU Usage"
because CPU cycles are used to perform GC.
Monitoring a Merb application is no different at all. The "Overview"
tab for a trivial Merb app looks like:
href="http://blogs.sun.com/arungupta/resource/ror/jconsole-merb-monit-overview.png"> style="border: 0px solid ; width: 700px; height: 546px;" alt=""
The wide range of JMX-compatible
tools is another advantage of using JRuby over MRI :)
Some other alternatives are using a combination of href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jps.html">jps
Home Page provides all details about JMX framework.
Subsequent blogs in this series will discuss:
- How to remotely manage your Rails/Merb applications using
JMX API ?
- How to publish your own metrics that can be managed using
JMX API ?
- How to use VisualVM to get more information about the
underlying VM ?
- How to use NewRelic/FiveRuns to manage/monitor an
How do you monitor your Rails or Merb applications ?
Please leave suggestions on other TOTD (Tip Of The Day) that
you'd like to see.
A complete archive of all tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.