JAMES-1901 Enable reporting in ES for Guice/Cassandra

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

Branch: refs/heads/master
Commit: 6f38dcbb4480465ff146284f1fa4211d575dadfb
Parents: 894f4c4
Author: Benoit Tellier <[email protected]>
Authored: Fri Dec 30 16:14:02 2016 +0700
Committer: Benoit Tellier <[email protected]>
Committed: Fri Feb 3 16:42:50 2017 +0700

----------------------------------------------------------------------
 .../destination/conf/elasticsearch.properties   |   6 +
 .../apache/james/CassandraJamesServerMain.java  |   4 +-
 .../mailbox/ElasticSearchMailboxModule.java     |   9 +-
 .../modules/server/ESMetricReporterModule.java  | 117 +++++++++++++++++++
 4 files changed, 132 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/6f38dcbb/dockerfiles/run/guice/cassandra/destination/conf/elasticsearch.properties
----------------------------------------------------------------------
diff --git 
a/dockerfiles/run/guice/cassandra/destination/conf/elasticsearch.properties 
b/dockerfiles/run/guice/cassandra/destination/conf/elasticsearch.properties
index a10cd3f..06e1852 100644
--- a/dockerfiles/run/guice/cassandra/destination/conf/elasticsearch.properties
+++ b/dockerfiles/run/guice/cassandra/destination/conf/elasticsearch.properties
@@ -28,3 +28,9 @@ elasticsearch.retryConnection.maxRetries=7
 elasticsearch.retryConnection.minDelay=3000
 # Index or not attachments (default value: true)
 elasticsearch.indexAttachments=true
+
+# Reports for metrics into ElasticSearch
+elasticsearch.http.port=9200
+elasticsearch.metrics.reports.enabled=true
+elasticsearch.metrics.reports.period=30
+elasticsearch.metrics.reports.index=james-metrics
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/6f38dcbb/server/container/guice/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java
index c269263..431ea6b 100644
--- 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/CassandraJamesServerMain.java
@@ -29,6 +29,7 @@ import 
org.apache.james.modules.mailbox.CassandraSessionModule;
 import org.apache.james.modules.mailbox.ElasticSearchMailboxModule;
 import org.apache.james.modules.protocols.JMAPServerModule;
 import org.apache.james.modules.server.ActiveMQQueueModule;
+import org.apache.james.modules.server.ESMetricReporterModule;
 import org.apache.james.modules.server.JMXServerModule;
 import org.apache.james.modules.server.QuotaModule;
 
