Skip to content

Commit

Permalink
Drop support for Elasticsearch 6 (#2361)
Browse files Browse the repository at this point in the history
Closes gh-1906
  • Loading branch information
izeye committed Nov 27, 2020
1 parent 133d359 commit def8d3d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 62 deletions.
Expand Up @@ -169,7 +169,9 @@ default String indexDateSeparator() {
*
* @return document type
* @since 1.4.0
* @deprecated This is no-op due to removal of mapping types since Elasticsearch 7.
*/
@Deprecated
default String documentType() {
return getString(this, "documentType").orElse("doc");
}
Expand Down
Expand Up @@ -24,7 +24,6 @@
import io.micrometer.core.ipc.http.HttpSender;
import io.micrometer.core.ipc.http.HttpUrlConnectionSender;
import io.micrometer.core.lang.NonNull;
import io.micrometer.core.lang.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -53,6 +52,7 @@
* @author Jon Schneider
* @author Johnny Lim
* @since 1.1.0
* @implNote This implementation requires Elasticsearch 7 or above.
*/
public class ElasticMeterRegistry extends StepMeterRegistry {
private static final ThreadFactory DEFAULT_THREAD_FACTORY = new NamedThreadFactory("elastic-metrics-publisher");
Expand Down Expand Up @@ -91,7 +91,6 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
" \"type\": \"double\"\n" +
" }\n" +
"}";
private static final Function<String, String> TEMPLATE_BODY_BEFORE_VERSION_7 = (indexPrefix) -> "{\"template\":\"" + indexPrefix + "*\",\"mappings\":{\"_default_\":{\"_all\":{\"enabled\":false}," + TEMPLATE_PROPERTIES + "}}}";
private static final Function<String, String> TEMPLATE_BODY_AFTER_VERSION_7 = (indexPrefix) -> "{\n" +
" \"index_patterns\": [\"" + indexPrefix + "*\"],\n" +
" \"mappings\": {\n" +
Expand All @@ -101,8 +100,6 @@ public class ElasticMeterRegistry extends StepMeterRegistry {
" }\n" +
"}";

private static final String TYPE_PATH_AFTER_VERSION_7 = "";

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

private static final String ERROR_RESPONSE_BODY_SIGNATURE = "\"errors\":true";
Expand All @@ -117,9 +114,6 @@ public class ElasticMeterRegistry extends StepMeterRegistry {

private final String indexLine;

@Nullable
private volatile Integer majorVersion;

private volatile boolean checkedForIndexTemplate;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -190,17 +184,15 @@ private void createIndexTemplateIfNeeded() {
checkedForIndexTemplate = true;
}

@SuppressWarnings("ConstantConditions")
private String getTemplateBody() {
return majorVersion == null || majorVersion < 7 ? TEMPLATE_BODY_BEFORE_VERSION_7.apply(config.index()) : TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index());
return TEMPLATE_BODY_AFTER_VERSION_7.apply(config.index());
}

@Override
protected void publish() {
determineMajorVersionIfNeeded();
createIndexTemplateIfNeeded();

String uri = config.host() + "/" + indexName() + getTypePath() + "/_bulk";
String uri = config.host() + "/" + indexName() + "/_bulk";
for (List<Meter> batch : MeterPartition.partition(this, config.batchSize())) {
try {
String requestBody = batch.stream()
Expand Down Expand Up @@ -244,21 +236,6 @@ protected void publish() {
}
}

private void determineMajorVersionIfNeeded() {
if (majorVersion != null) {
return;
}
try {
String responseBody = httpClient.get(config.host())
.withBasicAuthentication(config.userName(), config.password())
.send()
.body();
majorVersion = getMajorVersion(responseBody);
} catch (Throwable ex) {
throw new RuntimeException(ex);
}
}

// VisibleForTesting
static int getMajorVersion(String responseBody) {
Matcher matcher = MAJOR_VERSION_PATTERN.matcher(responseBody);
Expand All @@ -268,11 +245,6 @@ static int getMajorVersion(String responseBody) {
return Integer.parseInt(matcher.group(1));
}

@SuppressWarnings("ConstantConditions")
private String getTypePath() {
return majorVersion == null || majorVersion < 7 ? "/" + config.documentType() : TYPE_PATH_AFTER_VERSION_7;
}

// VisibleForTesting
static int countCreatedItems(String responseBody) {
Matcher matcher = STATUS_CREATED_PATTERN.matcher(responseBody);
Expand Down

This file was deleted.

0 comments on commit def8d3d

Please sign in to comment.