I'm trying to set up AspectJ for Metrics in a simple java project.
I have added the required dependencies in pom.xml. When i do mvn compile
,
I get the following warnings. It says, the advice is not applied. Where am i going wrong
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] advice defined in io.astefanutti.metrics.aspectj.TimedAspect has not been applied [Xlint:adviceDidNotMatch]
/root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/TimedAspect.class:26
[WARNING] advice defined in io.astefanutti.metrics.aspectj.ExceptionMeteredAspect has not been applied [Xlint:adviceDidNotMatch]
/root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/ExceptionMeteredAspect.class:26
[WARNING] advice defined in io.astefanutti.metrics.aspectj.MeteredAspect has not been applied [Xlint:adviceDidNotMatch]
/root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/MeteredAspect.class:26
[WARNING] advice defined in io.astefanutti.metrics.aspectj.ExceptionMeteredStaticAspect has not been applied [Xlint:adviceDidNotMatch]
/root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/ExceptionMeteredStaticAspect.class:26
This my pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-graphite</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<source>1.6</source>
<target>1.6</target>
<aspectLibraries>
<aspectLibrary>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>dropwizard.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here is my code:
package dropwizard;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import io.astefanutti.metrics.aspectj.Metrics;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
/**
* Hello world!
*
*/
@Metrics(registry = "graphiteregistry2")
public class App
{
static final MetricRegistry registry = new MetricRegistry();
public static void main(String args[]) {
startReport();
test();
wait5Seconds();
}
static void startReport() {
final Graphite graphite = new Graphite(new InetSocketAddress("127.0.0.1", 2003));
final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
.prefixedWith("test8.example.com")
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(graphite);
reporter.start(1, TimeUnit.SECONDS);
ConsoleReporter reporter1 = ConsoleReporter.forRegistry(registry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter1.start(3, TimeUnit.SECONDS);
SharedMetricRegistries.add("graphiteregistry2", registry);
}
static void wait5Seconds() {
try {
Thread.sleep(5*1000);
}
catch(InterruptedException e) {}
}
@Timed(name = "test8method")
@Metered(name = "methodmeter")
static void test() {
//Timer.Context time = responses.timer("test8.update").time();
System.out.println("inside test");
try {
for(int i=0;i<10000;i++){}
}
finally {
//time.stop();
}
}
}
The warnings only mean that in the tool library some aspects are defined the pointcuts of which do not fire and which thus have not been applied. Looking at your code, you really do not use those or use them in places where they should not be applied, so the warnings just describe the situation. In detail:
@Metrics
: is applied, no warning.@Timed
: is not applied because used on static method, thus a warning. If you look at the aspect source code you see that the pointcut only targets non-static methods: execution(@Timed !static * (@Metrics Profiled+).*(..))
@Metered
: is not applied because used on static method, thus a warning. If you look at the aspect source code you see that the pointcut only targets non-static methods: execution(@Metered !static * (@Metrics Profiled+).*(..))
Remove the static
from your test()
method and see what happens when you recompile.