我已尝试按照说明让drop wizard AspectJ指标正常工作,请参阅https://github.com/astefanutti/metrics-aspectj但是,当我按照这些简单的说明并通过JConsole mbeans查看指标时,没有为我的带注释的服务报告指标,只是指标对于资源,默认的放置向导行为。
我正在使用旧版本的drop wizard,因此com.codahale.metrics artefacts没有jar依赖性冲突
构建应用程序时,按下以下url会生成随机字符串http://localhost:8080/string
这是我的简单示例代码:
的pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test project</name>
<description>try and get aspect j metrics working in dropwizard following instructions on https://github.com/astefanutti/metrics-aspectj</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.7.1</version>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<!--
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>org.aspectj.weaver.loadtime.Agent</Premain-Class>
<Main-Class>com.example.MyApplication</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
MyApplication.java
package com.example;
import com.example.service.MyService;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception {
MyApplication app = new MyApplication();
app.run(args);
}
@Override
public void initialize(Bootstrap<MyConfiguration> arg0) {
}
@Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new MyResource(new MyService()));
}
}
MyConfiguration.java
package com.example;
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration {
}
MyResource.java
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.codahale.metrics.annotation.Timed;
import com.example.service.MyService;
@Path("/string")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
private final MyService service;
public MyResource(MyService service) {
this.service = service;
}
@GET
@Timed
public String getString() {
return "string is [" + this.service.constructString() + "]";
}
}
MyService.java
package com.example.service;
import java.util.Random;
import com.codahale.metrics.annotation.Timed;
import io.astefanutti.metrics.aspectj.Metrics;
@Metrics(registry = "myRegistry")
public class MyService {
private final Random random = new Random();
@Timed(name = "myTimedMethod")
public String constructString() {
byte[] bytes = new byte[20];
this.random.nextBytes(bytes);
return new String(bytes);
}
}
example.yml
logging:
level: INFO
我使用标准maven命令在命令行上构建着色jar:
mvn clean install
使用以下命令运行jar:
java -jar -javaagent:<path-to-maven-repo>/org/aspectj/aspectjweaver/1.8.7/aspectjweaver-1.8.7.jar target/test-project-0.0.1-SNAPSHOT.jar server example.yml
我不太确定我错过了什么但是搜索的一天并没有产生任何有用的见解。
答案 0 :(得分:0)
事实证明,您需要做的就是在JmxReporter中注册MetricRegistry,以便在JMX上发布指标。
似乎在最新版本的drop wizard中你应该能够创建一个MetricRegistry并将其添加到应用程序初始化方法中的bootstrap指标
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
MetricRegistry myRegistry = SharedMetricRegistries.getOrCreate("myRegistry");
MetricRegistry bootstrapMetricRegistry = bootstrap.getMetricRegistry();
bootstrapMetricRegistry.register("myRegistry", myRegistry);
}
然后当它自动放入bootstrap的registerMetrics方法中的JMX报告器时,但这似乎不起作用。
我目前的解决方案如下,在应用程序的run方法中从共享注册表中获取注册表并构建另一个JmxReporter。现在,在通过JMX连接时,会显示出度量标准。
@Override
public void run(MyConfiguration configuration, Environment environment) throws Exception {
...
JmxReporter.forRegistry(SharedMetricRegistries.getOrCreate("myRegistry")).build().start();
...
}