Skip to content

Commit

Permalink
Dropwizard metrics demo..
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvydas Jonusonis committed Mar 27, 2015
1 parent 1d317d2 commit efc6b93
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 0 deletions.
4 changes: 4 additions & 0 deletions initial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.ipr
*.iws
*.iml
target/
15 changes: 15 additions & 0 deletions initial/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
def metricsVersion = '3.1.1'

buildscript {
repositories {
mavenCentral()
Expand All @@ -23,10 +25,23 @@ repositories {

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.springframework.hateoas:spring-hateoas")
compile("org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE")
compile("com.jayway.jsonpath:json-path:0.9.1")

compile("io.dropwizard.metrics:metrics-core:${metricsVersion}")
compile("io.dropwizard.metrics:metrics-servlets:${metricsVersion}")
compile("io.dropwizard.metrics:metrics-annotation:${metricsVersion}")
compile("com.ryantenney.metrics:metrics-spring:3.0.3")
compile "io.dropwizard.metrics:metrics-httpclient:${metricsVersion}"
compile "io.dropwizard.metrics:metrics-jvm:${metricsVersion}"
compile "org.elasticsearch:metrics-elasticsearch-reporter:2.0"

compile 'org.apache.httpcomponents:httpclient:4.2.5'
compile 'org.apache.httpcomponents:httpclient-cache:4.2.5'

testCompile("org.springframework.boot:spring-boot-starter-test")
}

Expand Down
79 changes: 79 additions & 0 deletions initial/src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package hello;

import com.codahale.metrics.*;
import com.codahale.metrics.httpclient.HttpClientMetricNameStrategies;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import org.apache.http.client.HttpClient;
import org.elasticsearch.metrics.ElasticsearchReporter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.management.MBeanServerFactory;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* Created by Arvydas on 3/16/15.
*/
@SpringBootApplication
@Configuration
@EnableMetrics
@ComponentScan
@EnableAutoConfiguration
public class Application extends MetricsConfigurerAdapter {

static final MetricRegistry metrics = new MetricRegistry();

public static void main(String[] args) {
SpringApplication.run(Application.class, args);

}

@Override
public void configureReporters(MetricRegistry metricRegistry) {

// metricRegistry.registerAll(new GarbageCollectorMetricSet());
// metricRegistry.registerAll(new MemoryUsageGaugeSet());
// metricRegistry.registerAll(new ThreadStatesGaugeSet());

ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(10, TimeUnit.SECONDS);

JmxReporter jmxReporter = JmxReporter.forRegistry(metrics).inDomain("net.gogii").build();

jmxReporter.start();

ElasticsearchReporter esReporter = null;
try {
esReporter = ElasticsearchReporter.forRegistry(metricRegistry)
.hosts("9cn22.lax1.gogii.net:9200")
// .percolationFilter(MetricFilter.ALL)
.index("metricsdemo")
.timeout(10000)
.build();
esReporter.start(10, TimeUnit.SECONDS);
} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public MetricRegistry getMetricRegistry() {
return metrics;
}


}
22 changes: 22 additions & 0 deletions initial/src/main/java/hello/Greeting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hello;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.hateoas.ResourceSupport;

/**
* Created by Arvydas on 3/16/15.
*/
public class Greeting extends ResourceSupport {

private final String content;

@JsonCreator
public Greeting(@JsonProperty("content") String content) {
this.content = content;
}

public String getContent() {
return content;
}
}
37 changes: 37 additions & 0 deletions initial/src/main/java/hello/GreetingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hello;

import com.codahale.metrics.annotation.Gauge;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import com.ryantenney.metrics.annotation.Counted;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Created by Arvydas on 3/16/15.
*/
@Controller
public class GreetingController {

public static final String TEMPLATE = "Hello, %s";

// @Metered
// @Gauge
@Timed
// @Counted(name = "greetingCount")

@RequestMapping("/greeting")
public HttpEntity<Greeting> greeting(
@RequestParam(value="name", required = false, defaultValue = "World") String name
){
Greeting greeting = new Greeting(String.format(TEMPLATE, name));
greeting.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(GreetingController.class).greeting(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}

}
20 changes: 20 additions & 0 deletions initial/src/main/java/hello/MyObjectNameFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hello;

import com.codahale.metrics.ObjectNameFactory;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

/**
* Created by Arvydas on 3/24/15.
*/
public class MyObjectNameFactory implements ObjectNameFactory {
@Override
public ObjectName createName(String type, String domain, String name) {
try {
return new ObjectName(domain + ":" + type + ":" + name);
} catch (MalformedObjectNameException e) {
}
return null;
}
}
16 changes: 16 additions & 0 deletions initial/web/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%--
Created by IntelliJ IDEA.
User: Arvydas
Date: 3/16/15
Time: 3:48 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>

</body>
</html>

0 comments on commit efc6b93

Please sign in to comment.