Skip to content

Commit

Permalink
Add Zipkin support of OpenSearch storage
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Apr 17, 2024
1 parent f651485 commit 77197e1
Show file tree
Hide file tree
Showing 20 changed files with 793 additions and 255 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
!docker/test-images/zipkin-elasticsearch8/config/
!docker/test-images/zipkin-elasticsearch8/start-elasticsearch

!docker/test-images/zipkin-opensearch2/config/
!docker/test-images/zipkin-opensearch2/start-opensearch

!docker/test-images/zipkin-eureka/src/
!docker/test-images/zipkin-eureka/pom.xml
!docker/test-images/zipkin-eureka/start-eureka
Expand Down
73 changes: 73 additions & 0 deletions docker/test-images/zipkin-opensearch2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

# java_version is used for install and runtime layers of zipkin-opensearch2
#
# Use latest version here: https://github.com/orgs/openzipkin/packages/container/package/java
# This is defined in many places because Docker has no "env" script functionality unless you use
# docker-compose: When updating, update everywhere.
ARG java_version=21.0.2_p13

# We copy files from the context into a scratch container first to avoid a problem where docker and
# docker-compose don't share layer hashes https://github.com/docker/compose/issues/883 normally.
# COPY --from= works around the issue.
FROM scratch as scratch

COPY build-bin/docker/docker-healthcheck /docker-bin/
COPY docker/test-images/zipkin-opensearch2/start-opensearch /docker-bin/
COPY docker/test-images/zipkin-opensearch2/config/ /config/

FROM ghcr.io/openzipkin/java:${java_version} as install

WORKDIR /install

# Use latest 2.x version from https://opensearch.org/downloads.html
# This is defined in many places because Docker has no "env" script functionality unless you use
# docker-compose: When updating, update everywhere.
ARG opensearch2_version=2.13.0

# Download only the OSS distribution (lacks X-Pack)
RUN \
# Connection resets are frequent in GitHub Actions workflows \
wget --random-wait --tries=5 -qO- \
# We don't download bin scripts as we customize for reasons including BusyBox problems
https://artifacts.opensearch.org/releases/bundle/opensearch/${opensearch2_version}/opensearch-${opensearch2_version}-linux-x64.tar.gz| tar xz \
--wildcards --strip=1 --exclude=jdk
COPY --from=scratch /config/ ./config/

# Use a full Java distribution rather than adding test modules to the
# production -jre base layer used by zipkin and zipkin-slim.
FROM ghcr.io/openzipkin/java:${java_version} as zipkin-opensearch2
LABEL org.opencontainers.image.description="OpenSearch distribution on OpenJDK and Alpine Linux"
ARG opensearch2_version=2.13.0
LABEL opensearch-version=$opensearch2_version

# Install bash
RUN apk update && apk add bash

# The full license is also included in the image at /opensearch/LICENSE.txt.
LABEL org.opencontainers.image.licenses="Apache-2.0"

# Add HEALTHCHECK and ENTRYPOINT scripts into the default search path
COPY --from=scratch /docker-bin/* /usr/local/bin/
# We use start period of 30s to avoid marking the container unhealthy on slow or contended CI hosts
HEALTHCHECK --interval=1s --start-period=30s --timeout=5s CMD ["docker-healthcheck"]
ENTRYPOINT ["start-opensearch"]

# All content including binaries and logs write under WORKDIR
ARG USER=opensearch
WORKDIR /${USER}

# Ensure the process doesn't run as root
RUN adduser -g '' -h ${PWD} -D ${USER}
USER ${USER}

# Copy binaries and config we installed earlier
COPY --from=install --chown=${USER} /install .

# Use to set heap, trust store or other system properties.
ENV OPENSEARCH_JAVA_OPTS="-Xms512m -Xmx512m -XX:+ExitOnOutOfMemoryError"
ENV LIBFFI_TMPDIR=/tmp
EXPOSE 9200
27 changes: 27 additions & 0 deletions docker/test-images/zipkin-opensearch2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## zipkin-opensearch2 Docker image

The `zipkin-opensearch2` testing image runs OpenSearch 2.x for [Elasticsearch storage](../../../zipkin-storage/elasticsearch)
integration.

To build `openzipkin/zipkin-opensearch2:test`, from the top-level of the repository, run:
```bash
$ DOCKER_FILE=docker/test-images/zipkin-opensearch2/Dockerfile build-bin/docker/docker_build openzipkin/zipkin-opensearch2:test
```

You can use the env variable `OPENSEARCH_JAVA_OPTS` to change settings such as heap size for OpenSearch.

#### Host setup

OpenSearch is [strict](https://github.com/docker-library/docs/tree/master/elasticsearch#host-setup)
about virtual memory. You will need to adjust accordingly (especially if you notice OpenSearch crash!)

```bash
# If docker is running on your host machine, adjust the kernel setting directly
$ sudo sysctl -w vm.max_map_count=262144

# If using docker-machine/Docker Toolbox/Boot2Docker, remotely adjust the same
$ docker-machine ssh default "sudo sysctl -w vm.max_map_count=262144"

# If using colima, it is similar as well
$ colima ssh "sudo sysctl -w vm.max_map_count=262144"
```
14 changes: 14 additions & 0 deletions docker/test-images/zipkin-opensearch2/config/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

status = error

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n

rootLogger.level = info
rootLogger.appenderRef.console.ref = console
21 changes: 21 additions & 0 deletions docker/test-images/zipkin-opensearch2/config/opensearch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

cluster.name: "docker-cluster"
network.host: 0.0.0.0
node.name: zipkin-opensearch

# Enable development mode and disable bootstrap checks
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
discovery.type: single-node
# Avoid deprecation errors: as of 8.x the only accepted value is true.
cluster.routing.allocation.disk.watermark.enable_for_single_data_node: true

# This is a test image, so we are not afraid to delete all indices. Avoids:
# Wildcard expressions or all indices are not allowed
action.destructive_requires_name: false

# Disable security
plugins.security.disabled: true
17 changes: 17 additions & 0 deletions docker/test-images/zipkin-opensearch2/start-opensearch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

# ENTRYPOINT script that starts OpenSearch
#
# This intentionally locates config using the current working directory, in order to consolidate
# Dockerfile instructions to WORKDIR
set -eu

# Configure the Docker HEALTHCHECK
export HEALTHCHECK_PORT=9200
export HEALTHCHECK_PATH=/_cluster/health

exec ./bin/opensearch
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);
}
}
}
}
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();
}
}
Loading

0 comments on commit 77197e1

Please sign in to comment.