Skip to content

Commit 4c3e2d1

Browse files
committed
Auto-configure Micrometer's Elastic registry
Closes gh-14523
1 parent 95ecbc7 commit 4c3e2d1

File tree

13 files changed

+604
-0
lines changed

13 files changed

+604
-0
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@
107107
<artifactId>micrometer-registry-dynatrace</artifactId>
108108
<optional>true</optional>
109109
</dependency>
110+
<dependency>
111+
<groupId>io.micrometer</groupId>
112+
<artifactId>micrometer-registry-elastic</artifactId>
113+
<optional>true</optional>
114+
</dependency>
110115
<dependency>
111116
<groupId>io.micrometer</groupId>
112117
<artifactId>micrometer-registry-ganglia</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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+
17+
package org.springframework.boot.actuate.autoconfigure.metrics.export.elastic;
18+
19+
import io.micrometer.core.instrument.Clock;
20+
import io.micrometer.elastic.ElasticConfig;
21+
import io.micrometer.elastic.ElasticMeterRegistry;
22+
23+
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
24+
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
25+
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
27+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
28+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
33+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Configuration;
36+
37+
/**
38+
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to Elastic.
39+
*
40+
* @author Andy Wilkinson
41+
* @since 2.1.0
42+
*/
43+
@Configuration
44+
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class,
45+
SimpleMetricsExportAutoConfiguration.class })
46+
@AutoConfigureAfter(MetricsAutoConfiguration.class)
47+
@ConditionalOnBean(Clock.class)
48+
@ConditionalOnClass(ElasticMeterRegistry.class)
49+
@ConditionalOnProperty(prefix = "management.metrics.export.elastic", name = "enabled", havingValue = "true", matchIfMissing = true)
50+
@EnableConfigurationProperties(ElasticProperties.class)
51+
public class ElasticMetricsExportAutoConfiguration {
52+
53+
@Bean
54+
@ConditionalOnMissingBean
55+
public ElasticConfig elasticConfig(ElasticProperties elasticProperties) {
56+
return new ElasticPropertiesConfigAdapter(elasticProperties);
57+
}
58+
59+
@Bean
60+
@ConditionalOnMissingBean
61+
public ElasticMeterRegistry elasticMeterRegistry(ElasticConfig elasticConfig,
62+
Clock clock) {
63+
return new ElasticMeterRegistry(elasticConfig, clock);
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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+
17+
package org.springframework.boot.actuate.autoconfigure.metrics.export.elastic;
18+
19+
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
22+
/**
23+
* {@link ConfigurationProperties} for configuring Elastic metrics export.
24+
*
25+
* @author Andy Wilkinson
26+
* @since 2.1.0
27+
*/
28+
@ConfigurationProperties(prefix = "management.metrics.export.elastic")
29+
public class ElasticProperties extends StepRegistryProperties {
30+
31+
/**
32+
* Hosts to export metrics to.
33+
*/
34+
private String[] hosts = new String[] { "http://localhost:9200" };
35+
36+
/**
37+
* Index to export metrics to.
38+
*/
39+
private String index = "metrics";
40+
41+
/**
42+
* Index date format used for rolling indices. Appended to the index name, preceded by
43+
* a '-'.
44+
*/
45+
private String indexDateFormat = "yyyy-MM";
46+
47+
/**
48+
* Name of the timestamp field.
49+
*/
50+
private String timestampFieldName = "@timestamp";
51+
52+
/**
53+
* Whether to create the index automatically if it does not exist.
54+
*/
55+
private boolean autoCreateIndex = true;
56+
57+
/**
58+
* Username for basic authentication.
59+
*/
60+
private String userName = "";
61+
62+
/**
63+
* Password for basic authentication.
64+
*/
65+
private String password = "";
66+
67+
public String[] getHosts() {
68+
return this.hosts;
69+
}
70+
71+
public void setHosts(String[] hosts) {
72+
this.hosts = hosts;
73+
}
74+
75+
public String getIndex() {
76+
return this.index;
77+
}
78+
79+
public void setIndex(String index) {
80+
this.index = index;
81+
}
82+
83+
public String getIndexDateFormat() {
84+
return this.indexDateFormat;
85+
}
86+
87+
public void setIndexDateFormat(String indexDateFormat) {
88+
this.indexDateFormat = indexDateFormat;
89+
}
90+
91+
public String getTimestampFieldName() {
92+
return this.timestampFieldName;
93+
}
94+
95+
public void setTimestampFieldName(String timestampFieldName) {
96+
this.timestampFieldName = timestampFieldName;
97+
}
98+
99+
public boolean isAutoCreateIndex() {
100+
return this.autoCreateIndex;
101+
}
102+
103+
public void setAutoCreateIndex(boolean autoCreateIndex) {
104+
this.autoCreateIndex = autoCreateIndex;
105+
}
106+
107+
public String getUserName() {
108+
return this.userName;
109+
}
110+
111+
public void setUserName(String userName) {
112+
this.userName = userName;
113+
}
114+
115+
public String getPassword() {
116+
return this.password;
117+
}
118+
119+
public void setPassword(String password) {
120+
this.password = password;
121+
}
122+
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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+
17+
package org.springframework.boot.actuate.autoconfigure.metrics.export.elastic;
18+
19+
import io.micrometer.elastic.ElasticConfig;
20+
21+
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
22+
23+
/**
24+
* Adapter to convert {@link ElasticProperties} to an {@link ElasticConfig}.
25+
*
26+
* @author Andy Wilkinson
27+
*/
28+
class ElasticPropertiesConfigAdapter extends
29+
StepRegistryPropertiesConfigAdapter<ElasticProperties> implements ElasticConfig {
30+
31+
ElasticPropertiesConfigAdapter(ElasticProperties properties) {
32+
super(properties);
33+
}
34+
35+
@Override
36+
public String[] hosts() {
37+
return get(ElasticProperties::getHosts, ElasticConfig.super::hosts);
38+
}
39+
40+
@Override
41+
public String index() {
42+
return get(ElasticProperties::getIndex, ElasticConfig.super::index);
43+
}
44+
45+
@Override
46+
public String indexDateFormat() {
47+
return get(ElasticProperties::getIndexDateFormat,
48+
ElasticConfig.super::indexDateFormat);
49+
}
50+
51+
@Override
52+
public String timestampFieldName() {
53+
return get(ElasticProperties::getTimestampFieldName,
54+
ElasticConfig.super::timestampFieldName);
55+
}
56+
57+
@Override
58+
public boolean autoCreateIndex() {
59+
return get(ElasticProperties::isAutoCreateIndex,
60+
ElasticConfig.super::autoCreateIndex);
61+
}
62+
63+
@Override
64+
public String userName() {
65+
return get(ElasticProperties::getUserName, ElasticConfig.super::userName);
66+
}
67+
68+
@Override
69+
public String password() {
70+
return get(ElasticProperties::getPassword, ElasticConfig.super::password);
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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+
17+
/**
18+
* Support for exporting actuator metrics to Elastic.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.metrics.export.elastic;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsAutoCon
4646
org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration,\
4747
org.springframework.boot.actuate.autoconfigure.metrics.export.datadog.DatadogMetricsExportAutoConfiguration,\
4848
org.springframework.boot.actuate.autoconfigure.metrics.export.dynatrace.DynatraceMetricsExportAutoConfiguration,\
49+
org.springframework.boot.actuate.autoconfigure.metrics.export.elastic.ElasticMetricsExportAutoConfiguration,\
4950
org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia.GangliaMetricsExportAutoConfiguration,\
5051
org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration,\
5152
org.springframework.boot.actuate.autoconfigure.metrics.export.influx.InfluxMetricsExportAutoConfiguration,\

0 commit comments

Comments
 (0)