Skip to content

Commit

Permalink
Make _source.enabled configurable for ElasticMeterRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Mar 25, 2022
1 parent 3eb5072 commit 7309efc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ default String apiKeyCredentials() {
return getSecret(this, "apiKeyCredentials").orElse(null);
}

/**
* Enable {@literal _source} in the index template.
* Default is: {@code false}
*
* @return whether {@literal _source} will be enabled in the index template
* @since 2.0.0
*/
default boolean enableSource() {
return getBoolean(this, "enableSource").orElse(false);
}

@Override
default Validated<?> validate() {
return checkAll(this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -100,11 +100,11 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
" \"index\": false\n" +
" }\n" +
"}";
private static final Function<String, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix) -> "{\n" +
private static final BiFunction<String, Boolean, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix, enableSource) -> "{\n" +
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
" \"mappings\": {\n" +
" \"_source\": {\n" +
" \"enabled\": false\n" +
" \"enabled\": " + enableSource + "\n" +
" },\n" + TEMPLATE_PROPERTIES +
" }\n" +
"}";
Expand Down Expand Up @@ -192,7 +192,7 @@ private void createIndexTemplateIfNeeded() {
}

private String getTemplateBody() {
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator());
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator(), config.enableSource());
}

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

if (config.enableSource()) {
logger.warn("'_source' field is enabled. Disable '_source' field to save space and reduce I/O.");
}

String uri = config.host() + "/" + indexName() + "/_bulk";
for (List<Meter> batch : MeterPartition.partition(this, config.batchSize())) {
try {
Expand Down

0 comments on commit 7309efc

Please sign in to comment.