JAMES-1901 Wrap EsMetric reporter and configure it
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/894f4c48 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/894f4c48 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/894f4c48 Branch: refs/heads/master Commit: 894f4c484e80cb33bf86c31d4f8872185483fdb6 Parents: 31ef821 Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Dec 30 15:22:49 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Fri Feb 3 16:42:50 2017 +0700 ---------------------------------------------------------------------- .../metrics/metrics-dropwizard/pom.xml | 4 + .../dropwizard/DropWizardMetricFactory.java | 4 + .../metrics/dropwizard/ESMetricReporter.java | 69 +++++++++++++++++ .../dropwizard/ESReporterConfiguration.java | 81 ++++++++++++++++++++ server/pom.xml | 5 ++ 5 files changed, 163 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/894f4c48/server/container/metrics/metrics-dropwizard/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-dropwizard/pom.xml b/server/container/metrics/metrics-dropwizard/pom.xml index 3d40daf..5ad3440 100644 --- a/server/container/metrics/metrics-dropwizard/pom.xml +++ b/server/container/metrics/metrics-dropwizard/pom.xml @@ -57,6 +57,10 @@ <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> </dependency> + <dependency> + <groupId>org.elasticsearch</groupId> + <artifactId>metrics-elasticsearch-reporter</artifactId> + </dependency> </dependencies> </project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/894f4c48/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java index 8c7f731..005ae34 100644 --- a/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java +++ b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java @@ -39,6 +39,10 @@ public class DropWizardMetricFactory implements MetricFactory { .build(); } + public ESMetricReporter provideEsReporter(ESReporterConfiguration esReporterConfiguration) { + return new ESMetricReporter(esReporterConfiguration, metricRegistry); + } + @Override public Metric generate(String name) { return new DropWizardMetric(metricRegistry.counter(name)); http://git-wip-us.apache.org/repos/asf/james-project/blob/894f4c48/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESMetricReporter.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESMetricReporter.java b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESMetricReporter.java new file mode 100644 index 0000000..6d782ce --- /dev/null +++ b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESMetricReporter.java @@ -0,0 +1,69 @@ +/**************************************************************** + * 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.metrics.dropwizard; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import javax.annotation.PreDestroy; + +import org.elasticsearch.metrics.ElasticsearchReporter; + +import com.codahale.metrics.MetricRegistry; +import com.google.common.base.Optional; +import com.google.common.base.Throwables; + +public class ESMetricReporter { + + private final Optional<ElasticsearchReporter> reporter; + private final ESReporterConfiguration esReporterConfiguration; + + public ESMetricReporter(ESReporterConfiguration esReporterConfiguration, MetricRegistry registry) { + this.reporter = getReporter(esReporterConfiguration, registry); + this.esReporterConfiguration = esReporterConfiguration; + } + + private Optional<ElasticsearchReporter> getReporter(ESReporterConfiguration esReporterConfiguration, MetricRegistry registry) { + if (esReporterConfiguration.isEnabled()) { + try { + return Optional.of(ElasticsearchReporter.forRegistry(registry) + .hosts(esReporterConfiguration.getHostWithPort()) + .index(esReporterConfiguration.getIndex()) + .build()); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + return Optional.absent(); + } + + public void start() { + if (reporter.isPresent()) { + reporter.get().start(esReporterConfiguration.getPeriodInSecond(), TimeUnit.SECONDS); + } + } + + @PreDestroy + public void stop() { + if (reporter.isPresent()) { + reporter.get().stop(); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/894f4c48/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESReporterConfiguration.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESReporterConfiguration.java b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESReporterConfiguration.java new file mode 100644 index 0000000..06e669f --- /dev/null +++ b/server/container/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/ESReporterConfiguration.java @@ -0,0 +1,81 @@ +/**************************************************************** + * 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.metrics.dropwizard; + +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; + +public class ESReporterConfiguration { + + public static final boolean ENABLED = true; + public static final boolean DISABLED = !ENABLED; + public static final String DEFAULT_INDEX = "james-metrics"; + public static final long DEFAULT_PERIOD_IN_SECOND = 60L; + + public static ESReporterConfiguration disabled() { + return new ESReporterConfiguration( + Optional.<String>absent(), + Optional.<Integer>absent(), + DISABLED, + Optional.<String>absent(), + Optional.<Long>absent()); + } + + public static ESReporterConfiguration enabled(String host, int port, Optional<String> index, Optional<Long> periodInSecond) { + return new ESReporterConfiguration( + Optional.of(host), + Optional.of(port), + ENABLED, + index, + periodInSecond); + } + + private final Optional<String> host; + private final Optional<Integer> port; + private final boolean enabled; + private final Optional<String> index; + private final Optional<Long> periodInSecond; + + public ESReporterConfiguration(Optional<String> host, Optional<Integer> port, boolean enabled, Optional<String> index, Optional<Long> periodInSecond) { + this.host = host; + this.port = port; + this.enabled = enabled; + this.index = index; + this.periodInSecond = periodInSecond; + } + + public String getHostWithPort() { + Preconditions.checkState(host.isPresent()); + Preconditions.checkState(port.isPresent()); + return host.get() + ":" + port.get(); + } + + public boolean isEnabled() { + return enabled; + } + + public String getIndex() { + return index.or(DEFAULT_INDEX); + } + + public long getPeriodInSecond() { + return periodInSecond.or(DEFAULT_PERIOD_IN_SECOND); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/894f4c48/server/pom.xml ---------------------------------------------------------------------- diff --git a/server/pom.xml b/server/pom.xml index ab2eca5..66fdedf 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -1561,6 +1561,11 @@ <version>${jetty.version}</version> </dependency> <dependency> + <groupId>org.elasticsearch</groupId> + <artifactId>metrics-elasticsearch-reporter</artifactId> + <version>2.2.0</version> + </dependency> + <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <version>${testcontainers-version}</version> --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org