@@ -48,7 +49,8 @@ public class CassandraJamesServerMain {
         new CassandraSessionModule(),
         new ElasticSearchMailboxModule(),
         new QuotaModule(),
-        new ActiveMQQueueModule());
+        new ActiveMQQueueModule(),
+        new ESMetricReporterModule());
 
 
     public static void main(String[] args) throws Exception {

http://git-wip-us.apache.org/repos/asf/james-project/blob/6f38dcbb/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
index 15878dd..eb979a2 100644
--- 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
@@ -50,6 +50,9 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
     private static final int DEFAULT_CONNECTION_MAX_RETRIES = 7;
     private static final int DEFAULT_CONNECTION_MIN_DELAY = 3000;
     private static final boolean DEFAULT_INDEX_ATTACHMENTS = true;
+    public static final String ES_CONFIG_FILE = 
FileSystem.FILE_PROTOCOL_AND_CONF + "elasticsearch.properties";
+    public static final String ELASTICSEARCH_MASTER_HOST = 
"elasticsearch.masterHost";
+    public static final String ELASTICSEARCH_PORT = "elasticsearch.port";
 
     @Override
     protected void configure() {
@@ -64,10 +67,10 @@ public class ElasticSearchMailboxModule extends 
AbstractModule {
     @Provides
     @Singleton
     protected Client provideClientProvider(FileSystem fileSystem, 
AsyncRetryExecutor executor) throws ConfigurationException, 
FileNotFoundException, ExecutionException, InterruptedException {
-        PropertiesConfiguration propertiesReader = new 
PropertiesConfiguration(fileSystem.getFile(FileSystem.FILE_PROTOCOL_AND_CONF + 
"elasticsearch.properties"));
+        PropertiesConfiguration propertiesReader = new 
PropertiesConfiguration(fileSystem.getFile(ES_CONFIG_FILE));
 
-        ClientProvider clientProvider = new 
ClientProviderImpl(propertiesReader.getString("elasticsearch.masterHost"),
-                propertiesReader.getInt("elasticsearch.port"));
+        ClientProvider clientProvider = new 
ClientProviderImpl(propertiesReader.getString(ELASTICSEARCH_MASTER_HOST),
+                propertiesReader.getInt(ELASTICSEARCH_PORT));
         Client client = getRetryer(executor, propertiesReader)
                 .getWithRetry(ctx -> clientProvider.get()).get();
         IndexCreationFactory.createIndex(client,

http://git-wip-us.apache.org/repos/asf/james-project/blob/6f38dcbb/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java
 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java
new file mode 100644
index 0000000..fcf0eb7
--- /dev/null
+++ 
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/server/ESMetricReporterModule.java
@@ -0,0 +1,117 @@
+/****************************************************************
+ * 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.modules.server;
+
+import java.io.FileNotFoundException;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.lang.NotImplementedException;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.lifecycle.api.Configurable;
+import org.apache.james.metrics.dropwizard.DropWizardMetricFactory;
+import org.apache.james.metrics.dropwizard.ESMetricReporter;
+import org.apache.james.metrics.dropwizard.ESReporterConfiguration;
+import org.apache.james.modules.mailbox.ElasticSearchMailboxModule;
+import org.apache.james.utils.ConfigurationPerformer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import com.google.inject.Provides;
+import com.google.inject.Singleton;
+import com.google.inject.multibindings.Multibinder;
+
+public class ESMetricReporterModule extends AbstractModule {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ESMetricReporterModule.class);
+    public static final boolean DEFAULT_DISABLE = false;
+    public static final int DEFAULT_ES_HTTP_PORT = 9200;
+
+    @Override
+    protected void configure() {
+        Multibinder.newSetBinder(binder(), 
ConfigurationPerformer.class).addBinding().to(ESMetricReporterStarter.class);
+    }
+
+    @Provides
+    public ESReporterConfiguration provideConfiguration(FileSystem fileSystem) 
throws ConfigurationException {
+        try {
+            PropertiesConfiguration propertiesReader = 
getPropertiesConfiguration(fileSystem);
+
+            if (isMetricEnable(propertiesReader)) {
+                return ESReporterConfiguration.enabled(
+                    
propertiesReader.getString(ElasticSearchMailboxModule.ELASTICSEARCH_MASTER_HOST),
+                    propertiesReader.getInt("elasticsearch.http.port", 
DEFAULT_ES_HTTP_PORT),
+                    
Optional.fromNullable(propertiesReader.getString("elasticsearch.metrics.reports.index",
 null)),
+                    
Optional.fromNullable(propertiesReader.getLong("elasticsearch.metrics.reports.period",
 null)));
+            }
+        } catch (FileNotFoundException e) {
+            LOGGER.info("Can not locate " + 
ElasticSearchMailboxModule.ES_CONFIG_FILE);
+        }
+        return ESReporterConfiguration.disabled();
+    }
+
+    private boolean isMetricEnable(PropertiesConfiguration propertiesReader) {
+        return 
propertiesReader.getBoolean("elasticsearch.metrics.reports.enabled", 
DEFAULT_DISABLE);
+    }
+
+    private PropertiesConfiguration getPropertiesConfiguration(FileSystem 
fileSystem) throws ConfigurationException, FileNotFoundException {
+        return new PropertiesConfiguration(
+                    
fileSystem.getFile(ElasticSearchMailboxModule.ES_CONFIG_FILE));
+    }
+
+    @Provides
+    public ESMetricReporter provideReporter(DropWizardMetricFactory 
metricFactory, ESReporterConfiguration configuration) throws 
ConfigurationException, ExecutionException, InterruptedException {
+        return metricFactory.provideEsReporter(configuration);
+    }
+
+    @Singleton
+    public static class ESMetricReporterStarter implements 
ConfigurationPerformer, Configurable {
+
+        private final ESMetricReporter esMetricReporter;
+
+        @Inject
+        public ESMetricReporterStarter(ESMetricReporter esMetricReporter) {
+            this.esMetricReporter = esMetricReporter;
+        }
+
+        @Override
+        public void initModule() {
+            esMetricReporter.start();
+        }
+
+        @Override
+        public List<Class<? extends Configurable>> forClasses() {
+            return ImmutableList.of(ESMetricReporterStarter.class);
+        }
+
+        @Override
+        public void configure(HierarchicalConfiguration config) throws 
ConfigurationException {
+            throw new NotImplementedException();
+        }
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to