flume-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From de...@apache.org
Subject flume git commit: FLUME-3072. Add IP address to headers in Flume Log4j appender
Date Wed, 21 Jun 2017 14:03:58 GMT
Repository: flume
Updated Branches:
  refs/heads/trunk 33d19046f -> f2996cca5


FLUME-3072. Add IP address to headers in Flume Log4j appender

Log4jAppender and LoadBalancingLog4jAppender resolve local hosts address at startup and
add it to each event's header with the key "flume.client.log4j.address".

This closes #121.

(Andras Beni via Denes Arvay)


Project: http://git-wip-us.apache.org/repos/asf/flume/repo
Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/f2996cca
Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/f2996cca
Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/f2996cca

Branch: refs/heads/trunk
Commit: f2996cca55bbb852dc10512ca3369ae7fd56c44f
Parents: 33d1904
Author: Andras Beni <andrasbeni@cloudera.com>
Authored: Wed Mar 15 06:50:12 2017 +0100
Committer: Denes Arvay <denes@apache.org>
Committed: Wed Jun 21 16:02:33 2017 +0200

----------------------------------------------------------------------
 .../LoadBalancingLog4jAppender.java             |  2 +-
 .../clients/log4jappender/Log4jAppender.java    | 23 +++++++++++++++++++-
 .../clients/log4jappender/Log4jAvroHeaders.java |  1 +
 .../TestLoadBalancingLog4jAppender.java         |  2 ++
 .../log4jappender/TestLog4jAppender.java        |  1 +
 5 files changed, 27 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flume/blob/f2996cca/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/LoadBalancingLog4jAppender.java
----------------------------------------------------------------------
diff --git a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/LoadBalancingLog4jAppender.java
b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/LoadBalancingLog4jAppender.java
index ae31916..a82330b 100644
--- a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/LoadBalancingLog4jAppender.java
+++ b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/LoadBalancingLog4jAppender.java
@@ -135,7 +135,7 @@ public class LoadBalancingLog4jAppender extends Log4jAppender {
       }
       throw new FlumeException(e);
     }
-
+    initializeClientAddress();
   }
 
   private Properties getProperties(String hosts, String selector,

http://git-wip-us.apache.org/repos/asf/flume/blob/f2996cca/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
----------------------------------------------------------------------
diff --git a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
index 117542d..46b05e9 100644
--- a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
+++ b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAppender.java
@@ -20,6 +20,8 @@ package org.apache.flume.clients.log4jappender;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Map;
@@ -78,6 +80,7 @@ public class Log4jAppender extends AppenderSkeleton {
   private long timeout = RpcClientConfigurationConstants.DEFAULT_REQUEST_TIMEOUT_MILLIS;
   private boolean avroReflectionEnabled;
   private String avroSchemaUrl;
+  private String clientAddress = "";
 
   RpcClient rpcClient = null;
 
@@ -135,7 +138,7 @@ public class Log4jAppender extends AppenderSkeleton {
     hdrs.put(Log4jAvroHeaders.LOGGER_NAME.toString(), event.getLoggerName());
     hdrs.put(Log4jAvroHeaders.TIMESTAMP.toString(),
         String.valueOf(event.timeStamp));
-
+    hdrs.put(Log4jAvroHeaders.ADDRESS.toString(), clientAddress);
     //To get the level back simply use
     //LoggerEvent.toLevel(hdrs.get(Integer.parseInt(
     //Log4jAvroHeaders.LOG_LEVEL.toString()))
@@ -316,6 +319,24 @@ public class Log4jAppender extends AppenderSkeleton {
       }
       throw e;
     }
+    initializeClientAddress();
+  }
+
+  /**
+   * Resolves local host address so it can be included in event headers.
+   * @throws FlumeException if local host address can not be resolved.
+   */
+  protected void initializeClientAddress() throws FlumeException {
+    try {
+      clientAddress = InetAddress.getLocalHost().getHostAddress();
+    } catch (UnknownHostException e) {
+      String errormsg = "Failed to resolve local host address! " + e.getMessage();
+      LogLog.error(errormsg);
+      if (unsafeMode) {
+        return;
+      }
+      throw new FlumeException(e);
+    }
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/flume/blob/f2996cca/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAvroHeaders.java
----------------------------------------------------------------------
diff --git a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAvroHeaders.java
b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAvroHeaders.java
index 22983d3..dfb6b5e 100644
--- a/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAvroHeaders.java
+++ b/flume-ng-clients/flume-ng-log4jappender/src/main/java/org/apache/flume/clients/log4jappender/Log4jAvroHeaders.java
@@ -25,6 +25,7 @@ public enum Log4jAvroHeaders {
   LOGGER_NAME("flume.client.log4j.logger.name"),
   LOG_LEVEL("flume.client.log4j.log.level"),
   MESSAGE_ENCODING("flume.client.log4j.message.encoding"),
+  ADDRESS("flume.client.log4j.address"),
   TIMESTAMP("flume.client.log4j.timestamp"),
   AVRO_SCHEMA_LITERAL("flume.avro.schema.literal"),
   AVRO_SCHEMA_URL("flume.avro.schema.url");

http://git-wip-us.apache.org/repos/asf/flume/blob/f2996cca/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLoadBalancingLog4jAppender.java
----------------------------------------------------------------------
diff --git a/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLoadBalancingLog4jAppender.java
b/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLoadBalancingLog4jAppender.java
index 53795fb..ec5d6df 100644
--- a/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLoadBalancingLog4jAppender.java
+++ b/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLoadBalancingLog4jAppender.java
@@ -254,6 +254,8 @@ public class TestLoadBalancingLog4jAppender {
 
       Assert.assertNotNull(hdrs.get(Log4jAvroHeaders.TIMESTAMP.toString()));
 
+      Assert.assertNotNull(hdrs.get(Log4jAvroHeaders.ADDRESS.toString()));
+
       Assert.assertEquals(Level.toLevel(level),
           Level.toLevel(hdrs.get(Log4jAvroHeaders.LOG_LEVEL.toString())));
 

http://git-wip-us.apache.org/repos/asf/flume/blob/f2996cca/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
----------------------------------------------------------------------
diff --git a/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
b/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
index c087b67..b8663a6 100644
--- a/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
+++ b/flume-ng-clients/flume-ng-log4jappender/src/test/java/org/apache/flume/clients/log4jappender/TestLog4jAppender.java
@@ -115,6 +115,7 @@ public class TestLog4jAppender {
           Level.toLevel(Integer.valueOf(hdrs.get(Log4jAvroHeaders.LOG_LEVEL
               .toString()))
           ));
+      Assert.assertNotNull(hdrs.get(Log4jAvroHeaders.ADDRESS.toString()));
 
       Assert.assertEquals(logger.getName(),
           hdrs.get(Log4jAvroHeaders.LOGGER_NAME.toString()));


Mime
View raw message