From efc6b933540697cac3474c70597adc87f8d8596c Mon Sep 17 00:00:00 2001 From: Arvydas Jonusonis Date: Fri, 27 Mar 2015 11:23:34 -0700 Subject: [PATCH] Dropwizard metrics demo.. --- initial/.gitignore | 4 + initial/build.gradle | 15 ++++ initial/src/main/java/hello/Application.java | 79 +++++++++++++++++++ initial/src/main/java/hello/Greeting.java | 22 ++++++ .../main/java/hello/GreetingController.java | 37 +++++++++ .../main/java/hello/MyObjectNameFactory.java | 20 +++++ initial/web/index.jsp | 16 ++++ 7 files changed, 193 insertions(+) create mode 100644 initial/.gitignore create mode 100644 initial/src/main/java/hello/Application.java create mode 100644 initial/src/main/java/hello/Greeting.java create mode 100644 initial/src/main/java/hello/GreetingController.java create mode 100644 initial/src/main/java/hello/MyObjectNameFactory.java create mode 100644 initial/web/index.jsp diff --git a/initial/.gitignore b/initial/.gitignore new file mode 100644 index 0000000..8c50aca --- /dev/null +++ b/initial/.gitignore @@ -0,0 +1,4 @@ +*.ipr +*.iws +*.iml +target/ diff --git a/initial/build.gradle b/initial/build.gradle index 3182963..8fc70ab 100644 --- a/initial/build.gradle +++ b/initial/build.gradle @@ -1,3 +1,5 @@ +def metricsVersion = '3.1.1' + buildscript { repositories { mavenCentral() @@ -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") } diff --git a/initial/src/main/java/hello/Application.java b/initial/src/main/java/hello/Application.java new file mode 100644 index 0000000..484460e --- /dev/null +++ b/initial/src/main/java/hello/Application.java @@ -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; + } + + +} diff --git a/initial/src/main/java/hello/Greeting.java b/initial/src/main/java/hello/Greeting.java new file mode 100644 index 0000000..ca78002 --- /dev/null +++ b/initial/src/main/java/hello/Greeting.java @@ -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; + } +} diff --git a/initial/src/main/java/hello/GreetingController.java b/initial/src/main/java/hello/GreetingController.java new file mode 100644 index 0000000..e6f66a4 --- /dev/null +++ b/initial/src/main/java/hello/GreetingController.java @@ -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( + @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, HttpStatus.OK); + } + +} diff --git a/initial/src/main/java/hello/MyObjectNameFactory.java b/initial/src/main/java/hello/MyObjectNameFactory.java new file mode 100644 index 0000000..01cbe8d --- /dev/null +++ b/initial/src/main/java/hello/MyObjectNameFactory.java @@ -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; + } +} diff --git a/initial/web/index.jsp b/initial/web/index.jsp new file mode 100644 index 0000000..c4cde05 --- /dev/null +++ b/initial/web/index.jsp @@ -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" %> + + + + + + + +