tuwiendsg / MELA

Monitoring and analyzing elasticity of cloud services
http://tuwiendsg.github.io/MELA/
2 stars 4 forks source link

Integrate some Metrics-suite for application insight #16

Open olmoser opened 10 years ago

olmoser commented 10 years ago

Currently the performance/statistics of a method/API operation invocation are measured using simple

long start = System.currentTimeMillis();
doOperation();
long stop = System.currentTimeMillis();
long duration = stop - start;
...

we should streamline this kind of application insight and integrate a metrics suite (don't mix that up with the MELA measured metrics!) such as Codahale Metrics or Netflix Servo

for Codahale integration, those are the required maven dependencies:

<!-- application insight -->
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.0.0-BETA3</version>
</dependency>
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-jvm</artifactId>
    <version>3.0.0-BETA3</version>
</dependency>
<dependency>
    <groupId>com.ryantenney.metrics</groupId>
    <artifactId>metrics-spring</artifactId>
    <version>3.0.0-RC3</version>
</dependency>
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-ganglia</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-servlets</artifactId>
    <version>3.0.0-RC1</version>
</dependency>

Now we can annotate our methods with com.codahale.metrics.annotation.ExceptionMetered, com.codahale.metrics.annotation.Gauge, com.codahale.metrics.annotation.Metered and com.codahale.metrics.annotation.Timed. Additionally, we need to add the following to web.xml:

 <servlet>
        <servlet-name>Metrics</servlet-name>
        <servlet-class>com.codahale.metrics.servlets.MetricsServlet</servlet-class>
        <load-on-startup>-1</load-on-startup>
    </servlet>

for this to work, we also need a MetricsRegistry initializer which sets the correct REGISTRY_NAME attribute:

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContextEvent;

public class MetricsRegistryInitializingContextLoaderListener extends ContextLoaderListener {

    public static final String REGISTRY_NAME = "com.codahale.metrics.servlets.MetricsServlet.registry";

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
        WebApplicationContext context = getCurrentWebApplicationContext();
        Object registry = context.getBean("metrics");
        event.getServletContext().setAttribute(REGISTRY_NAME, registry);
    }

and add one last thing to web.xml

<listener>
        <listener-class>at.a1telekom.asmp.appshoster.metrics.MetricsRegistryInitializingContextLoaderListener</listener-class>
    </listener>

that should be it.