Skip to content

Commit

Permalink
Move to Micrometer
Browse files Browse the repository at this point in the history
These changes move metrics gathering and export to Micrometer, an application metrics facade that supports numerous monitoring systems, such as influxdb.

[#3]
  • Loading branch information
s-bortolussi committed Nov 13, 2017
1 parent 0a136c5 commit 62b7df8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 128 deletions.
31 changes: 11 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.example</groupId>
<artifactId>metrics-influxdb-sample</artifactId>
<version>0.0.4.RELEASE</version>
<version>0.0.5.SNAPSHOT</version>
<packaging>jar</packaging>

<name>metrics-influxdb-sample</name>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M3</version>
<version>2.0.0.M5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -30,19 +30,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.7</version>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
Expand All @@ -51,16 +47,11 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.orange.spring.cloud.connectors</groupId>
<artifactId>spring-cloud-influxdb-spring-service-connector</artifactId>
<version>${influxdb.connectors.version}</version>
</dependency>
<!-- If you intend to deploy the app on Cloud Foundry, add the following -->
<dependency>
<groupId>com.orange.spring.cloud.connectors</groupId>
<artifactId>spring-cloud-influxdb-connectors-cloudfoundry</artifactId>
<version>${influxdb.connectors.version}</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
</dependency>

</dependencies>
Expand Down
74 changes: 0 additions & 74 deletions src/main/java/com/example/InfluxDBMetricWriter.java

This file was deleted.

46 changes: 12 additions & 34 deletions src/main/java/com/example/MetricsInfluxdbSampleApplication.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
package com.example;

import org.influxdb.InfluxDB;
import org.springframework.beans.factory.annotation.Autowired;
import io.micrometer.core.annotation.Timed;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.MetricsEndpointMetricReader;
import org.springframework.boot.actuate.metrics.jmx.JmxMetricWriter;
import org.springframework.boot.actuate.metrics.writer.GaugeWriter;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class MetricsInfluxdbSampleApplication {

@Value("${cloud.application.instance_id:app_01}")
private String appInstanceId;

@Autowired
private InfluxDB influxDB;


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

@Bean
@ExportMetricWriter
MetricWriter metricWriter(MBeanExporter exporter) {
return new JmxMetricWriter(exporter);
}

@Bean
@ExportMetricWriter
GaugeWriter influxMetricsWriter() {
InfluxDBMetricWriter.Builder builder = new InfluxDBMetricWriter.Builder(influxDB);
builder.appInstanceId(appInstanceId);
builder.batchActions(500); // number of points for batch before data is sent to Influx
return builder.build();
}

@Bean
public MetricsEndpointMetricReader metricsEndpointMetricReader(MetricsEndpoint metricsEndpoint) {
return new MetricsEndpointMetricReader(metricsEndpoint);
}
}

@RestController
class GreetingController {

@GetMapping("/greeting")
public String greeting(@RequestParam(value = "name", defaultValue = "world !") String name) {
@Timed(quantiles = {0.5, 0.85, 0.95, 0.99})
public String greeting(@RequestParam(value = "name", defaultValue = "world !") String name) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
latch.await(1, TimeUnit.SECONDS);
if (new Random().nextInt(100) > 90) {
throw new Exception("Error (random) while handling request.");
}
return String.format("hello %s", name);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
security.basic.enabled=false
management.security.enabled=false
logging.level.com.example.demo=debug
spring.metrics.influx.uri=http://localhost:8086/write
spring.metrics.influx.db=metrics
spring.metrics.influx.step=PT10S

0 comments on commit 62b7df8

Please sign in to comment.