TOTD #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?
By arungupta on Jan 06, 2009
|GlassFish Gem can easily run both Rails and Merb application. Using JRuby, as opposed to MRI, allows you to use standard Java monitoring framework and tools. Java Management Extensions (JMX) is a standard management and monitoring solution for Java platform. This Tip Of The 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 TOTD #52 shows to create a simple Merb application and 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.
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:
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".
The 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.
It shows heap memory usage, threads, classes loaded, and CPU usage over a period of time. The "Memory" tab tracks different GC pools and shows their dynamic growth. Clicking on "Perform GC" on top-right corner performs a garbage collection (more on this later).
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.
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. Clicking on each MBean shows detailed information collected by that MBean as shown below:
Clicking on "Attributes" node shows the current snapshot of all the attributes exposed by the MBean as shown below:
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 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:
And permgen is increased because new classes are JITed by JRuby as shown below:
After a Garbage Collection is performed (by clicking on "Perform GC" button in "Memory" tab), the updated screenshot looks like:
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:
The wide range of JMX-compatible tools is another advantage of using JRuby over MRI :)
Some other alternatives are using a combination of jps and jstat.
The 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 ?
Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all tips is available here.
Technorati: totd glassfish jruby rubyonrails merb jmx manage monitor