You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/08/24 03:46:33 UTC

[08/38] james-project git commit: JAMES-2114 MDC contectual logging for WEBADMIN

JAMES-2114 MDC contectual logging for WEBADMIN


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

Branch: refs/heads/master
Commit: ba02c57b9fc0be611657b21a0517f7f7042f820d
Parents: 176d7bf
Author: benwa <bt...@linagora.com>
Authored: Wed Aug 23 16:24:12 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Aug 24 10:34:42 2017 +0700

----------------------------------------------------------------------
 .../apache/james/webadmin/WebAdminServer.java   |  8 ++++
 .../james/webadmin/mdc/MDCCleanupFilter.java    | 44 +++++++++++++++++++
 .../apache/james/webadmin/mdc/MDCFilter.java    | 45 ++++++++++++++++++++
 3 files changed, 97 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/ba02c57b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
index abce524..cfd60b6 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
@@ -31,6 +31,8 @@ import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.webadmin.authentication.AuthenticationFilter;
 import org.apache.james.webadmin.authentication.NoAuthenticationFilter;
+import org.apache.james.webadmin.mdc.MDCCleanupFilter;
+import org.apache.james.webadmin.mdc.MDCFilter;
 import org.apache.james.webadmin.metric.MetricPostFilter;
 import org.apache.james.webadmin.metric.MetricPreFilter;
 import org.apache.james.webadmin.routes.CORSRoute;
@@ -81,12 +83,18 @@ public class WebAdminServer implements Configurable {
             configureCORS();
             configureMetrics();
             service.before(authenticationFilter);
+            configureMDC();
             routesList.forEach(routes -> routes.define(service));
             service.awaitInitialization();
             LOGGER.info("Web admin server started");
         }
     }
 
+    private void configureMDC() {
+        service.before(new MDCFilter());
+        service.after(new MDCCleanupFilter());
+    }
+
     private void configureMetrics() {
         service.before(new MetricPreFilter(metricFactory));
         service.after(new MetricPostFilter());

http://git-wip-us.apache.org/repos/asf/james-project/blob/ba02c57b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCCleanupFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCCleanupFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCCleanupFilter.java
new file mode 100644
index 0000000..6c8e144
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCCleanupFilter.java
@@ -0,0 +1,44 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class MDCCleanupFilter implements Filter {
+    private static final Logger LOGGER = LoggerFactory.getLogger(MDCCleanupFilter.class);
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        Object attribute = request.attribute(MDCFilter.MDC_CLOSEABLE);
+        if (attribute instanceof Closeable) {
+            Closeable closeable = (Closeable) attribute;
+            closeable.close();
+        } else {
+            LOGGER.error("Invalid MDC closeable {} of class {}", attribute, attribute.getClass());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/ba02c57b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCFilter.java b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCFilter.java
new file mode 100644
index 0000000..1c12d78
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/mdc/MDCFilter.java
@@ -0,0 +1,45 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.mdc;
+
+import java.io.Closeable;
+
+import org.apache.james.util.MDCBuilder;
+
+import spark.Filter;
+import spark.Request;
+import spark.Response;
+
+public class MDCFilter implements Filter {
+    public static final String VERB = "verb";
+    public static final String MDC_CLOSEABLE = "MDCCloseable";
+
+    @Override
+    public void handle(Request request, Response response) throws Exception {
+        Closeable mdcCloseable = MDCBuilder.create()
+            .addContext(MDCBuilder.IP, request.ip())
+            .addContext(MDCBuilder.HOST, request.host())
+            .addContext(VERB, request.requestMethod())
+            .addContext(MDCBuilder.PROTOCOL, "webadmin")
+            .addContext(MDCBuilder.ACTION, request.pathInfo())
+            .build();
+        request.attribute(MDC_CLOSEABLE, mdcCloseable);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org