2015-12-22 19 views
5

Mam około 20 interfejsów API i chcę zaimplementować statystyki, takie jak czas wykonania, liczby odpowiedzi dla każdego interfejsu API. Po przeprowadzeniu badań dowiedziałem się, że metryki dropwizard to najlepsze podejście do wdrażania takich funkcji. Korzystam ze środowiska Spring MVC (nie-rozruchowego). Czy ktokolwiek może zaproponować mi, jak zintegrować dane z platformą Spring MVC?Jak zaimplementować statystykę za pomocą danych dropwizard i spring-mvc

Jeśli to możliwe, proszę podać dowolny kod jako odniesienie.

+0

Link który podałeś jest cały kod, który chcesz zintegrować go w aplikacji Wiosny. Czy próbowałeś? – Lucky

+0

Tak, próbowałem. ale nie mogę się dowiedzieć, jak zadzwonić, aby uzyskać statystyki interfejsów API. – kumar

Odpowiedz

4

Można użyć Metrics for Spring. Oto github link, który wyjaśnia, w jaki sposób zintegrować go ze Spring MVC. Moduł sprężyn pomiarowych integruje Dropwizard Metrics library ze sprężyną i zapewnia konfigurację XML i Java.

Maven

Aktualna wersja to 3.1.2, który jest kompatybilny z Metrics 3.1.2

<dependency> 
    <groupId>com.ryantenney.metrics</groupId> 
    <artifactId>metrics-spring</artifactId> 
    <version>3.1.2</version> 
</dependency> 

Podstawowe Wykorzystanie

Począwszy od wersji 3, metryka-wiosna może być konfigurowana ured przy użyciu XML lub Java, w zależności od osobistych preferencji.

konfiguracji 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:

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

} 

Czytaj więcej na Metrics Spring

+3

Dziękuję. Zaimplementowałem to samo, ale nie mogę się dowiedzieć, jak wywołać statystyki statystyk API. – kumar

+0

@kumar, gdzie można dowiedzieć się, jak zadzwonić, aby uzyskać dane? –

1

jak to zostało już sugerowane Metrics Wiosna oferuje kilka ciekawych integrację z wiosny. Jeśli chcesz uzyskać dostęp do tych danych z interfejsu API JSON, nadal musisz dodać serwlet, jak udokumentowano pod numerem http://metrics.dropwizard.io/3.1.0/manual/servlets/.

W celu wykorzystania tych serwletów trzeba dodać zależność:

<dependency> 
    <groupId>io.dropwizard.metrics</groupId> 
    <artifactId>metrics-servlets</artifactId> 
    <version>${metrics.version}</version> 
</dependency> 

Następnie dodać serwletu w web.xml:

<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> 

Można również użyć JavaConfig go skonfigurować.

Rejestracja serwletu:

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/*"); 
    } 
} 

i podać atrybuty potrzebne serwletu:

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

Mam kilka dodatków do powyższej odpowiedzi.

Musisz zarejestrować MetricsConfiguration jako RootConfigClasses wewnątrz WebInitializer, w przeciwnym razie nie zostanie załadowany.

Znalazłem w mojej wersji Spring (4.2.5) niezgodność wersji AOP ze sprężyną metryk, która powoduje wyjątek ClassNotFoundException. Po prostu wykluczyć sprężynę jako zależność wskaźników - sprężyna w twojej pom.

Wreszcie, można łatwo zaimplementować własną Metrics Controller tak:

@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."); 
    } 
} 
} 
Powiązane problemy