Skip to content

Commit

Permalink
Make _source.enabled configurable for ElasticMeterRegistry (#2363)
Browse files Browse the repository at this point in the history
But also warn about the costs associated as a mitigation to this mistakenly being enabled in a production environment.

Closes gh-1629
  • Loading branch information
izeye authored Mar 28, 2022
1 parent a962c98 commit 8bf66dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 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 @@ -38,7 +38,6 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
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,14 +99,6 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
" \"index\": false\n" +
" }\n" +
"}";
private static final Function<String, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix) -> "{\n" +
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
" \"mappings\": {\n" +
" \"_source\": {\n" +
" \"enabled\": false\n" +
" },\n" + TEMPLATE_PROPERTIES +
" }\n" +
"}";

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

Expand Down Expand Up @@ -192,7 +183,15 @@ private void createIndexTemplateIfNeeded() {
}

private String getTemplateBody() {
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index() + config.indexDateSeparator());
String indexPrefix = config.index() + config.indexDateSeparator();
return "{\n" +
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
" \"mappings\": {\n" +
" \"_source\": {\n" +
" \"enabled\": " + config.enableSource() + "\n" +
" },\n" + TEMPLATE_PROPERTIES +
" }\n" +
"}";
}

private HttpSender.Request.Builder connect(HttpSender.Method method, String uri) {
Expand All @@ -212,6 +211,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 8bf66dc

Please sign in to comment.