-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Zipkin support of OpenSearch storage
Signed-off-by: Andriy Redko <[email protected]>
- Loading branch information
Showing
15 changed files
with
670 additions
and
255 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
zipkin-storage/elasticsearch/src/main/java/zipkin2/elasticsearch/BaseVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package zipkin2.elasticsearch; | ||
|
||
import static zipkin2.elasticsearch.internal.JsonReaders.enterPath; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Supplier; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.linecorp.armeria.common.AggregatedHttpRequest; | ||
import com.linecorp.armeria.common.HttpMethod; | ||
|
||
import zipkin2.elasticsearch.internal.client.HttpCall; | ||
|
||
/** | ||
* Base version for both Elasticsearch and OpenSearch distributions. | ||
*/ | ||
public abstract class BaseVersion { | ||
final int major, minor; | ||
|
||
BaseVersion(int major, int minor) { | ||
this.major = major; | ||
this.minor = minor; | ||
} | ||
|
||
/** | ||
* Gets the version for particular distribution, returns either {@link ElasticsearchVersion} | ||
* or {@link OpensearchVersion} instance. | ||
* @param http the HTTP client | ||
* @return either {@link ElasticsearchVersion} or {@link OpensearchVersion} instance | ||
* @throws IOException in case of I/O errors | ||
*/ | ||
static BaseVersion get(HttpCall.Factory http) throws IOException { | ||
return Parser.INSTANCE.get(http); | ||
} | ||
|
||
/** | ||
* Does this version of Elasticsearch / OpenSearch still support mapping types? | ||
* @return "true" if mapping types are supported, "false" otherwise | ||
*/ | ||
public abstract boolean supportsTypes(); | ||
|
||
enum Parser implements HttpCall.BodyConverter<BaseVersion> { | ||
INSTANCE; | ||
|
||
final Pattern REGEX = Pattern.compile("(\\d+)\\.(\\d+).*"); | ||
|
||
BaseVersion get(HttpCall.Factory callFactory) throws IOException { | ||
AggregatedHttpRequest getNode = AggregatedHttpRequest.of(HttpMethod.GET, "/"); | ||
BaseVersion version = callFactory.newCall(getNode, this, "get-node").execute(); | ||
if (version == null) { | ||
throw new IllegalArgumentException("No content reading Elasticsearch version"); | ||
} | ||
return version; | ||
} | ||
|
||
@Override | ||
public BaseVersion convert(JsonParser parser, Supplier<String> contentString) { | ||
String version = null; | ||
String distribution = null; | ||
try { | ||
if (enterPath(parser, "version") != null) { | ||
while (parser.nextToken() != null) { | ||
if (parser.currentToken() == JsonToken.VALUE_STRING) { | ||
if (parser.currentName() == "distribution") { | ||
distribution = parser.getText(); | ||
} else if (parser.currentName() == "number") { | ||
version = parser.getText(); | ||
} | ||
} | ||
} | ||
} | ||
} catch (RuntimeException | IOException possiblyParseException) { | ||
// EmptyCatch ignored | ||
} | ||
|
||
if (version == null) { | ||
throw new IllegalArgumentException( | ||
".version.number not found in response: " + contentString.get()); | ||
} | ||
|
||
Matcher matcher = REGEX.matcher(version); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException("Invalid .version.number: " + version); | ||
} | ||
|
||
try { | ||
int major = Integer.parseInt(matcher.group(1)); | ||
int minor = Integer.parseInt(matcher.group(2)); | ||
if ("opensearch".equalsIgnoreCase(distribution)) { | ||
return new OpensearchVersion(major, minor); | ||
} else { | ||
return new ElasticsearchVersion(major, minor); | ||
} | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("Invalid .version.number: " + version | ||
+ ", for .version.distribution:" + distribution); | ||
} | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...age/elasticsearch/src/main/java/zipkin2/elasticsearch/ElasticsearchSpecificTemplates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package zipkin2.elasticsearch; | ||
|
||
import static zipkin2.elasticsearch.ElasticsearchVersion.V5_0; | ||
import static zipkin2.elasticsearch.ElasticsearchVersion.V6_0; | ||
import static zipkin2.elasticsearch.ElasticsearchVersion.V6_7; | ||
import static zipkin2.elasticsearch.ElasticsearchVersion.V7_0; | ||
import static zipkin2.elasticsearch.ElasticsearchVersion.V7_8; | ||
import static zipkin2.elasticsearch.ElasticsearchVersion.V9_0; | ||
|
||
import zipkin2.internal.Nullable; | ||
|
||
final class ElasticsearchSpecificTemplates extends VersionSpecificTemplates<ElasticsearchVersion> { | ||
static class DistributionTemplate extends DistributionSpecificTemplates { | ||
private final ElasticsearchVersion version; | ||
|
||
DistributionTemplate(ElasticsearchVersion version) { | ||
this.version = version; | ||
} | ||
|
||
@Override String indexTemplatesUrl(String indexPrefix, String type, @Nullable Integer templatePriority) { | ||
if (version.compareTo(V7_8) >= 0 && templatePriority != null) { | ||
return "/_index_template/" + indexPrefix + type + "_template"; | ||
} | ||
if (version.compareTo(V6_7) >= 0 && version.compareTo(V7_0) < 0) { | ||
// because deprecation warning on 6 to prepare for 7: | ||
// | ||
// [types removal] The parameter include_type_name should be explicitly specified in get | ||
// template requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', | ||
// which means responses will omit the type name in mapping definitions. | ||
// | ||
// The parameter include_type_name was added in 6.7. Using this with ES older than | ||
// 6.7 will result in unrecognized parameter: [include_type_name]. | ||
return "/_template/" + indexPrefix + type + "_template?include_type_name=true"; | ||
} | ||
|
||
return "/_template/" + indexPrefix + type + "_template"; | ||
} | ||
|
||
@Override char indexTypeDelimiter() { | ||
return ElasticsearchSpecificTemplates.indexTypeDelimiter(version); | ||
} | ||
|
||
@Override | ||
IndexTemplates get(String indexPrefix, int indexReplicas, int indexShards, | ||
boolean searchEnabled, boolean strictTraceId, Integer templatePriority) { | ||
return new ElasticsearchSpecificTemplates(indexPrefix, indexReplicas, indexShards, | ||
searchEnabled, strictTraceId, templatePriority).get(version); | ||
} | ||
} | ||
|
||
ElasticsearchSpecificTemplates(String indexPrefix, int indexReplicas, int indexShards, | ||
boolean searchEnabled, boolean strictTraceId, Integer templatePriority) { | ||
super(indexPrefix, indexReplicas,indexShards, searchEnabled, strictTraceId, templatePriority); | ||
} | ||
|
||
@Override String indexPattern(String type, ElasticsearchVersion version) { | ||
return '"' | ||
+ (version.compareTo(V6_0) < 0 ? "template" : "index_patterns") | ||
+ "\": \"" | ||
+ indexPrefix | ||
+ indexTypeDelimiter(version) | ||
+ type | ||
+ "-*" | ||
+ "\""; | ||
} | ||
|
||
@Override boolean useComposableTemplate(ElasticsearchVersion version) { | ||
return (version.compareTo(V7_8) >= 0 && templatePriority != null); | ||
} | ||
|
||
/** | ||
* This returns a delimiter based on what's supported by the Elasticsearch version. | ||
* | ||
* <p>Starting in Elasticsearch 7.x, colons are no longer allowed in index names. This logic will | ||
* make sure the pattern in our index template doesn't use them either. | ||
* | ||
* <p>See https://github.com/openzipkin/zipkin/issues/2219 | ||
*/ | ||
static char indexTypeDelimiter(ElasticsearchVersion version) { | ||
return version.compareTo(V7_0) < 0 ? ':' : '-'; | ||
} | ||
|
||
@Override String maybeWrap(String type, ElasticsearchVersion version, String json) { | ||
// ES 7.x defaults include_type_name to false https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#_literal_include_type_name_literal_now_defaults_to_literal_false_literal | ||
if (version.compareTo(V7_0) >= 0) return json; | ||
return " \"" + type + "\": {\n " + json.replace("\n", "\n ") + " }\n"; | ||
} | ||
|
||
@Override IndexTemplates get(ElasticsearchVersion version) { | ||
if (version.compareTo(V5_0) < 0 || version.compareTo(V9_0) >= 0) { | ||
throw new IllegalArgumentException( | ||
"Elasticsearch versions 5-8.x are supported, was: " + version); | ||
} | ||
return IndexTemplates.newBuilder() | ||
.version(version) | ||
.indexTypeDelimiter(indexTypeDelimiter(version)) | ||
.span(spanIndexTemplate(version)) | ||
.dependency(dependencyTemplate(version)) | ||
.autocomplete(autocompleteTemplate(version)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.