Skip to content

Commit 8bf66dc

Browse files
authored
Make _source.enabled configurable for ElasticMeterRegistry (#2363)
But also warn about the costs associated as a mitigation to this mistakenly being enabled in a production environment. Closes gh-1629
1 parent a962c98 commit 8bf66dc

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

implementations/micrometer-registry-elastic/src/main/java/io/micrometer/elastic/ElasticConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ default String apiKeyCredentials() {
181181
return getSecret(this, "apiKeyCredentials").orElse(null);
182182
}
183183

184+
/**
185+
* Enable {@literal _source} in the index template.
186+
* Default is: {@code false}
187+
*
188+
* @return whether {@literal _source} will be enabled in the index template
189+
* @since 2.0.0
190+
*/
191+
default boolean enableSource() {
192+
return getBoolean(this, "enableSource").orElse(false);
193+
}
194+
184195
@Override
185196
default Validated<?> validate() {
186197
return checkAll(this,

implementations/micrometer-registry-elastic/src/main/java/io/micrometer/elastic/ElasticMeterRegistry.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.concurrent.ThreadFactory;
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.function.Consumer;
41-
import java.util.function.Function;
4241
import java.util.regex.Matcher;
4342
import java.util.regex.Pattern;
4443

@@ -100,14 +99,6 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
10099
" \"index\": false\n" +
101100
" }\n" +
102101
"}";
103-
private static final Function<String, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix) -> "{\n" +
104-
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
105-
" \"mappings\": {\n" +
106-
" \"_source\": {\n" +
107-
" \"enabled\": false\n" +
108-
" },\n" + TEMPLATE_PROPERTIES +
109-
" }\n" +
110-
"}";
111102

112103
private static final Pattern MAJOR_VERSION_PATTERN = Pattern.compile("\"number\" *: *\"([\\d]+)");
113104

@@ -192,7 +183,15 @@ private void createIndexTemplateIfNeeded() {
192183
}
193184

194185
private String getTemplateBody() {
195-
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator());
186+
String indexPrefix = config.index() + config.indexDateSeparator();
187+
return "{\n" +
188+
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
189+
" \"mappings\": {\n" +
190+
" \"_source\": {\n" +
191+
" \"enabled\": " + config.enableSource() + "\n" +
192+
" },\n" + TEMPLATE_PROPERTIES +
193+
" }\n" +
194+
"}";
196195
}
197196

198197
private HttpSender.Request.Builder connect(HttpSender.Method method, String uri) {
@@ -212,6 +211,10 @@ private HttpSender.Request.Builder authentication(HttpSender.Request.Builder req
212211
protected void publish() {
213212
createIndexTemplateIfNeeded();
214213

214+
if (config.enableSource()) {
215+
logger.warn("'_source' field is enabled. Disable '_source' field to save space and reduce I/O.");
216+
}
217+
215218
String uri = config.host() + "/" + indexName() + "/_bulk";
216219
for (List<Meter> batch : MeterPartition.partition(this, config.batchSize())) {
217220
try {

0 commit comments

Comments
 (0)