Skip to content

Commit 7309efc

Browse files
committed
Make _source.enabled configurable for ElasticMeterRegistry
Closes gh-1629
1 parent 3eb5072 commit 7309efc

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
import java.util.Optional;
3838
import java.util.concurrent.ThreadFactory;
3939
import java.util.concurrent.TimeUnit;
40+
import java.util.function.BiFunction;
4041
import java.util.function.Consumer;
41-
import java.util.function.Function;
4242
import java.util.regex.Matcher;
4343
import java.util.regex.Pattern;
4444

@@ -100,11 +100,11 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
100100
" \"index\": false\n" +
101101
" }\n" +
102102
"}";
103-
private static final Function<String, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix) -> "{\n" +
103+
private static final BiFunction<String, Boolean, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix, enableSource) -> "{\n" +
104104
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
105105
" \"mappings\": {\n" +
106106
" \"_source\": {\n" +
107-
" \"enabled\": false\n" +
107+
" \"enabled\": " + enableSource + "\n" +
108108
" },\n" + TEMPLATE_PROPERTIES +
109109
" }\n" +
110110
"}";
@@ -192,7 +192,7 @@ private void createIndexTemplateIfNeeded() {
192192
}
193193

194194
private String getTemplateBody() {
195-
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator());
195+
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator(), config.enableSource());
196196
}
197197

198198
private HttpSender.Request.Builder connect(HttpSender.Method method, String uri) {
@@ -212,6 +212,10 @@ private HttpSender.Request.Builder authentication(HttpSender.Request.Builder req
212212
protected void publish() {
213213
createIndexTemplateIfNeeded();
214214

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

0 commit comments

Comments
 (0)