Skip to content

Commit 99078a2

Browse files
izeyeshakuzen
authored andcommitted
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 8c53ae7 commit 99078a2

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DefaultIndexTemplateCreator implements IndexTemplateCreator {
3434
private final Logger logger = LoggerFactory.getLogger(DefaultIndexTemplateCreator.class);
3535

3636
private final String indexTemplateRequest = "{\n" + " \"index_patterns\": [\"%s*\"],\n" + " \"template\": {\n"
37-
+ " \"mappings\": {\n" + " \"_source\": {\n" + " \"enabled\": false\n" + " },\n"
37+
+ " \"mappings\": {\n" + " \"_source\": {\n" + " \"enabled\": %b\n" + " },\n"
3838
+ " \"properties\": {\n" + " \"name\": { \"type\": \"keyword\" },\n"
3939
+ " \"count\": { \"type\": \"double\", \"index\": false },\n"
4040
+ " \"value\": { \"type\": \"double\", \"index\": false },\n"
@@ -81,9 +81,10 @@ public IndexTemplateStatus fetchIndexTemplateStatus(ElasticConfig configuration)
8181
@Override
8282
public void createIndexTemplate(ElasticConfig configuration) throws Throwable {
8383
String indexPattern = configuration.index() + configuration.indexDateSeparator();
84+
boolean enableSource = configuration.enableSource();
8485
HttpSender.Request.Builder request = this.httpClient.put(configuration.host() + INDEX_TEMPLATE_PATH);
8586
configureAuthentication(configuration, request);
86-
request.withJsonContent(String.format(indexTemplateRequest, indexPattern))
87+
request.withJsonContent(String.format(indexTemplateRequest, indexPattern, enableSource))
8788
.send()
8889
.onError(response -> logger.error("Failed to create index template in Elastic: {}", response.body()));
8990
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ default String documentType() {
178178
return getString(this, "documentType").orElse("doc");
179179
}
180180

181+
/**
182+
* Enable {@literal _source} in the index template. Default is: {@code false}
183+
* @return whether {@literal _source} will be enabled in the index template used with
184+
* {@link #autoCreateIndex()}
185+
* @since 1.14.0
186+
*/
187+
default boolean enableSource() {
188+
return getBoolean(this, "enableSource").orElse(false);
189+
}
190+
181191
@Override
182192
default Validated<?> validate() {
183193
return checkAll(this, c -> StepRegistryConfig.validate(c), checkRequired("host", ElasticConfig::host),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ private void createIndexTemplateIfNeeded() {
153153
if (this.checkedForIndexTemplate || !this.config.autoCreateIndex()) {
154154
return;
155155
}
156+
if (config.enableSource()) {
157+
logger.warn("'_source' field is enabled. Disable '_source' field to save space and reduce I/O.");
158+
}
156159
attemptIndexTemplateCreation(new DefaultIndexTemplateCreator(this.httpClient));
157160
if (!this.checkedForIndexTemplate) {
158161
logger.debug("Attempt to create index template using legacy /_template/ endpoint");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LegacyIndexTemplateCreator implements IndexTemplateCreator {
3434
private final Logger logger = LoggerFactory.getLogger(LegacyIndexTemplateCreator.class);
3535

3636
private final String indexTemplateRequest = "{\n" + " \"index_patterns\": [\"%s*\"],\n" + " \"mappings\": {\n"
37-
+ " \"_source\": {\n" + " \"enabled\": false\n" + " },\n" + " \"properties\": {\n"
37+
+ " \"_source\": {\n" + " \"enabled\": %b\n" + " },\n" + " \"properties\": {\n"
3838
+ " \"name\": { \"type\": \"keyword\" },\n"
3939
+ " \"count\": { \"type\": \"double\", \"index\": false },\n"
4040
+ " \"value\": { \"type\": \"double\", \"index\": false },\n"
@@ -80,9 +80,10 @@ public IndexTemplateStatus fetchIndexTemplateStatus(ElasticConfig configuration)
8080
@Override
8181
public void createIndexTemplate(ElasticConfig configuration) throws Throwable {
8282
String indexPattern = configuration.index() + configuration.indexDateSeparator();
83+
boolean enableSource = configuration.enableSource();
8384
HttpSender.Request.Builder request = this.httpClient.put(configuration.host() + INDEX_TEMPLATE_PATH);
8485
configureAuthentication(configuration, request);
85-
request.withJsonContent(String.format(indexTemplateRequest, indexPattern))
86+
request.withJsonContent(String.format(indexTemplateRequest, indexPattern, enableSource))
8687
.send()
8788
.onError(response -> logger.error("Failed to create index template in Elastic: {}", response.body()));
8889
}

0 commit comments

Comments
 (0)