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 7679e99
Show file tree
Hide file tree
Showing 24 changed files with 944 additions and 255 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
!docker/test-images/zipkin-elasticsearch8/config/
!docker/test-images/zipkin-elasticsearch8/start-elasticsearch

!docker/test-images/zipkin-opensearch1/config/
!docker/test-images/zipkin-opensearch1/start-opensearch
!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-opensearch1/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=17.0.10_p7

# 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-opensearch1/start-opensearch /docker-bin/
COPY docker/test-images/zipkin-opensearch1/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 opensearch1_version=1.3.15

# 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/${opensearch1_version}/opensearch-${opensearch1_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-opensearch1
LABEL org.opencontainers.image.description="OpenSearch distribution on OpenJDK and Alpine Linux"
ARG opensearch1_version=1.3.15
LABEL opensearch-version=$opensearch1_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-opensearch1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## zipkin-opensearch1 Docker image

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

To build `openzipkin/zipkin-opensearch1:test`, from the top-level of the repository, run:
```bash
$ DOCKER_FILE=docker/test-images/zipkin-opensearch1/Dockerfile build-bin/docker/docker_build openzipkin/zipkin-opensearch1: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-opensearch1/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-opensearch1/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-opensearch1/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
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-opensearch1/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"
```
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);
}
}
}
}
Loading

0 comments on commit 7679e99

Please sign in to comment.