Discussions on Grails melody plugin

314 views
Skip to first unread message

evernat

unread,
Nov 24, 2009, 5:36:52 PM11/24/09
to javamelody
Hello all,

A grails melody plugin which integrates monitoring with JavaMelody in
grails was started by a volunteer (Liu Chao).
Some code for the plugin is here : https://svn.codehaus.org/grails-plugins/grails-grails-melody/trunk/

Certainly other volunteers could help on this plugin. There are
technical aspects on this such as jdbc, but also documentation, tests
with your apps, and usual character pushing. We also need a place to
talk ...


Discussions for this plugin which are more about JavaMelody than
Grails can be in this thread, or in other threads in the google group
of JavaMelody (but certainly in English, even if it is not our native
language). So that we can try to help.

Likewise, issues for this plugin could be entered here :
http://code.google.com/p/javamelody/issues/list until there is another
place for this in codehaus.

Note that there is a user guide for JavaMelody and also a dev guide
(http://code.google.com/p/javamelody/wiki/DevGuide) with a new chapter
to import sources from svn in Eclipse.


Happy hacking !

Emeric
Disclaimer: I do not know Grails yet, so I will let you do all that
part for the moment. It is your turn.

evernat

unread,
Nov 24, 2009, 5:44:45 PM11/24/09
to javamelody
Just to start the talk :

There is an issue on javamelody v1.8.2 in grails here :
http://code.google.com/p/javamelody/issues/detail?id=2

Grails plugins home page is here :
http://xircles.codehaus.org/p/grails-plugins

Grails dev mailing list is here :
http://archive.codehaus.org/lists/org.codehaus.grails.dev

Liu Chao

unread,
Nov 24, 2009, 10:48:00 PM11/24/09
to javam...@googlegroups.com
Current status of the plugin:
1. It is 'usable' but not released yet
2. To use the plugin, you can download the zip file from https://svn.codehaus.org/grails-plugins/grails-grails-melody/trunk/grails-grails-melody-0.1.zip and install
or
check out the source and put them somewhere, such as "d:\grailsmelody". In your BuildConfig.groovy add a line: grails.plugin.location.'grails-melody'="d:\grailsmelody" 
3. In grails dev mode, datasource is not under monitoring
4. I tried to monitor grails services but exception occured
5. other monitoring functions are fine

PSM

unread,
Nov 25, 2009, 6:10:54 AM11/25/09
to javamelody
I wrote a german blog entry about javamelody and the successfull
integration in grails (http://psm-blog.blogspot.com/2009/11/monitoring-
von-jee-anwedungen.html).

@Liu Chao:
Did you manage to get the jdbc monitoring working without switching to
jndi?
Please let me know when the plugin is released, i can test it in my
different grails projects

On 25 Nov., 05:48, Liu Chao <liucha...@gmail.com> wrote:
> Current status of the plugin:
> 1. It is 'usable' but not released yet
> 2. To use the plugin, you can download the zip file fromhttps://svn.codehaus.org/grails-plugins/grails-grails-melody/trunk/gr...
> install
> or
> check out the source and put them somewhere, such as "d:\grailsmelody". In
> your BuildConfig.groovy add a line:
> grails.plugin.location.'grails-melody'="d:\grailsmelody"
> 3. In grails dev mode, datasource is not under monitoring
> 4. I tried to monitor grails services but exception occured
> 5. other monitoring functions are fine
>

Liu Chao

unread,
Nov 25, 2009, 2:48:40 PM11/25/09
to javam...@googlegroups.com
I read through java melody code and believe that I have no way to get datasource under monitoring without switching to jndi. 
I find a way to use jndi datasource in development mode. Just put a jetty-env.xml with jndi datasource configured under WEB-INF and use jndi datasource in developement environment. However this is not a neat solution.

Emeric
Would you be able to change codes so that I can inject datasource of grails into javamelody?

Emeric Vernat

unread,
Nov 25, 2009, 4:22:45 PM11/25/09
to javam...@googlegroups.com
I can add a static setter to inject datasource of grails into javamelody
without problem (we just need to find an appropriate public class, like
JdbcDriver perhaps).
And if you know when to call the method and the type of the datasource
(dbcp.BasicDataSource ?) I can do something like the javamelody method
JdbcWrapper.rewrapDataSource. This method rewrapDataSource is used in
javamelody for application servers where JNDI rebind doesn't work (and
not in jetty because JNDI rebinding in jetty can be made to work).

