【问题标题】:Drop wizard AspectJ metrics does not report metrics via JConsole MBeans删除向导 AspectJ 指标不通过 JConsole MBean 报告指标
【发布时间】:2016-07-13 09:16:43
【问题描述】:

我已尝试按照说明使放置向导 AspectJ 指标正常工作,请参阅 https://github.com/astefanutti/metrics-aspectj 但是,当我按照这些简单说明并通过 JConsole mbeans 查看指标时,没有为我的注释服务报告指标,只有资源是默认的放置向导行为。

我使用的是旧版本的放置向导,因此不会与 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 

我不太确定自己错过了什么,但花了一天的时间搜索并没有产生任何有用的见解。

【问题讨论】:

    标签: java aop aspectj dropwizard code-metrics


    【解决方案1】:

    所以事实证明,您需要做的是向 JmxReporter 注册您的 MetricRegistry,以便在 JMX 上显示指标。

    似乎在最新版本的拖放向导中,您应该能够创建一个 MetricRegistry 并将其添加到应用程序的初始化方法中的引导指标中

    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        MetricRegistry myRegistry = SharedMetricRegistries.getOrCreate("myRegistry");
        MetricRegistry bootstrapMetricRegistry = bootstrap.getMetricRegistry();
        bootstrapMetricRegistry.register("myRegistry", myRegistry);
    }
    

    然后当它自动放入引导程序的 registerMetrics 方法中的 JMX 报告器时,但这似乎不起作用。

    我目前的解决方案如下,在应用程序的run方法中,从共享注册表中抓取注册表并构建另一个JmxReporter。通过 JMX 连接时,现在可以愉快地显示指标。

    @Override
    public void run(MyConfiguration configuration, Environment environment) throws Exception {
        ...
        JmxReporter.forRegistry(SharedMetricRegistries.getOrCreate("myRegistry")).build().start();
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多