2016-03-02 3 views
2

Я пытаюсь настроить AspectJ for Metrics в простой проект java. Я добавил необходимые зависимости в pom.xml. Когда я делаю mvn compile, Я получаю следующие предупреждения. В нем говорится, что совет не применяется. Где я буду неправильноМетки метрики Drop с рекомендациями AspectJ throws не были применены

[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 

Это мое 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> 

Вот мой код:

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(); 
     } 
    } 

} 

ответ

2

Предупреждений только означает, что в библиотеке инструментов некоторые аспекты определен точечные стрелки которых не срабатывают и которые, таким образом, не были применены. Рассматривая свой код, вы действительно не используете его или не используете его в тех местах, где их не следует применять, поэтому предупреждения просто описывают ситуацию. Подробно:

  • @Metrics: применяется, без предупреждения.
  • @Timed: не применяется, потому что используется на статическом методе, таким образом, предупреждение. Если вы посмотрите на aspect source code, вы увидите, что pointcut нацелен на нестатические методы: execution(@Timed !static * (@Metrics Profiled+).*(..))
  • @Metered: не применяется, потому что используется на статическом методе, таким образом, предупреждение. Если вы посмотрите на aspect source code вы видите, что только цель среза точек нестатических метод: execution(@Metered !static * (@Metrics Profiled+).*(..))

Просто удалите static из метода test() и посмотреть, что происходит, когда вы перекомпилировать. ;-)

+0

Есть ли способ применить его к 'static' методам? как мы должны отслеживать статические методы? – simplyblue

+0

Здесь, в документах, говорится, что он может применяться к методам 'static': https://github.com/astefanutti/metrics-aspectj – simplyblue

+0

Здесь реализован аспект' TimedStatic', -> https://github.com /astefanutti/metrics-aspectj/blob/master/impl/src/main/aspect/io/astefanutti/metrics/aspectj/TimedStaticAspect.aj – simplyblue

Смежные вопросы