Skip to content

Commit

Permalink
Replace the internal custom logger with Log4j standard loggers (#150)
Browse files Browse the repository at this point in the history
* Replace the internal custom logger with Log4j standard loggers

The primary work here is to remove the internal custom logging library
and replace it with a standard Log4j setup. This lets the user define
their own logging settings in a standard way that doesn't require a
bunch of custom settings in the AgentConfiguration class.

Unfortunately there are a few other fixes in here that we've squashed
down - I've tried to list them here:

* Fix a few bugs in the Ubuntu init script
* Dynamically find the 'service' command path in the babysit script
* Leverate the actual init scripts for auto-restarting the agent if necessary.
* A few more Ubuntu init script bug fixes.
* Package the app up into amazon-kinesis-agent-<tag>.tar.gz files.
* Bugfix: /etc/default, not /etc/defaults on Ubuntu
* Bugfix on the babysit script: --ppid doesn't work everywhere.
* Remove custom logger

* Revert setup -> setup.sh change

* Add back inadvertantly removed lines from the babysit script
  • Loading branch information
diranged authored and chaochenq committed Oct 9, 2018
1 parent a52ac1a commit 627d226
Show file tree
Hide file tree
Showing 34 changed files with 81 additions and 457 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ install:
before_deploy:
- mvn clean
- sudo ./setup --build
- tar -zcvf /tmp/binary.tgz .
- tar -zcvf /tmp/amazon-kinesis-agent-${TRAVIS_TAG}.tgz .
deploy:
- provider: releases
file: /tmp/binary.tgz
file: /tmp/amazon-kinesis-agent-${TRAVIS_TAG}.tgz
api_key: "$OAUTH_TOKEN"
skip_cleanup: true
on:
Expand Down
18 changes: 16 additions & 2 deletions bin/aws-kinesis-agent-babysit
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
DAEMON_NAME=aws-kinesis-agent
PIDFILE=/var/run/$DAEMON_NAME.pid
SERVICE="service $DAEMON_NAME"

# If using systemd and the service is disabled skip a babysit check.
#
# We don't do this for upstart/sysvinit systems as a `restart` will fail immediately
# when trying to a stop a service that isn't registred to be running.
# Additionally checking if a service is enabled is not straightforward as in systemd.
if [ -d /run/systemd/system ]; then
systemctl is-enabled "${DAEMON_NAME}.service" >/dev/null 2>&1 || exit 0
fi

function start_agent() {
$SERVICE restart || exit 1
sleep 3
Expand All @@ -15,8 +25,12 @@ function start_agent() {
$SERVICE status >/dev/null 2>&1
status=$?

if [ "$status" -eq "1" -o "$status" -eq "2" ]; then
# Check if PID file exists.
# If it does not, it means either the agent was never started or it was stopped by the user.
[[ -f $PIDFILE ]] || exit 0

if [ "$status" -eq "1" -o "$status" -eq "2" -o "$status" -eq "3" ]; then
start_agent
fi

exit 0
exit 0
7 changes: 4 additions & 3 deletions bin/aws-kinesis-agent.RedHat
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ LOG_DIR=/var/log/$DAEMON_NAME
[[ -d $STATE_HOME ]] || install -o $AGENT_USER -g $AGENT_USER -d $STATE_HOME
MUTEXFILE=$STATE_HOME/mutex
SHUTDOWN_TIME=11 #10 second default value in AgentConfiguration.java, +1 second buffer
AGENT_LOG_LEVEL=${AGENT_LOG_LEVEL:-INFO}
LOG_CONFIG=${LOG_CONFIG:-/etc/aws-kinesis/log4j.xml}
AGENT_ARGS=${AGENT_ARGS:-}

INITLOGFILE=/tmp/$DAEMON_NAME.`date '+%Y%m%d%H%M%S'`.initlog
Expand All @@ -67,8 +67,9 @@ do_start () {
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION
export JVM_ARGS="$JVM_ARGS -Dlog4j.configuration=file://$LOG_CONFIG "

DAEMON_NAME=$DAEMON_NAME nohup runuser $AGENT_USER -s /bin/sh -c "$DAEMON_EXEC -L $AGENT_LOG_LEVEL $AGENT_ARGS $@" > $INITLOGFILE 2>&1 &
DAEMON_NAME=$DAEMON_NAME nohup runuser $AGENT_USER -s /bin/sh -c "$DAEMON_EXEC $AGENT_ARGS $@" > $INITLOGFILE 2>&1 &

pid=$!
echo $pid > $PIDFILE
Expand Down Expand Up @@ -223,4 +224,4 @@ fi
;;
esac
exit $RETVAL
) 200>&-
) 200>&-
8 changes: 5 additions & 3 deletions bin/aws-kinesis-agent.Ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ INITLOGFILE=/tmp/$DAEMON_NAME.`date '+%Y%m%d%H%M%S'`.initlog
# This script is in /etc/rc.d/init.d/ and the executable is in /usr/bin
BASEDIR=${BASEDIR%/}
DAEMON_EXEC=$BASEDIR/usr/bin/start-$DAEMON_NAME
LOGLEVEL=${LOGLEVEL:-INFO}
LOG_CONFIG=${LOG_CONFIG:-/etc/aws-kinesis/log4j.xml}
ARGS=${ARGS:-}
RETVAL=0


# load any configs/environment from /etc/default/<name>
[ -e /etc/default/$DAEMON_NAME ] && . /etc/default/$DAEMON_NAME

export JVM_ARGS="$JVM_ARGS -Dlog4j.configuration=file://$LOG_CONFIG "

# Export the settings
do_start () {
# Return
Expand All @@ -48,7 +51,6 @@ do_start () {
--pidfile $PIDFILE \
--chuid $AGENT_USER \
--startas $DAEMON_EXEC -- \
-L $LOGLEVEL \
$ARGS
}

Expand Down Expand Up @@ -103,4 +105,4 @@ case "$command" in
exit 1
;;
esac
exit $RETVAL
exit $RETVAL
3 changes: 2 additions & 1 deletion setup.sh → setup
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ do_install () {
install -m755 ./bin/${daemon_name}.${dist} ${init_dir}/${daemon_name}
install -m644 ./support/${daemon_name}.cron ${cron_dir}/${daemon_name}
install -m644 ./support/${daemon_name}.sysconfig ${sysconfig_dir}/${daemon_name}

install -m644 ./support/log4j.xml ${config_dir}

# add a user for starting the agent
id -u $agent_user_name > /dev/null 2>&1 || \
useradd -c "Streaming Data Agent" -r $agent_user_name
Expand Down
12 changes: 4 additions & 8 deletions src/com/amazon/kinesis/streaming/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazon.kinesis.streaming.agent.config.AgentConfiguration;
import com.amazon.kinesis.streaming.agent.config.AgentOptions;
Expand Down Expand Up @@ -55,12 +56,7 @@ public static void main(String[] args) throws Exception {
AgentOptions opts = AgentOptions.parse(args);
String configFile = opts.getConfigFile();
AgentConfiguration config = tryReadConfigurationFile(Paths.get(opts.getConfigFile()));
Path logFile = opts.getLogFile() != null ? Paths.get(opts.getLogFile()) : (config != null ? config.logFile() : null);
String logLevel = config != null ? config.logLevel() : (opts.getLogLevel() != null ? opts.getLogLevel() : null );
int logMaxBackupFileIndex = (config != null ? config.logMaxBackupIndex() : -1);
long logMaxFileSize = (config != null ? config.logMaxFileSize() : -1L);
Logging.initialize(logFile, logLevel, logMaxBackupFileIndex, logMaxFileSize);
final Logger logger = Logging.getLogger(Agent.class);
final Logger logger = LoggerFactory.getLogger(Agent.class);

// Install an unhandled exception hook
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
Expand Down Expand Up @@ -141,7 +137,7 @@ private static AgentConfiguration tryReadConfigurationFile(Path configFile) {

private static AgentConfiguration readConfigurationDirectory(AgentConfiguration agentConfiguration) {
final String DEFAULT_CONFIG_DIRECTORY = "/etc/aws-kinesis/agent.d/";
final Logger logger = Logging.getLogger(Agent.class);
final Logger logger = LoggerFactory.getLogger(Agent.class);

File configDir = new File(DEFAULT_CONFIG_DIRECTORY);

Expand Down Expand Up @@ -188,7 +184,7 @@ private static AgentConfiguration readConfigurationDirectory(AgentConfiguration
private AbstractScheduledService metricsEmitter;

public Agent(AgentContext agentContext) {
this.logger = Logging.getLogger(Agent.class);
this.logger = LoggerFactory.getLogger(Agent.class);
this.agentContext = agentContext;
this.sendingExecutor = getSendingExecutor(agentContext);
this.checkpoints = new SQLiteFileCheckpointStore(agentContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.amazon.kinesis.streaming.agent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazon.kinesis.streaming.agent.config.AgentConfiguration;
import com.amazonaws.AmazonClientException;
Expand All @@ -23,7 +24,7 @@
import com.google.common.base.Strings;

public class AgentAWSCredentialsProvider implements AWSCredentialsProvider {
private static final Logger LOGGER = Logging.getLogger(AgentAWSCredentialsProvider.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AgentAWSCredentialsProvider.class);
private final AgentConfiguration config;

public AgentAWSCredentialsProvider(AgentConfiguration config) {
Expand Down
5 changes: 3 additions & 2 deletions src/com/amazon/kinesis/streaming/agent/AgentContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazon.kinesis.streaming.agent.config.AgentConfiguration;
import com.amazon.kinesis.streaming.agent.config.Configuration;
Expand Down Expand Up @@ -56,7 +57,7 @@
* state.
*/
public class AgentContext extends AgentConfiguration implements IMetricsContext {
private static final Logger LOGGER = Logging.getLogger(AgentContext.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AgentContext.class);

@VisibleForTesting
static final String DEFAULT_USER_AGENT = "aws-kinesis-agent";
Expand Down Expand Up @@ -114,7 +115,7 @@ public AgentContext(Configuration configuration, FileFlowFactory fileFlowFactory
*/
public String version() {
final String VERSION_INFO_FILE = "versionInfo.properties";
try (InputStream versionInfoStream = Logging.class.getResourceAsStream(VERSION_INFO_FILE)) {
try (InputStream versionInfoStream = Agent.class.getResourceAsStream(VERSION_INFO_FILE)) {
Properties versionInfo = new Properties();
versionInfo.load(versionInfoStream);
return versionInfo.getProperty("version");
Expand Down
Loading

0 comments on commit 627d226

Please sign in to comment.