我有大约20个API,我想为每个API实现执行时间,响应计数等统计信息。在做了一些研究后,我发现dropwizard指标是实现这些功能的最佳方法。我正在使用Spring MVC框架(不可引导)。任何人都可以建议我如何将Metrics集成到Spring MVC框架中?
如果可能,请提供任何代码作为参考。
答案 0 :(得分:4)
您可以使用Metrics for Spring。这是一个github link,它解释了如何将它与Spring MVC集成。 metrics-spring模块将Dropwizard Metrics library与Spring集成,并提供XML和Java配置。
<强>的Maven 强>
当前版本为3.1.2,与Metrics 3.1.2
兼容
<dependency>
<groupId>com.ryantenney.metrics</groupId>
<artifactId>metrics-spring</artifactId>
<version>3.1.2</version>
</dependency>
基本用法
从版本3开始,可以使用XML或Java配置metrics-spring, 取决于您的个人喜好。
XML配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics.xsd">
<!-- Creates a MetricRegistry bean -->
<metrics:metric-registry id="metricRegistry" />
<!-- Creates a HealthCheckRegistry bean (Optional) -->
<metrics:health-check-registry id="health" />
<!-- Registers BeanPostProcessors with Spring which proxy beans and capture metrics -->
<!-- Include this once per context (once in the parent context and in any subcontexts) -->
<metrics:annotation-driven metric-registry="metricRegistry" />
<!-- Example reporter definiton. Supported reporters include jmx, slf4j, graphite, and others. -->
<!-- Reporters should be defined only once, preferably in the parent context -->
<metrics:reporter type="console" metric-registry="metricRegistry" period="1m" />
<!-- Register metric beans (Optional) -->
<!-- The metrics in this example require metrics-jvm -->
<metrics:register metric-registry="metricRegistry">
<bean metrics:name="jvm.gc" class="com.codahale.metrics.jvm.GarbageCollectorMetricSet" />
<bean metrics:name="jvm.memory" class="com.codahale.metrics.jvm.MemoryUsageGaugeSet" />
<bean metrics:name="jvm.thread-states" class="com.codahale.metrics.jvm.ThreadStatesGaugeSet" />
<bean metrics:name="jvm.fd.usage" class="com.codahale.metrics.jvm.FileDescriptorRatioGauge" />
</metrics:register>
<!-- Beans and other Spring config -->
</beans>
Java配置:
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
@Configuration
@EnableMetrics
public class SpringConfiguringClass extends MetricsConfigurerAdapter {
@Override
public void configureReporters(MetricRegistry metricRegistry) {
// registerReporter allows the MetricsConfigurerAdapter to
// shut down the reporter when the Spring context is closed
registerReporter(ConsoleReporter
.forRegistry(metricRegistry)
.build())
.start(1, TimeUnit.MINUTES);
}
}
的更多内容
答案 1 :(得分:2)
正如已经建议的那样,Metrics Spring提供了一些与Spring有趣的集成。如果您想从JSON API访问这些指标,您仍需要按照http://metrics.dropwizard.io/3.1.0/manual/servlets/中的说明添加servlet。
为了使用这些servlet,您需要添加依赖项:
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-servlets</artifactId>
<version>${metrics.version}</version>
</dependency>
然后在web.xml中添加servlet:
<servlet>
<servlet-name>metrics-admin</servlet-name>
<servlet-class>com.codahale.metrics.servlets.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>metrics-admin</servlet-name>
<url-pattern>/metrics/admin/*</url-pattern>
</servlet-mapping>
您也可以使用JavaConfig进行配置。
注册servlet:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.codahale.metrics.servlets.AdminServlet;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
ServletRegistration.Dynamic metricsServlet = servletContext.addServlet("metrics", new AdminServlet());
metricsServlet.addMapping("/metrics/admin/*");
}
}
并提供servlet所需的属性:
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.servlets.HealthCheckServlet;
import com.codahale.metrics.servlets.MetricsServlet;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
@Configuration
@EnableMetrics
public class MetricsConfiguration extends MetricsConfigurerAdapter {
@Autowired ServletContext servletContext;
@Autowired
private HealthCheckRegistry healthCheckRegistry;
@Override
public void configureReporters(MetricRegistry metricRegistry) {
registerReporter(ConsoleReporter
.forRegistry(metricRegistry)
.build())
.start(1, TimeUnit.MINUTES);
servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);
servletContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthCheckRegistry);
}
}
答案 2 :(得分:0)
我对上述答案有几点补充。
您需要在WebInitializer中将MetricsConfiguration注册为RootConfigClasses,否则将无法加载。
我在Spring版本(4.2.5)中发现AOP版本与metrics-spring不匹配导致ClassNotFoundException。简单地将spring-aop排除在你的pom中作为metrics-spring的依赖。
最后,您可以轻松实现自己的Metrics Controller:
@Controller
public class MetricsContoller {
@Autowired
private ServletContext servletContext;
@RequestMapping(value="/metrics", method=RequestMethod.GET)
public @ResponseBody MetricRegistry saveTestStep() throws ServletException {
final Object registryAttr = servletContext.getAttribute(MetricsServlet.METRICS_REGISTRY);
if (registryAttr instanceof MetricRegistry) {
return (MetricRegistry) registryAttr;
} else {
throw new ServletException("Couldn't find a MetricRegistry instance.");
}
}
}