Otherwise it is good to know that a jndi datasource can be used in
developement environment with jetty-env.xml

Emeric


Liu Chao a �crit :

Emeric Vernat

unread,
Nov 25, 2009, 4:32:32 PM11/25/09
to javam...@googlegroups.com
Note that we need to change the grails datasource : either replace the
datasource in grails or jndi with another equivalent instance or change
something in the initial datasource.
That is because we need to wrap the result of methods getConnection of
the DataSource interface/implementation.

Just giving the grails DataSource to javamelody without replacing or
changing the grails DataSource is not sufficient to monitor jdbc requests.

Emeric

Emeric Vernat a �crit :

Liu Chao

unread,
Nov 26, 2009, 1:30:06 AM11/26/09
to javam...@googlegroups.com
Hi Emeric,

How to wrap datasource of grails with java melody stuff? I can try to do it in plugin. I am reading java melody codes. It would be much better if you can give some advice. ;D

Regards
Liu Chao

On Thu, Nov 26, 2009 at 5:32 AM, Emeric Vernat <eve...@free.fr> wrote:
Note that we need to change the grails datasource : either replace the datasource in grails or jndi with another equivalent instance or change something in the initial datasource.
That is because we need to wrap the result of methods getConnection of the DataSource interface/implementation.

Just giving the grails DataSource to javamelody without replacing or changing the grails DataSource is not sufficient to monitor jdbc requests.

Emeric

Emeric Vernat a écrit :

I can add a static setter to inject datasource of grails into javamelody without problem (we just need to find an appropriate public class, like JdbcDriver perhaps).
And if you know when to call the method and the type of the datasource (dbcp.BasicDataSource ?) I can do something like the javamelody method JdbcWrapper.rewrapDataSource. This method rewrapDataSource is used in javamelody for application servers where JNDI rebind doesn't work (and not in jetty because JNDI rebinding in jetty can be made to work).

Otherwise it is good to know that a jndi datasource can be used in developement environment with jetty-env.xml

Emeric


Liu Chao a écrit :

evernat

unread,
Nov 26, 2009, 9:38:37 AM11/26/09
to javamelody
If you know how to replace the instance of dataSource used in grails,
you can do DataSource dataSourceProxy =
JdbcWrapper.SINGLETON.createDataSourceProxy(dataSource);
and then use this dataSourceProxy to put in grails. (except that
JdbcWrapper is not a public class, so we need to add a public method
createDataSourceProxy in javamelody JdbcDriver for example).

If you cannot replace the instance of dataSource in grails, we will
need to modify the method JdbcWrapper.rewrapDataSource and to call
this method from grails, in order to change some private field in the
dataSource. (Likewise, the method rewrapDataSource is private for now
so we will need to a public method rewrapDataSource in JdbcDriver for
example).

(and if the datasource is a jndi datasource with a jetty-env.xml,
javamelody does the rebinding in jndi to replace the instance of
datasource).

Liu Chao

unread,
Nov 26, 2009, 12:22:45 PM11/26/09
to javam...@googlegroups.com
grails plugin status update:
I add a spring beanPostProcessor to wrap original grails datasource as what Emeric told me -- JdbcWrapper.SINGLETON.createDataSourceProxy(dataSource)
It looks fine. You guys can download new plugin and try it out.

Liu Chao

unread,
Nov 27, 2009, 1:13:11 AM11/27/09
to javam...@googlegroups.com
Hi Emeric,

Services in Grails could be proxied with spring 'TransactionProxyFactoryBean' already. When I apply java melody spring monitoring, some exception occur. Do you have any advice to monitor service methods?

Regards
Liu Chao

evernat

unread,
Nov 28, 2009, 5:41:34 AM11/28/09
to javamelody
Hi Liu,

First, there is something I didn't understand. Are services a
particular concept in grails ? If not, to know wich beans to monitor,
do we use either a java annotation or an interface like said for
javamelody in the user guide (I've seen you have tried that in
doWithSpring) ?

Then monitoring of spring services in javamelody is made by
registering in Spring a javamelody MonitoringSpringInterceptor (which
implements aop MethodInterceptor).
There are several things that help for this in javamelody depending on
the configuration the user wants : MonitoredWithAnnotationPointcut,
MonitoredWithInterfacePointcut, MonitoringSpringAdvisor and monitoring-
spring.xml (you can see javamelody user guide for the configurations).

