Skip to content

Commit d00dad6

Browse files
committed
Micrometer support for all Task app starters
- Extends the SCT micrometer support. - Add post-processor to disable the influx/prometheus by default. - Add task apps core global dependencies on influx, prometheus. Resolves spring-attic#14 Depends on #spring-cloud/spring-cloud-task/issues/608
1 parent a10c419 commit d00dad6

File tree

8 files changed

+317
-8
lines changed

8 files changed

+317
-8
lines changed

core-dependencies/pom.xml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
<parent>
1212
<artifactId>spring-cloud-dependencies-parent</artifactId>
1313
<groupId>org.springframework.cloud</groupId>
14-
<version>2.1.3.RELEASE</version>
14+
<version>2.2.0.BUILD-SNAPSHOT</version>
1515
<relativePath />
1616
</parent>
1717

1818
<properties>
1919
<java.version>1.8</java.version>
20-
<spring-cloud-task.version>2.1.1.RELEASE</spring-cloud-task.version>
21-
<spring.cloud.dependencies.version>Greenwich.SR1</spring.cloud.dependencies.version>
22-
<spring.cloud.deployer.spi>2.0.0.RELEASE</spring.cloud.deployer.spi>
23-
<spring.cloud.deployer.resource.maven>2.0.0.RELEASE</spring.cloud.deployer.resource.maven>
20+
<spring-cloud-task.version>2.2.0.BUILD-SNAPSHOT</spring-cloud-task.version>
21+
<spring.cloud.dependencies.version>Greenwich.SR2</spring.cloud.dependencies.version>
22+
<spring.cloud.deployer.spi>2.0.2.RELEASE</spring.cloud.deployer.spi>
23+
<spring.cloud.deployer.resource.maven>2.0.2.RELEASE</spring.cloud.deployer.resource.maven>
2424
<hibernate.validation.version>6.0.14.Final</hibernate.validation.version>
2525
<log4j.version>1.2.17</log4j.version>
26+
<java-cfenv-boot.version>1.0.1.RELEASE</java-cfenv-boot.version>
2627
</properties>
2728

2829
<dependencyManagement>
@@ -41,6 +42,11 @@
4142
<type>pom</type>
4243
<scope>import</scope>
4344
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.cloud.task.app</groupId>
47+
<artifactId>task-app-starters-micrometer-common</artifactId>
48+
<version>2.1.1.BUILD-SNAPSHOT</version>
49+
</dependency>
4450
<dependency>
4551
<groupId>org.springframework.cloud</groupId>
4652
<artifactId>spring-cloud-deployer-spi</artifactId>
@@ -61,6 +67,11 @@
6167
<artifactId>log4j</artifactId>
6268
<version>${log4j.version}</version>
6369
</dependency>
70+
<dependency>
71+
<groupId>io.pivotal.cfenv</groupId>
72+
<artifactId>java-cfenv-test-support</artifactId>
73+
<version>${java-cfenv-boot.version}</version>
74+
</dependency>
6475
</dependencies>
6576
</dependencyManagement>
6677

pom.xml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<groupId>org.springframework.cloud.task.app</groupId>
56
<artifactId>task-app-starters-build</artifactId>
@@ -9,8 +10,8 @@
910
<parent>
1011
<groupId>org.springframework.cloud</groupId>
1112
<artifactId>spring-cloud-build</artifactId>
12-
<version>2.1.3.RELEASE</version>
13-
<relativePath />
13+
<version>2.2.0.M2</version>
14+
<relativePath/>
1415
</parent>
1516
<scm>
1617
<connection>scm:git:git://github.com/spring-cloud/spring-cloud-task-app-starters.git</connection>
@@ -29,6 +30,7 @@
2930

3031
<modules>
3132
<module>core-dependencies</module>
33+
<module>task-app-starters-common</module>
3234
</modules>
3335

3436
<dependencyManagement>
@@ -162,6 +164,22 @@
162164
</bom>
163165
</additionalBoms>
164166
<additionalGlobalDependencies>
167+
<dependency>
168+
<groupId>org.springframework.cloud.task.app</groupId>
169+
<artifactId>task-app-starters-micrometer-common</artifactId>
170+
</dependency>
171+
<dependency>
172+
<groupId>org.springframework.boot</groupId>
173+
<artifactId>spring-boot-starter-actuator</artifactId>
174+
</dependency>
175+
<dependency>
176+
<groupId>io.micrometer</groupId>
177+
<artifactId>micrometer-registry-influx</artifactId>
178+
</dependency>
179+
<dependency>
180+
<groupId>io.micrometer</groupId>
181+
<artifactId>micrometer-registry-prometheus</artifactId>
182+
</dependency>
165183
<dependency>
166184
<groupId>org.springframework.cloud</groupId>
167185
<artifactId>spring-cloud-starter-config</artifactId>

