Skip to main content

TOTD #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?

Posted by arungupta on January 7, 2009 at 9:17 AM PST


cellspacing="5">
style="width: 200px; height: 127px;" alt=""
src="http://upload.wikimedia.org/wikipedia/en/4/41/C128_ML_Monitor.gif">
GlassFish
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.
FYI  href="http://blogs.sun.com/arungupta/entry/getting_started_with_merb_using">TOTD
#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
application
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.



href="http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html">jconsole
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
jconsole).



Invoke "jconsole" in a shell as:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
jconsole



It shows the following window:



src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-connection.png">



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".



The href="http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html#interface">jconsole
interface explains the different tabs and meaning of
information displayed on each tab. Here is a snapshot of different tabs
captured
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=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-overview.png">



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
later).



href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-memory.png"> style="border: 0px solid ; width: 700px; height: 577px;" alt=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-memory.png">



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
waiting.



href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-threads.png"> style="border: 0px solid ; width: 700px; height: 544px;" alt=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-threads.png">



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.



src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-mbeans-view.png">



JRuby runtime also exposes 4 different MBeans - ClassCache, Config,
JITCompiler and ParserStats - all in "org.jruby" domain.
Clicking on
each MBean shows detailed information collected by that MBean as shown
below:



href="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-config-mbean-view.png"> style="border: 0px solid ; width: 700px; height: 544px;" alt=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-config-mbean-view.png">



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=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsol-rails-monit-config-attributes-view.png">



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:



src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-classcache-mbean-functions.png">



These functions can be invoked by clicking on the button in right-hand
panel.



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:



src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-1request-heap.png">



And permgen is increased because new classes are JITed by JRuby as
shown below:



src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-1request-permgen.png">





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=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-rails-monit-1request-postgc-overview.png">




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=""
src="http://blogs.sun.com/arungupta/resource/ror/jconsole-merb-monit-overview.png">



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
and jstat.



The href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX
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
    application ?

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.




Technorati: totd
glassfish
jruby

rubyonrails
merb

jmx
manage
monitor

Related Topics >>

Comments

Cross Language Profiling with JXInsight - JRuby-to-Ruby http://williamlouth.wordpress.com/2008/10/14/cross-language-profiling-wi...