Skip to content

Make _source.enabled configurable for ElasticMeterRegistry #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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