task-app-starters-common/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>task-app-starters-build</artifactId>
7+
<groupId>org.springframework.cloud.task.app</groupId>
8+
<version>2.1.1.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>task-app-starters-common</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>task-app-starters-micrometer-common</module>
16+
</modules>
17+
18+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>task-app-starters-common</artifactId>
7+
<groupId>org.springframework.cloud.task.app</groupId>
8+
<version>2.1.1.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>task-app-starters-micrometer-common</artifactId>
13+
<name>task-app-starters-micrometer-common</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-actuator</artifactId>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>io.micrometer</groupId>
23+
<artifactId>micrometer-registry-influx</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.pivotal.cfenv</groupId>
28+
<artifactId>java-cfenv-test-support</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
35+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.cloud.task.app.micrometer.common;
17+
18+
import java.util.Properties;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.env.EnvironmentPostProcessor;
22+
import org.springframework.core.env.ConfigurableEnvironment;
23+
import org.springframework.core.env.PropertiesPropertySource;
24+
25+
/**
26+
* Disables all Micrometer Repositories added as App Starters dependencies by default.
27+
* That means disabling Datadog, Influx and Prometheus.
28+
*
29+
* @author Christian Tzolov
30+
*/
31+
public class SpringCloudTaskMicrometerEnvironmentPostProcessor implements EnvironmentPostProcessor {
32+
33+
protected static final String PROPERTY_SOURCE_KEY_NAME = SpringCloudTaskMicrometerEnvironmentPostProcessor.class.getName();
34+
35+
private final static String METRICS_PROPERTY_NAME_TEMPLATE = "management.metrics.export.%s.enabled";
36+
37+
private final static String[] METRICS_REPOSITORY_NAMES = new String[] { "datadog", "influx", "prometheus" };
38+
39+
@Override
40+
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
41+
Properties properties = new Properties();
42+
43+
for (String metricsRepositoryName : METRICS_REPOSITORY_NAMES) {
44+
String metricsEnabledPropertyKey = String.format(METRICS_PROPERTY_NAME_TEMPLATE, metricsRepositoryName);
45+
46+
// Back off if the property is already set externally
47+
if (!environment.containsProperty(metricsEnabledPropertyKey)) {
48+
properties.setProperty(metricsEnabledPropertyKey, "false");
49+
}
50+
}
51+
52+
// Ensure idempotency as this post-processor is called multiple times (once for each application context)
53+
// but the properties should be set only once.
54+
if (!properties.isEmpty()) {
55+
environment.getPropertySources().addLast(new PropertiesPropertySource(PROPERTY_SOURCE_KEY_NAME, properties));
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.springframework.boot.env.EnvironmentPostProcessor=\
2+
org.springframework.cloud.task.app.micrometer.common.SpringCloudTaskMicrometerEnvironmentPostProcessor
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2018-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.cloud.task.app.micrometer.common;
17+
18+
import io.micrometer.core.instrument.Clock;
19+
import io.micrometer.core.instrument.Meter;
20+
import io.micrometer.core.instrument.simple.SimpleConfig;
21+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
22+
import org.junit.Before;
23+
import org.junit.runner.RunWith;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.SpringApplication;
27+
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties;
28+
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimplePropertiesConfigAdapter;
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32+
import org.springframework.boot.test.context.SpringBootTest;
33+
import org.springframework.context.ConfigurableApplicationContext;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36+
37+
import static org.junit.Assert.assertNotNull;
38+
39+
/**
40+
* @author Christian Tzolov
41+
* @author Soby Chacko
42+
*/
43+
@RunWith(SpringJUnit4ClassRunner.class)
44+
@SpringBootTest(classes = AbstractMicrometerTagTest.AutoConfigurationApplication.class)
45+
public class AbstractMicrometerTagTest {
46+
47+
@Autowired
48+
protected SimpleMeterRegistry simpleMeterRegistry;
49+
50+
@Autowired
51+
protected ConfigurableApplicationContext context;
52+
53+
protected Meter meter;
54+
55+
@Before
56+
public void before() {
57+
assertNotNull(simpleMeterRegistry);
58+
meter = simpleMeterRegistry.find("jvm.memory.committed").meter();
59+
assertNotNull("The jvm.memory.committed meter mast be present in SpringBoot apps!", meter);
60+
}
61+
62+
@SpringBootApplication
63+
@EnableConfigurationProperties(SimpleProperties.class)
64+
public static class AutoConfigurationApplication {
65+
66+
public static void main(String[] args) {
67+
SpringApplication.run(AutoConfigurationApplication.class, args);
68+
}
69+
70+
@Bean
71+
public SimpleMeterRegistry simpleMeterRegistry(SimpleConfig config, Clock clock) {
72+
return new SimpleMeterRegistry(config, clock);
73+
}
74+
75+
@Bean
76+
@ConditionalOnMissingBean
77+
public SimpleConfig simpleConfig(SimpleProperties simpleProperties) {
78+
return new SimplePropertiesConfigAdapter(simpleProperties);
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.cloud.task.app.micrometer.common;
17+
18+
import org.hamcrest.core.Is;
19+
import org.junit.Test;
20+
import org.junit.experimental.runners.Enclosed;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.core.env.PropertySource;
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertNull;
28+
import static org.junit.Assert.assertThat;
29+
30+
/**
31+
* @author Christian Tzolov
32+
*/
33+
@RunWith(Enclosed.class)
34+
public class SpringCloudTaskMicrometerEnvironmentPostProcessorTest {
35+
36+
public static class TestDefaultMetricsEnabledProperties extends AbstractMicrometerTagTest {
37+
38+
@Test
39+
public void testDefaultProperties() {
40+
assertNotNull(context);
41+
42+
PropertySource propertySource = context.getEnvironment().getPropertySources()
43+
.get(SpringCloudTaskMicrometerEnvironmentPostProcessor.PROPERTY_SOURCE_KEY_NAME);
44+
45+
assertNotNull("Property source "
46+
+ SpringCloudTaskMicrometerEnvironmentPostProcessor.PROPERTY_SOURCE_KEY_NAME + " is null",
47+
propertySource);
48+
49+
assertThat(propertySource.getProperty("management.metrics.export.influx.enabled"), Is.is("false"));
50+
assertThat(propertySource.getProperty("management.metrics.export.prometheus.enabled"), Is.is("false"));
51+
assertThat(propertySource.getProperty("management.metrics.export.datadog.enabled"), Is.is("false"));
52+
53+
assertThat(context.getEnvironment().getProperty("management.metrics.export.influx.enabled"), Is.is("false"));
54+
assertThat(context.getEnvironment().getProperty("management.metrics.export.prometheus.enabled"), Is.is("false"));
55+
assertThat(context.getEnvironment().getProperty("management.metrics.export.datadog.enabled"), Is.is("false"));
56+
}
57+
}
58+
59+
@TestPropertySource(properties = {
60+
"management.metrics.export.simple.enabled=true",
61+
"management.metrics.export.influx.enabled=true",
62+
"management.metrics.export.prometheus.enabled=true",
63+
"management.metrics.export.datadog.enabled=true",
64+
"management.endpoints.web.exposure.include=info,health" })
65+
public static class TestOverrideMetricsEnabledProperties extends AbstractMicrometerTagTest {
66+
67+
@Test
68+
public void testOverrideProperties() {
69+
assertNotNull(context);
70+
71+
PropertySource propertySource = context.getEnvironment().getPropertySources()
72+
.get(SpringCloudTaskMicrometerEnvironmentPostProcessor.PROPERTY_SOURCE_KEY_NAME);
73+
74+
assertNull("Property source "
75+
+ SpringCloudTaskMicrometerEnvironmentPostProcessor.PROPERTY_SOURCE_KEY_NAME + " is not null",
76+
propertySource);
77+
78+
assertThat(context.getEnvironment().getProperty("management.metrics.export.influx.enabled"), Is.is("true"));
79+
assertThat(context.getEnvironment().getProperty("management.metrics.export.prometheus.enabled"), Is.is("true"));
80+
assertThat(context.getEnvironment().getProperty("management.metrics.export.datadog.enabled"), Is.is("true"));
81+
82+
assertThat(context.getEnvironment().getProperty("management.endpoints.web.exposure.include"), Is.is("info,health"));
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)