Tracking Hibernate statistics across Grails actions
About a year ago I stumbled upon a great post by Himanshu Seth on the IntelliGrape website about tracking Hibernate stats across Grails actions/requests. It involves: adding a new Grails controller filter to enable stats before a request is processed, letting Grails execute the action, and then logging the stats after the action is finished. I’ve found it very useful as an at-a-glance tool to determine if the work I’ve done in a controller or service has affected the total number of transactions, flush counts, or other performance metrics before and after the change. Since I’ve started using this filter in my code, I’ve added a few extra features that fit my needs and thought I’d share them in case they’re useful.
Let’s start with a (slightly modified) version of the original code, added to the filters portion of ControllerFilters.groovy:
Note the added comment on the stats.clear() call — we assume that our code is the only one using the Hibernate stats, so if there’s any sort of logging going on independently, the clear() method may skew that data. Furthermore, this filter is intended for development environments and as such should not typically be enabled in prod. Along with that, I’ve removed some of the stats I don’t typically need, and added logging of the controller name and action name that the request was sent to. The first call (on line 9) is particularly useful when logSql is enabled, as it gives you a rough idea of which queries were made in an action. Finally, I changed the println() to a log.info() call instead, so be sure that your logging for ‘grails.app.filters’ is set to ‘info’ or lower in the config. Now that we’ve established a new baseline, let’s add a couple of new features.
Logging the elapsed time
Having a timer on the action so we have a general idea of how long the request took could be useful. We need a way to transmit the start time from the ‘before’ to the ‘afterView’ filter, though, and the request itself seems like a well-scoped object to do that. Once we have that, we can subtract the start time from the current time to get the total.
Pretty straightforward, but it’s helpful nonetheless.
The last thing I felt I needed was a way to selectively enable Hibernate statistics since there does seem to be a minor performance hit with them and the logging isn’t always needed. Since the filter acts on individual requests, it makes sense to send the “start tracking” signal via HTTP parameters. At the same time, since this feature is intended only for development, we should have a way to enable/disabling the filter per environment. We can do this via the config by creating a new property in Config.groovy:
As visible from the above snippet, if we set the value to “ALLOWED”, we can invoke the Hibernate logging whenever we need by appending ‘logHibernateStats’ to the request’s params (similar to the _debugResources parameter for the Grails Resources plugin) such as localhost:8080/mySite/controller/action?logHibernateStats=true. If we don’t send that parameter, then the stats are disabled. If the value is set to “ALWAYS”, then each request will be logged, without the need to send the parameter key. Otherwise we can set the value to “NEVER” for environments like production, so that we do not need to comment out the code when we aren’t using it.
Now we have our final iteration of the filter:
Just a couple points to remember — the logs may not be accurate if you have multiple requests processing in rapid succession, and as such, the filter works best when isolating one controller action at a time. Secondly, the filter assumes that it is the only code using Hibernate stats, so if you use them elsewhere, you may need to more selectively clear or disable the stats.