如何使用Dropwizard指标和spring-mvc实施统计信息 [英] How to implement statistics using dropwizard metrics and spring-mvc
问题描述
我大约有20个API,我想实现每个API的统计信息,例如执行时间,响应计数.经过研究,我发现 dropwizard 指标是最好的方法用于实现此类功能.我正在使用Spring MVC框架(不可引导).有人可以建议我如何将Metrics集成到Spring MVC框架吗?
I am having about 20 APIs and I want to implement statistics like execution time, responses count .. for each API. After doing some research, I came to know that dropwizard metrics is the best approach for implementing such functionalities. I am using Spring MVC framework (non-bootable). Can anybody please suggest me how to integrate Metrics to Spring MVC framework?
请尽可能提供任何代码作为参考.
If possible please provide any code as a reference.
推荐答案
您可以使用 Spring的指标.这是一个 github链接,其中说明了如何将其与Spring MVC集成. metrics-spring模块将 Dropwizard Metrics库与Spring集成在一起,并提供XML和Java配置.
You can use Metrics for Spring. Here's a github link, which explains how to integrate it with Spring MVC. The metrics-spring module integrates Dropwizard Metrics library with Spring, and provides XML and Java configuration.
Maven
当前版本为3.1.2,与Metrics 3.1.2兼容
Current version is 3.1.2, which is compatible with 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, 根据您的个人喜好.
As of version 3, metrics-spring may be configured using XML or Java, depending on your personal preference.
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 Config:
Java Config:
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);
}
}
有关度量标准春季
这篇关于如何使用Dropwizard指标和spring-mvc实施统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!