For grails, I have read on the web that we can define a method
interceptor in conf/spring/resources.xml but I did not find an
example : perhaps you can find an example or post a question in the
dev mailing list of grails (I know I don't help much here).
It seems you have tried MonitoringWithAnnotationPointcut in
doWithSpring, but you got an exception. As I'm curious, what was it ?

bye, Emeric

evernat

unread,
Nov 28, 2009, 6:32:31 AM11/28/09
to javamelody
I finally found an example :
http://code.google.com/p/grails-aop-hello-world/source/browse/trunk/grails-app/conf/spring/resources.groovy

Unfortunately it uses AspectJ syntax and not Spring AOP syntax.

Liu Chao

unread,
Nov 30, 2009, 12:23:04 AM11/30/09
to javam...@googlegroups.com
Interesting example, I will try it out.

Liu Chao

unread,
Nov 30, 2009, 12:36:08 AM11/30/09
to javam...@googlegroups.com
I just browsed the example code. It works because they let grails services implement some interface then do AOP upon that interface. However a lot of grails developers write their services as POJO without implementing any interfaces. I will try groovy meta programming way as a guy reminds me in grails user group.

Liu Chao

unread,
Dec 1, 2009, 3:21:40 AM12/1/09
to javam...@googlegroups.com
Hi Emeric,

I just updated the plugin. 
1. All grails service class loaded before grails-melody plugin are under monitoring (Further changes are required. Plugin user should be able to config what service to be monitored)
2. A j2ee interceptor interface jar file was added in plugin lib. It seems that javamelody spring monitoring has dependency to this interface.

Regards
Liu Chao 

evernat

unread,
Dec 1, 2009, 1:26:34 PM12/1/09
to javamelody
1. It's great !
I thought you wouldn't come with a solution so fast.

Just a question : when you say "loaded before", does this mean
something special to do for users ? (perhaps something to say in a
wiki in the future)

2. I did not see this problem before. But I think I see why it
happens, so I just commited some changes in svn and it should work now
without the interceptor jar file.
You can download the new version with these changes here :
http://javamelody.googlecode.com/files/javamelody-1.10.0-beta.jar

Perhaps we are near a public release of the plugin. What do you
think ?

Emeric
PS: I just understood why you called JdbcWrapper.createDataSourceProxy
directly : groovy does not check visibility. It is quite a difference
with the java language.

Liu Chao

unread,
Dec 1, 2009, 9:29:12 PM12/1/09
to javam...@googlegroups.com
Yup, I think we are close to release the 1st version :D

"loaded before": Grails load plugins in certain order. Our plugin can wrap services in main grails without any problem. However if user want to monitor some services in other plugins, such as searchable plugin, those plugin must be loaded before our grails-melody plugin. I will figure out how to control the order. 

 Another issue is code duplication in spring method wrapping. You can check the "def doWithDynamicMethods" part of code in GrailsMelodyGrailsMelody.groovy file. The code mimic your MonitoringSpringInterceptor.invoke method. It's ok but not so neat. I want to refactor it later.

Groovy is indeed java, it just do things like hacking :D

Regards
Liu Chao

evernat

unread,
Dec 2, 2009, 6:00:51 PM12/2/09
to javamelody
And about the test of the plugin with the beta version of javamelody
and without interceptor jar file ...
There is a new beta version that also changes the default language to
English. It is available here :
http://javamelody.googlecode.com/files/javamelody-1.10.0-beta2.jar

With this beta2, the language of the UI is based on the language of
the browser if a translation is found, or now on the language of the
java server (that is java.util.Locale.getDefault()) if a translation
is found, or to the default translation (that is now English).

That will please our German users which will have English by default
and not French.
(English users do have already the English translation.)

Note: The 1.10.0 final version is planned in 1 or 2 week(s), or before
if needed.

Liu Chao

unread,
Dec 3, 2009, 11:10:15 PM12/3/09
to javam...@googlegroups.com
Hi Emeric,

The interceptor dependency is till there.

Regards
Liu Chao

evernat

unread,
Dec 5, 2009, 12:23:36 PM12/5/09
to javamelody
I've made some changes in trunk of javamelody to reduce the
duplication.

GrailsMelodyGrailsPlugin can become as below with the trunk version.
Could I commit the javamelody.jar and the groovy file in svn ?

And I have grails-bin-1.1.1.zip, how can I test the grails plugin ?
(for the javaee dependency)


def SPRING_COUNTER = MonitoringProxy.getSpringCounter();

...

def doWithDynamicMethods = {ctx ->
//Enable groovy meta programming
ExpandoMetaClass.enableGlobally()
//For each service class in Grails, the plugin use groovy meta
programming (invokeMethod)
//to 'intercept' method call and collect infomation for
monitoring purpose.
//The code below mimics 'MonitoringSpringInterceptor.invoke()'
application.serviceClasses.each {serviceArtifactClass ->
def serviceClass = serviceArtifactClass.getClazz()
serviceClass.metaClass.invokeMethod = {String name, args -
>

def metaMethod = delegate.metaClass.getMetaMethod
(name, args)

if (metaMethod) {

if (DISABLED || !SPRING_COUNTER.isDisplayed()) {
return metaMethod.doMethodInvoke(delegate,
args)
}

final String requestName = "${serviceClass.name}.$
{name}";

boolean systemError = false;
try {
SPRING_COUNTER.bindContextIncludingCpu
(requestName);
return metaMethod.doMethodInvoke(delegate,
args)
} catch (final Error e) {
systemError = true;
throw e;
} finally {
SPRING_COUNTER.addRequestForCurrentContext
(systemError);
}

} else {
throw new MissingMethodException(name,
delegate.class, args)
}

}
}
}

Liu Chao

unread,
Dec 6, 2009, 9:30:11 PM12/6/09
to javam...@googlegroups.com
Hi Emeric,

Sorry for late reply. I was "offline" last weekend. Internet connection in my place was down.

To test the plugin, you can create a simple grails project by following the tutorial
Then check out grails-melody plugin from svn to some directory such as 'D:\grails-melody' .
After that, add a file called 'BuildConfig.groovy' to grails-app/conf directory of your grails project. Add a line of code to that file:
grails.plugin.location.'grails-melody'="D:\grails-melody"

Now you can run 'grails run-app' command in your grails project directory to test.

Emeric Vernat

unread,
Dec 7, 2009, 5:49:57 PM12/7/09
to javam...@googlegroups.com
Thank you Liu,

This was of great help to start with grails and grails-melody plugin.

I was able to run my app, then with grails-melody.
Then I added a service (http://www.grails.org/Services) where Joshua,
the WOPR, says "Hello Falken".
And the service was displayed in the counter statistics ! Definitly cool
for my first grails app.

Notes:
1. Before calling the service, I added the parameter
"displayed-counters=http,sql,error,log,spring"

I think we can add the equivalent of the following in the filter of
doWithWebDescriptor ?
<init-param>
<param-name>displayed-counters</param-name>
<param-value>http,sql,error,log,spring</param-value>
</init-param>
Or do you think it could be way too much and too slow ?

2. It worked with the new javamelody.jar from trunk and the modified
groovy file I posted as text yesterday, and without the javaee
interceptor jar (could we remove it ?).

bye, Emeric
ps: this grails plugin rocks !


Liu Chao

unread,
Dec 7, 2009, 11:46:01 PM12/7/09
to javam...@googlegroups.com
Thant's great! 
For 'displayed-counters' and other parameters, I think I can give user the choice to config in their Config.groovy or other grails config files.
I will try new javamelody.jar. If it works I will remove the interceptor jar.

Liu Chao

unread,
Dec 10, 2009, 11:23:20 AM12/10/09
to javam...@googlegroups.com
Hi Emeric,

I changed the plugin a little bit.
1. When user install the plugin into their grails project, a GrailsMelodyConfig.groovy file will be copied to their grails-app/conf directory
2. All parameters in GrailsMelodyConfig.groovy file will be add as 'init-param' of 'MonitoringFilter'

It seems not working. Please help. This time you should use install-plugin command to install grails-melody plugin into your test grails project instead of using BuildConfig.groovy.

Regards
Liu Chao

On Tue, Dec 8, 2009 at 6:49 AM, Emeric Vernat <eve...@free.fr> wrote:

evernat

unread,
Dec 11, 2009, 2:53:41 PM12/11/09
to javamelody
Hi Liu,

I sent you a fix by email. (I did not commit in svn, because it seems
that I must ask on the mailing list first)

bye, Emeric
Reply all
Reply to author
Forward
0 new messages