forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract metric export into a separate autoconfig class
This avoids a potential problems with ordering between Dropwizard and normal repository configuration. A Dropwizard sample has been added to verify the behaviour.
- Loading branch information
1 parent
7be13b2
commit 64af327
Showing
13 changed files
with
329 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...in/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.metrics.export.MetricExportProperties; | ||
import org.springframework.boot.actuate.metrics.export.MetricExporters; | ||
import org.springframework.boot.actuate.metrics.reader.MetricReader; | ||
import org.springframework.boot.actuate.metrics.writer.MetricWriter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
||
/** | ||
* @author Dave Syer | ||
*/ | ||
@Configuration | ||
@EnableScheduling | ||
@ConditionalOnProperty(value = "spring.metrics.export.enabled", matchIfMissing = true) | ||
public class MetricExportAutoConfiguration { | ||
|
||
@Autowired(required = false) | ||
private Map<String, MetricWriter> writers = Collections.emptyMap(); | ||
|
||
@Autowired | ||
private MetricExportProperties metrics; | ||
|
||
@Autowired(required = false) | ||
@ActuatorMetricRepository | ||
private MetricWriter actuatorMetricRepository; | ||
|
||
@Autowired(required = false) | ||
@ActuatorMetricRepository | ||
private MetricReader reader; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public SchedulingConfigurer metricWritersMetricExporter() { | ||
Map<String, MetricWriter> writers = new HashMap<String, MetricWriter>(); | ||
if (this.reader != null) { | ||
writers.putAll(this.writers); | ||
if (this.actuatorMetricRepository != null | ||
&& writers.containsValue(this.actuatorMetricRepository)) { | ||
for (String name : this.writers.keySet()) { | ||
if (writers.get(name).equals(this.actuatorMetricRepository)) { | ||
writers.remove(name); | ||
} | ||
} | ||
} | ||
MetricExporters exporters = new MetricExporters(this.reader, writers, | ||
this.metrics); | ||
return exporters; | ||
} | ||
return new SchedulingConfigurer() { | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
spring-boot-samples/spring-boot-sample-metrics-dropwizard/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<!-- Your own application should inherit from spring-boot-starter-parent --> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-samples</artifactId> | ||
<version>1.3.0.BUILD-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>spring-boot-sample-metrics-dropwizard</artifactId> | ||
<name>spring-boot-sample-metrics-dropwizard</name> | ||
<description>Spring Boot Metrics Redis Sample</description> | ||
<url>http://projects.spring.io/spring-boot/</url> | ||
<organization> | ||
<name>Pivotal Software, Inc.</name> | ||
<url>http://www.spring.io</url> | ||
</organization> | ||
<properties> | ||
<main.basedir>${basedir}/../..</main.basedir> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<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>io.dropwizard.metrics</groupId> | ||
<artifactId>metrics-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
40 changes: 40 additions & 0 deletions
40
...-sample-metrics-dropwizard/src/main/java/sample/metrics/dropwizard/HelloWorldService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2012-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sample.metrics.dropwizard; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false) | ||
public class HelloWorldService { | ||
|
||
private String name = "World"; | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getHelloMessage() { | ||
return "Hello " + this.name; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...t-sample-metrics-dropwizard/src/main/java/sample/metrics/dropwizard/SampleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2012-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sample.metrics.dropwizard; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.hibernate.validator.constraints.NotBlank; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.metrics.GaugeService; | ||
import org.springframework.context.annotation.Description; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@Controller | ||
@Description("A controller for handling requests for hello messages") | ||
public class SampleController { | ||
|
||
@Autowired | ||
private HelloWorldService helloWorldService; | ||
|
||
@Autowired | ||
private GaugeService gauges; | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
@ResponseBody | ||
public Map<String, String> hello() { | ||
this.gauges.submit("timer.test.value", Math.random() * 1000 + 1000); | ||
return Collections.singletonMap("message", | ||
this.helloWorldService.getHelloMessage()); | ||
} | ||
|
||
protected static class Message { | ||
|
||
@NotBlank(message = "Message value cannot be empty") | ||
private String value; | ||
|
||
public String getValue() { | ||
return this.value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ropwizard/src/main/java/sample/metrics/dropwizard/SampleDropwizardMetricsApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2012-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package sample.metrics.dropwizard; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SampleDropwizardMetricsApplication { | ||
|
||
public static void main(String[] args) throws Exception { | ||
SpringApplication.run(SampleDropwizardMetricsApplication.class, args); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...t-samples/spring-boot-sample-metrics-dropwizard/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
service.name: Phil |
Oops, something went wrong.