From 508c72f1d9ca01a2df4d173a5fcbe3bd29e3e8c9 Mon Sep 17 00:00:00 2001 From: basilisk487 Date: Fri, 3 Apr 2020 18:57:11 -0500 Subject: [PATCH 1/3] PUB-225: Java 11 migration --- README.md | 2 +- pkg/README.md | 2 +- pkg/after-install.sh | 10 ++-- pkg/before-remove.sh | 8 +-- pkg/etc/init.d/wavefront-proxy | 37 ++++++++----- pkg/stage.sh | 2 +- pom.xml | 8 +-- proxy/docker/Dockerfile | 2 +- proxy/docker/Dockerfile-rhel | 7 ++- proxy/docker/run.sh | 2 +- proxy/pom.xml | 2 +- ...Daemon.java => WavefrontProxyService.java} | 2 +- .../wavefront/common/DelegatingLogger.java | 52 ++++++++++++------- 13 files changed, 81 insertions(+), 55 deletions(-) rename proxy/src/main/java/com/wavefront/agent/{PushAgentDaemon.java => WavefrontProxyService.java} (91%) diff --git a/README.md b/README.md index b65e15976..9ef1c797b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ The [Wavefront Proxy](https://docs.wavefront.com/proxies.html) is a light-weight Java application that you send your metrics, histograms, and trace data to. It handles batching and transmission of your data to the Wavefront service in a secure, fast, and reliable manner. ## Requirements - * Java >= 1.8 + * Java 11 or higher * Maven ## Overview diff --git a/pkg/README.md b/pkg/README.md index 276d18404..847903b96 100644 --- a/pkg/README.md +++ b/pkg/README.md @@ -4,7 +4,7 @@ Wavefront Proxy Packaging Tools ----- * Install [docker](https://www.docker.com/). -* Install Java 8+/Maven. +* Install Java 11+/Maven. Methodology ----------- diff --git a/pkg/after-install.sh b/pkg/after-install.sh index 8c99351b2..1276687fa 100755 --- a/pkg/after-install.sh +++ b/pkg/after-install.sh @@ -7,7 +7,7 @@ spool_dir="/var/spool/wavefront-proxy" wavefront_dir="/opt/wavefront" conf_dir="/etc/wavefront" log_dir="/var/log/wavefront" -jre_dir="$wavefront_dir/$service_name/proxy-jre" +jre_dir="$wavefront_dir/$service_name/proxy-jre-11" # Set up wavefront user. if ! groupmod $group &> /dev/null; then @@ -46,10 +46,10 @@ chown $user:$group $conf_dir/$service_name if [[ ! -f $conf_dir/$service_name/wavefront.conf ]]; then if [[ -f $wavefront_dir/$service_name/conf/wavefront.conf ]]; then - echo "Copying $conf_dir/$service_name/wavefront.conf from $wavefront_dir/$service_name/conf/wavefront.conf" + echo "Copying $conf_dir/$service_name/wavefront.conf from $wavefront_dir/$service_name/conf/wavefront.conf" >&2 cp $wavefront_dir/$service_name/conf/wavefront.conf $conf_dir/$service_name/wavefront.conf else - echo "Creating $conf_dir/$service_name/wavefront.conf from template" + echo "Creating $conf_dir/$service_name/wavefront.conf from default template" >&2 cp $conf_dir/$service_name/wavefront.conf.default $conf_dir/$service_name/wavefront.conf fi else @@ -57,12 +57,12 @@ else fi if [[ ! -f $conf_dir/$service_name/preprocessor_rules.yaml ]]; then - echo "Creating $conf_dir/$service_name/preprocessor_rules.yaml from template" + echo "Creating $conf_dir/$service_name/preprocessor_rules.yaml from default template" >&2 cp $conf_dir/$service_name/preprocessor_rules.yaml.default $conf_dir/$service_name/preprocessor_rules.yaml fi if [[ ! -f $conf_dir/$service_name/log4j2.xml ]]; then - echo "Creating $conf_dir/$service_name/log4j2.xml from template" + echo "Creating $conf_dir/$service_name/log4j2.xml from default template" >&2 cp $conf_dir/$service_name/log4j2.xml.default $conf_dir/$service_name/log4j2.xml fi diff --git a/pkg/before-remove.sh b/pkg/before-remove.sh index d3a93785a..b8e3fdeab 100755 --- a/pkg/before-remove.sh +++ b/pkg/before-remove.sh @@ -2,7 +2,7 @@ service_name="wavefront-proxy" wavefront_dir="/opt/wavefront" -jre_dir="$wavefront_dir/$service_name/proxy-jre" +jre_dir="$wavefront_dir/$service_name/proxy-jre-11" #service wavefront-proxy stop || true @@ -15,8 +15,10 @@ jre_dir="$wavefront_dir/$service_name/proxy-jre" # - https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html if [[ "$1" == "0" ]] || [[ "$1" == "remove" ]] || [[ "$1" == "purge" ]]; then service wavefront-proxy stop || true - echo "Removing installed JRE from $jre_dir" - rm -rf $jre_dir + if [ -d $jre_dir ]; then + [ "$(ls -A $jre_dir)" ] && echo "Removing installed JRE from $jre_dir" >&2 + rm -rf $jre_dir + fi fi exit 0 diff --git a/pkg/etc/init.d/wavefront-proxy b/pkg/etc/init.d/wavefront-proxy index 8d9be7b55..a56683d54 100755 --- a/pkg/etc/init.d/wavefront-proxy +++ b/pkg/etc/init.d/wavefront-proxy @@ -35,7 +35,7 @@ user="wavefront" wavefront_dir="/opt/wavefront" proxy_dir=${PROXY_DIR:-$wavefront_dir/wavefront-proxy} config_dir=${CONFIG_DIR:-/etc/wavefront/wavefront-proxy} -proxy_jre_dir="$proxy_dir/proxy-jre" +proxy_jre_dir="$proxy_dir/proxy-jre-11" export JAVA_HOME=${PROXY_JAVA_HOME:-$proxy_jre_dir} conf_file=$CONF_FILE if [[ -z $conf_file ]]; then @@ -53,8 +53,8 @@ fi daemon_log_file=${DAEMON_LOG_FILE:-/var/log/wavefront/wavefront-daemon.log} err_file="/var/log/wavefront/wavefront-error.log" pid_file=${PID_FILE:-/var/run/$service_name.pid} -agent_jar=${AGENT_JAR:-$proxy_dir/bin/wavefront-push-agent.jar} -class="com.wavefront.agent.PushAgentDaemon" +proxy_jar=${AGENT_JAR:-$proxy_dir/bin/wavefront-proxy.jar} +class="com.wavefront.agent.WavefrontProxyService" app_args=${APP_ARGS:--f $conf_file} # If JAVA_ARGS is not set, try to detect memory size and set heap to 8GB if machine has more than 8GB. @@ -88,23 +88,23 @@ fi download_jre() { [[ -d $proxy_jre_dir ]] || mkdir -p $proxy_jre_dir - echo "Checking $conf_file for HTTP proxy settings" + echo "Checking $conf_file for HTTP proxy settings" >&2 proxy_host=$(grep "^\s*proxyHost=" $conf_file | cut -d'=' -f2) proxy_port=$(grep "^\s*proxyPort=" $conf_file | cut -d'=' -f2) proxy_user=$(grep "^\s*proxyUser=" $conf_file | cut -d'=' -f2) proxy_password=$(grep "^\s*proxyPassword=" $conf_file | cut -d'=' -f2) if [[ -n $proxy_host && -n $proxy_port ]]; then - echo "Using HTTP proxy $proxy_host:$proxy_port" + echo "Using HTTP proxy $proxy_host:$proxy_port" >&2 proxy_args="--proxy $proxy_host:$proxy_port" if [[ -n $proxy_user && -n $proxy_password ]]; then - echo "Authenticating as $proxy_user" + echo "Authenticating as $proxy_user" >&2 proxy_args+=" --proxy-user $proxy_user:$proxy_password" fi else - echo "No HTTP proxy configuration detected - attempting direct download" + echo "No HTTP proxy configuration detected - attempting direct download" >&2 fi - curl -L --silent -o /tmp/jre.tar.gz $proxy_args https://s3-us-west-2.amazonaws.com/wavefront-misc/proxy-jre.tgz || true + curl -L --silent -o /tmp/jre.tar.gz $proxy_args https://s3-us-west-2.amazonaws.com/wavefront-misc/proxy-jre-11.0.6-linux_x64.tar.gz || true tar -xf /tmp/jre.tar.gz --strip 1 -C $proxy_jre_dir || true rm /tmp/jre.tar.gz || true } @@ -115,8 +115,19 @@ download_jre() { # from wavefront.conf, if any, and try to download the JRE again. Ideally we should go back to bundling JRE # and not having to worry about accessibility of external resources. if [[ -z "$PROXY_JAVA_HOME" && ! -r $proxy_jre_dir/bin/java ]]; then - echo "JRE not found - trying to download and install" - download_jre + if type -p java; then + JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1) + if [[ $JAVA_VERSION == "11" ]]; then + JAVA_HOME=$($(dirname $(dirname $(readlink -f $(which javac))))) + echo "Using Java 11 detected in $JAVA_HOME" >&2 + else + echo "Java 11 not found - trying to download and install" >&2 + download_jre + fi + else + echo "Java 11 not found - trying to download and install" >&2 + download_jre + fi fi jsvc=$proxy_dir/bin/jsvc @@ -128,7 +139,7 @@ jsvc_exec() > $err_file fi - cd "$(dirname "$agent_jar")" + cd "$(dirname "$proxy_jar")" set +e # We want word splitting below, as we're building up a command line. @@ -136,7 +147,7 @@ jsvc_exec() $jsvc \ -user $user \ -home $JAVA_HOME \ - -cp $agent_jar \ + -cp $proxy_jar \ $java_args \ -Xss2049k \ -XX:OnOutOfMemoryError="kill -1 %p" \ @@ -150,7 +161,7 @@ jsvc_exec() $class \ $app_args &> $daemon_log_file if [[ $? -ne 0 ]]; then - echo "There was a problem, see $err_file and $daemon_log_file" + echo "There was a problem, see $err_file and $daemon_log_file" >&2 fi } diff --git a/pkg/stage.sh b/pkg/stage.sh index abe176ac0..f1291c306 100755 --- a/pkg/stage.sh +++ b/pkg/stage.sh @@ -61,4 +61,4 @@ ln -s ../commons-daemon/src/native/unix/jsvc jsvc echo "Stage the agent jar..." cd $PROG_DIR -cp $PUSH_AGENT_JAR $PROXY_DIR/bin/wavefront-push-agent.jar +cp $PUSH_AGENT_JAR $PROXY_DIR/bin/wavefront-proxy.jar diff --git a/pom.xml b/pom.xml index 77e2cfb90..86d617d55 100644 --- a/pom.xml +++ b/pom.xml @@ -10,8 +10,8 @@ pom - Wavefront All-in-One - Top-level Wavefront Public Java POM + Wavefront Proxy + Wavefront Proxy Project http://www.wavefront.com @@ -53,12 +53,12 @@ 4.12 0.29 - 1.8 + 11 2.12.1 2.9.10 2.9.10.3 4.1.45.Final - 2020-02.2 + 2020-03.4 none diff --git a/proxy/docker/Dockerfile b/proxy/docker/Dockerfile index a46deca76..ef0c960a8 100644 --- a/proxy/docker/Dockerfile +++ b/proxy/docker/Dockerfile @@ -17,7 +17,7 @@ RUN apt-get install -y sudo RUN apt-get install -y gnupg2 RUN apt-get install -y debian-archive-keyring RUN apt-get install -y apt-transport-https -RUN apt-get install -y openjdk-8-jdk +RUN apt-get install -y openjdk-11-jdk # Download wavefront proxy (latest release). Merely extract the debian, don't want to try running startup scripts. RUN echo "deb https://packagecloud.io/wavefront/proxy/ubuntu/ bionic main" > /etc/apt/sources.list.d/wavefront_proxy.list diff --git a/proxy/docker/Dockerfile-rhel b/proxy/docker/Dockerfile-rhel index 241278946..a1f44fece 100644 --- a/proxy/docker/Dockerfile-rhel +++ b/proxy/docker/Dockerfile-rhel @@ -2,7 +2,6 @@ FROM registry.access.redhat.com/ubi7 USER root - # This script may automatically configure wavefront without prompting, based on # these variables: # WAVEFRONT_URL (required) @@ -12,17 +11,17 @@ USER root # WAVEFRONT_PROXY_ARGS (default is none) # JAVA_ARGS (default is none) +RUN yum-config-manager --enable rhel-7-server-optional-rpms RUN yum update --disableplugin=subscription-manager -y && rm -rf /var/cache/yum RUN yum install -y sudo RUN yum install -y curl RUN yum install -y hostname -RUN yum install -y java-1.8.0-openjdk-devel.x86_64 +RUN yum install -y java-11-openjdk-devel -# Download wavefront proxy (latest release). Merely extract the debian, don't want to try running startup scripts. +# Download wavefront proxy (latest release). Merely extract the package, don't want to try running startup scripts. RUN curl -s https://packagecloud.io/install/repositories/wavefront/proxy/script.rpm.sh | sudo bash RUN yum -y update - RUN yum -y -q install wavefront-proxy # Configure agent diff --git a/proxy/docker/run.sh b/proxy/docker/run.sh index b50e29ce0..b3c4bdff3 100644 --- a/proxy/docker/run.sh +++ b/proxy/docker/run.sh @@ -25,7 +25,7 @@ java \ $jvm_container_opts $JAVA_ARGS \ -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager \ -Dlog4j.configurationFile=/etc/wavefront/wavefront-proxy/log4j2.xml \ - -jar /opt/wavefront/wavefront-proxy/bin/wavefront-push-agent.jar \ + -jar /opt/wavefront/wavefront-proxy/bin/wavefront-proxy.jar \ -h $WAVEFRONT_URL \ -t $WAVEFRONT_TOKEN \ --hostname ${WAVEFRONT_HOSTNAME:-$(hostname)} \ diff --git a/proxy/pom.xml b/proxy/pom.xml index 8473a6088..f187f9bef 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -9,7 +9,7 @@ - 1.8 + 11 proxy diff --git a/proxy/src/main/java/com/wavefront/agent/PushAgentDaemon.java b/proxy/src/main/java/com/wavefront/agent/WavefrontProxyService.java similarity index 91% rename from proxy/src/main/java/com/wavefront/agent/PushAgentDaemon.java rename to proxy/src/main/java/com/wavefront/agent/WavefrontProxyService.java index 201670f1a..042437f51 100644 --- a/proxy/src/main/java/com/wavefront/agent/PushAgentDaemon.java +++ b/proxy/src/main/java/com/wavefront/agent/WavefrontProxyService.java @@ -6,7 +6,7 @@ /** * @author Mori Bellamy (mori@wavefront.com) */ -public class PushAgentDaemon implements Daemon { +public class WavefrontProxyService implements Daemon { private PushAgent agent; private DaemonContext daemonContext; diff --git a/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java b/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java index 721f787a2..d8a9665cb 100644 --- a/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java +++ b/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java @@ -1,5 +1,9 @@ package com.wavefront.common; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Optional; +import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -37,28 +41,38 @@ public void log(LogRecord logRecord) { delegate.log(logRecord); } - /** - * This is a JDK8-specific implementation. TODO: switch to StackWalker after migrating to JDK9+ - */ private void inferCaller(LogRecord logRecord) { - StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); - boolean lookingForLogger = true; - for (StackTraceElement frame : stackTraceElements) { - String cname = frame.getClassName(); + Optional frame = new CallerFinder().get(); + frame.ifPresent(f -> { + logRecord.setSourceClassName(f.getClassName()); + logRecord.setSourceMethodName(f.getMethodName()); + }); + } + + static final class CallerFinder implements Predicate { + private static final StackWalker WALKER; + static { + final PrivilegedAction action = + () -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + WALKER = AccessController.doPrivileged(action); + } + + Optional get() { + return WALKER.walk(s -> s.filter(this).findFirst()); + } + + private boolean lookingForLogger = true; + + @Override + public boolean test(StackWalker.StackFrame t) { + final String cname = t.getClassName(); if (lookingForLogger) { - // Skip all frames until we have found the first logger frame. - if (cname.endsWith("Logger")) { - lookingForLogger = false; - } - } else { - if (!cname.endsWith("Logger") && !cname.startsWith("java.lang.reflect.") && - !cname.startsWith("sun.reflect.")) { - // We've found the relevant frame. - logRecord.setSourceClassName(cname); - logRecord.setSourceMethodName(frame.getMethodName()); - return; - } + // the log record could be created for a platform logger + lookingForLogger = !(cname.endsWith("Logger") || + cname.startsWith("sun.util.logging.PlatformLogger")); + return false; } + return !cname.endsWith("Logger"); } } } From 490c98266df98ce0b7e53af5be545bc0fbb27471 Mon Sep 17 00:00:00 2001 From: basilisk487 Date: Thu, 9 Apr 2020 18:44:10 -0500 Subject: [PATCH 2/3] Update to create 1.8 binaries but still default to 11 runtime --- README.md | 2 +- pkg/README.md | 2 +- pkg/after-install.sh | 2 +- pkg/before-remove.sh | 2 +- pkg/etc/init.d/wavefront-proxy | 22 ++++---- pom.xml | 2 +- proxy/pom.xml | 2 +- .../wavefront/common/DelegatingLogger.java | 52 +++++++------------ 8 files changed, 37 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 9ef1c797b..9a729bdb5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ The [Wavefront Proxy](https://docs.wavefront.com/proxies.html) is a light-weight Java application that you send your metrics, histograms, and trace data to. It handles batching and transmission of your data to the Wavefront service in a secure, fast, and reliable manner. ## Requirements - * Java 11 or higher + * Java 8 or higher * Maven ## Overview diff --git a/pkg/README.md b/pkg/README.md index 847903b96..276d18404 100644 --- a/pkg/README.md +++ b/pkg/README.md @@ -4,7 +4,7 @@ Wavefront Proxy Packaging Tools ----- * Install [docker](https://www.docker.com/). -* Install Java 11+/Maven. +* Install Java 8+/Maven. Methodology ----------- diff --git a/pkg/after-install.sh b/pkg/after-install.sh index 1276687fa..d9caf989d 100755 --- a/pkg/after-install.sh +++ b/pkg/after-install.sh @@ -7,7 +7,7 @@ spool_dir="/var/spool/wavefront-proxy" wavefront_dir="/opt/wavefront" conf_dir="/etc/wavefront" log_dir="/var/log/wavefront" -jre_dir="$wavefront_dir/$service_name/proxy-jre-11" +jre_dir="$wavefront_dir/$service_name/proxy-jre" # Set up wavefront user. if ! groupmod $group &> /dev/null; then diff --git a/pkg/before-remove.sh b/pkg/before-remove.sh index b8e3fdeab..a914e1616 100755 --- a/pkg/before-remove.sh +++ b/pkg/before-remove.sh @@ -2,7 +2,7 @@ service_name="wavefront-proxy" wavefront_dir="/opt/wavefront" -jre_dir="$wavefront_dir/$service_name/proxy-jre-11" +jre_dir="$wavefront_dir/$service_name/proxy-jre" #service wavefront-proxy stop || true diff --git a/pkg/etc/init.d/wavefront-proxy b/pkg/etc/init.d/wavefront-proxy index a56683d54..2899ebe71 100755 --- a/pkg/etc/init.d/wavefront-proxy +++ b/pkg/etc/init.d/wavefront-proxy @@ -35,7 +35,7 @@ user="wavefront" wavefront_dir="/opt/wavefront" proxy_dir=${PROXY_DIR:-$wavefront_dir/wavefront-proxy} config_dir=${CONFIG_DIR:-/etc/wavefront/wavefront-proxy} -proxy_jre_dir="$proxy_dir/proxy-jre-11" +proxy_jre_dir="$proxy_dir/proxy-jre" export JAVA_HOME=${PROXY_JAVA_HOME:-$proxy_jre_dir} conf_file=$CONF_FILE if [[ -z $conf_file ]]; then @@ -85,6 +85,11 @@ if [[ -r $proxy_launch_conf ]]; then fi fi +# Workaround for environments with locked-down internet access where approved traffic goes through HTTP proxy. +# If JRE cannot be found in $proxy_jre_dir (most likely because its download failed during installation), and +# $PROXY_JAVA_HOME pointing to a user-defined JRE is not defined either, we'll try to read HTTP proxy settings +# from wavefront.conf, if any, and try to download the JRE again. Ideally we should go back to bundling JRE +# and not having to worry about accessibility of external resources. download_jre() { [[ -d $proxy_jre_dir ]] || mkdir -p $proxy_jre_dir @@ -109,23 +114,20 @@ download_jre() { rm /tmp/jre.tar.gz || true } -# Workaround for environments with locked-down internet access where approved traffic goes through HTTP proxy. -# If JRE cannot be found in $proxy_jre_dir (most likely because its download failed during installation), and -# $PROXY_JAVA_HOME pointing to a user-defined JRE is not defined either, we'll try to read HTTP proxy settings -# from wavefront.conf, if any, and try to download the JRE again. Ideally we should go back to bundling JRE -# and not having to worry about accessibility of external resources. +# If $PROXY_JAVA_HOME is not defined and there is no JRE in $proxy_jre_dir, try to auto-detect +# locally installed JDK first. We will accept 1.8, 9, 10, 11. if [[ -z "$PROXY_JAVA_HOME" && ! -r $proxy_jre_dir/bin/java ]]; then if type -p java; then JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1) - if [[ $JAVA_VERSION == "11" ]]; then + if [[ $JAVA_VERSION == "11" || $JAVA_VERSION == "10" || $JAVA_VERSION == "9" || $JAVA_VERSION == "1.8" ]]; then JAVA_HOME=$($(dirname $(dirname $(readlink -f $(which javac))))) - echo "Using Java 11 detected in $JAVA_HOME" >&2 + echo "Using Java runtime $JAVA_VERSION detected in $JAVA_HOME" >&2 else - echo "Java 11 not found - trying to download and install" >&2 + echo "Java runtime [1.8; 12) not found - trying to download and install" >&2 download_jre fi else - echo "Java 11 not found - trying to download and install" >&2 + echo "Java runtime not found - trying to download and install" >&2 download_jre fi fi diff --git a/pom.xml b/pom.xml index 86d617d55..e066798d9 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,7 @@ 4.12 0.29 - 11 + 1.8 2.12.1 2.9.10 2.9.10.3 diff --git a/proxy/pom.xml b/proxy/pom.xml index f187f9bef..8473a6088 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -9,7 +9,7 @@ - 11 + 1.8 proxy diff --git a/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java b/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java index d8a9665cb..721f787a2 100644 --- a/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java +++ b/proxy/src/main/java/com/wavefront/common/DelegatingLogger.java @@ -1,9 +1,5 @@ package com.wavefront.common; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.Optional; -import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -41,38 +37,28 @@ public void log(LogRecord logRecord) { delegate.log(logRecord); } + /** + * This is a JDK8-specific implementation. TODO: switch to StackWalker after migrating to JDK9+ + */ private void inferCaller(LogRecord logRecord) { - Optional frame = new CallerFinder().get(); - frame.ifPresent(f -> { - logRecord.setSourceClassName(f.getClassName()); - logRecord.setSourceMethodName(f.getMethodName()); - }); - } - - static final class CallerFinder implements Predicate { - private static final StackWalker WALKER; - static { - final PrivilegedAction action = - () -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); - WALKER = AccessController.doPrivileged(action); - } - - Optional get() { - return WALKER.walk(s -> s.filter(this).findFirst()); - } - - private boolean lookingForLogger = true; - - @Override - public boolean test(StackWalker.StackFrame t) { - final String cname = t.getClassName(); + StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); + boolean lookingForLogger = true; + for (StackTraceElement frame : stackTraceElements) { + String cname = frame.getClassName(); if (lookingForLogger) { - // the log record could be created for a platform logger - lookingForLogger = !(cname.endsWith("Logger") || - cname.startsWith("sun.util.logging.PlatformLogger")); - return false; + // Skip all frames until we have found the first logger frame. + if (cname.endsWith("Logger")) { + lookingForLogger = false; + } + } else { + if (!cname.endsWith("Logger") && !cname.startsWith("java.lang.reflect.") && + !cname.startsWith("sun.reflect.")) { + // We've found the relevant frame. + logRecord.setSourceClassName(cname); + logRecord.setSourceMethodName(frame.getMethodName()); + return; + } } - return !cname.endsWith("Logger"); } } } From 6a956f6e6cfa4e41c8844d8c36ef750c07497c9d Mon Sep 17 00:00:00 2001 From: basilisk487 Date: Thu, 9 Apr 2020 19:08:03 -0500 Subject: [PATCH 3/3] Fix flaky unit test --- .../com/wavefront/agent/PointMatchers.java | 23 +++++++ .../logsharvesting/LogsIngesterTest.java | 61 +++++++------------ 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/proxy/src/test/java/com/wavefront/agent/PointMatchers.java b/proxy/src/test/java/com/wavefront/agent/PointMatchers.java index 55ae58f32..4d7c9cb0c 100644 --- a/proxy/src/test/java/com/wavefront/agent/PointMatchers.java +++ b/proxy/src/test/java/com/wavefront/agent/PointMatchers.java @@ -6,6 +6,7 @@ import java.util.Map; +import wavefront.report.Histogram; import wavefront.report.ReportPoint; /** @@ -98,4 +99,26 @@ public void describeTo(Description description) { }; } + public static Matcher histogramMatches(int samples, double weight) { + return new BaseMatcher() { + + @Override + public boolean matches(Object o) { + ReportPoint point = (ReportPoint) o; + if (!(point.getValue() instanceof Histogram)) return false; + Histogram value = (Histogram) point.getValue(); + double sum = 0; + for (int i = 0; i < value.getBins().size(); i++) { + sum += value.getBins().get(i) * value.getCounts().get(i); + } + return sum == weight && value.getCounts().stream().reduce(Integer::sum).get() == samples; + } + + @Override + public void describeTo(Description description) { + description.appendText( + "Total histogram weight should be " + weight + ", and total samples = " + samples); + } + }; + } } diff --git a/proxy/src/test/java/com/wavefront/agent/logsharvesting/LogsIngesterTest.java b/proxy/src/test/java/com/wavefront/agent/logsharvesting/LogsIngesterTest.java index 501360d7b..1df1d78a5 100644 --- a/proxy/src/test/java/com/wavefront/agent/logsharvesting/LogsIngesterTest.java +++ b/proxy/src/test/java/com/wavefront/agent/logsharvesting/LogsIngesterTest.java @@ -1,12 +1,28 @@ package com.wavefront.agent.logsharvesting; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; + +import org.easymock.Capture; +import org.easymock.CaptureType; +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Test; +import org.logstash.beats.Message; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.wavefront.agent.PointMatchers; import com.wavefront.agent.auth.TokenAuthenticatorBuilder; import com.wavefront.agent.channel.NoopHealthCheckManager; @@ -20,23 +36,6 @@ import com.wavefront.common.MetricConstants; import com.wavefront.data.ReportableEntityType; -import org.easymock.Capture; -import org.easymock.CaptureType; -import org.easymock.EasyMock; -import org.junit.After; -import org.junit.Test; -import org.logstash.beats.Message; - -import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.Consumer; - import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import oi.thekraken.grok.api.exception.GrokException; @@ -54,7 +53,6 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.lessThan; @@ -568,24 +566,9 @@ public void testWavefrontHistogramMultipleCentroids() throws Exception { lines[i] = "histo " + (i + 1); } List reportPoints = getPoints(mockHistogramHandler, 2, 500, this::receiveLog, lines); - ReportPoint reportPoint = reportPoints.get(0); - assertThat(reportPoint.getValue(), instanceOf(Histogram.class)); - Histogram wavefrontHistogram = (Histogram) reportPoint.getValue(); - double sum = 0; - for (int i = 0; i < wavefrontHistogram.getBins().size(); i++) { - sum += wavefrontHistogram.getBins().get(i) * wavefrontHistogram.getCounts().get(i); - } - assertThat(sum, equalTo(7260.0)); - assertThat(wavefrontHistogram.getCounts().stream().reduce(Integer::sum).get(), equalTo(120)); - reportPoint = reportPoints.get(1); - assertThat(reportPoint.getValue(), instanceOf(Histogram.class)); - wavefrontHistogram = (Histogram) reportPoint.getValue(); - sum = 0; - for (int i = 0; i < wavefrontHistogram.getBins().size(); i++) { - sum += wavefrontHistogram.getBins().get(i) * wavefrontHistogram.getCounts().get(i); - } - assertThat(sum, equalTo(21660.0)); - assertThat(wavefrontHistogram.getCounts().stream().reduce(Integer::sum).get(), equalTo(120)); + assertThat(reportPoints.size(), equalTo(2)); + assertThat(reportPoints, containsInAnyOrder(PointMatchers.histogramMatches(120, 7260.0), + PointMatchers.histogramMatches(120, 21660.0))); } @Test(expected = ConfigurationException.class)