JAMES-1868 Adding metrics as a Mailet I changed mailetContainer.xml for cassandra docker in order to count :
- errors of the mail pipeline - relay denied - localAddressErrors - spam - bounces Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6ce3c8ec Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6ce3c8ec Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6ce3c8ec Branch: refs/heads/master Commit: 6ce3c8ecdcb34091d34581d8cadc9f1b42c04c9a Parents: 7a9c438 Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Nov 25 09:45:51 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Nov 30 16:32:21 2016 +0700 ---------------------------------------------------------------------- .../guice/destination/conf/mailetcontainer.xml | 15 ++++ .../james/transport/mailets/MetricsMailet.java | 76 ++++++++++++++++++++ .../transport/mailets/MetricsMailetTest.java | 76 ++++++++++++++++++++ .../mailets/delivery/ToRecipientFolderTest.java | 3 +- 4 files changed, 169 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6ce3c8ec/dockerfiles/run/guice/destination/conf/mailetcontainer.xml ---------------------------------------------------------------------- diff --git a/dockerfiles/run/guice/destination/conf/mailetcontainer.xml b/dockerfiles/run/guice/destination/conf/mailetcontainer.xml index 5008caa..25d02d4 100644 --- a/dockerfiles/run/guice/destination/conf/mailetcontainer.xml +++ b/dockerfiles/run/guice/destination/conf/mailetcontainer.xml @@ -64,6 +64,9 @@ </processor> <processor state="error" enableJmx="true"> + <mailet match="All" class="MetricsMailet"> + <metricName>mailetContainerErrors</metricName> + </mailet> <mailet match="All" class="Bounce"/> <mailet match="All" class="ToRepository"> <repositoryPath>file://var/mail/error/</repositoryPath> @@ -108,12 +111,18 @@ </processor> <processor state="spam" enableJmx="true"> + <mailet match="All" class="MetricsMailet"> + <metricName>mailetContainerSpam</metricName> + </mailet> <mailet match="All" class="ToRepository"> <repositoryPath>file://var/mail/spam/</repositoryPath> </mailet> </processor> <processor state="local-address-error" enableJmx="true"> + <mailet match="All" class="MetricsMailet"> + <metricName>mailetContainerLocalAddressError</metricName> + </mailet> <mailet match="All" class="Bounce"> <attachment>none</attachment> </mailet> @@ -123,6 +132,9 @@ </processor> <processor state="relay-denied" enableJmx="true"> + <mailet match="All" class="MetricsMailet"> + <metricName>mailetContainerRelayDenied</metricName> + </mailet> <mailet match="All" class="Bounce"> <attachment>none</attachment> </mailet> @@ -133,6 +145,9 @@ </processor> <processor state="bounces" enableJmx="true"> + <mailet match="All" class="MetricsMailet"> + <metricName>bounces</metricName> + </mailet> <mailet match="All" class="DSNBounce"> <passThrough>false</passThrough> </mailet> http://git-wip-us.apache.org/repos/asf/james-project/blob/6ce3c8ec/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/MetricsMailet.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/MetricsMailet.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/MetricsMailet.java new file mode 100644 index 0000000..22f5461 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/MetricsMailet.java @@ -0,0 +1,76 @@ +/**************************************************************** + * 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.transport.mailets; + +import javax.inject.Inject; +import javax.mail.MessagingException; + +import org.apache.james.metrics.api.Metric; +import org.apache.james.metrics.api.MetricFactory; +import org.apache.mailet.Mail; +import org.apache.mailet.base.GenericMailet; + +import com.google.common.base.Preconditions; + +/** + * This Metrics mailet increments a counter on every incoming emails. + * + * This counter is accessible via JMX. + * + * Example : + * + * <mailet matcher="all" class="MetricsMailet"> + * <metricName>relayDenied</metricName> + * </mailet> + * + * Will increment a counter relay denied + */ +public class MetricsMailet extends GenericMailet { + public static final String METRIC_NAME = "metricName"; + + private final MetricFactory metricFactory; + private Metric metric; + + @Inject + public MetricsMailet(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + + @Override + public void init() throws MessagingException { + String metricName = getInitParameter(METRIC_NAME); + init(metricName); + } + + private void init(String metricName) { + Preconditions.checkNotNull(metricName); + metric = metricFactory.generate(metricName); + } + + @Override + public void service(Mail mail) throws MessagingException { + metric.increment(); + } + + @Override + public String getMailetInfo() { + return "Metrics mailet"; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6ce3c8ec/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/MetricsMailetTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/MetricsMailetTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/MetricsMailetTest.java new file mode 100644 index 0000000..29032ab --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/MetricsMailetTest.java @@ -0,0 +1,76 @@ +/**************************************************************** + * 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.transport.mailets; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import org.apache.james.metrics.api.Metric; +import org.apache.james.metrics.api.MetricFactory; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailetConfig; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MetricsMailetTest { + + public static final String MAILET_NAME = "Metric test"; + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MetricFactory metricFactory; + private Metric metric; + private MetricsMailet mailet; + + @Before + public void setUp() throws Exception { + metricFactory = mock(MetricFactory.class); + metric = mock(Metric.class); + when(metricFactory.generate(anyString())).thenReturn(metric); + + mailet = new MetricsMailet(metricFactory); + } + + @Test + public void initShouldThrowWhenMetricNameIsNotGiven() throws Exception { + expectedException.expect(NullPointerException.class); + + mailet.init(FakeMailetConfig.builder().mailetName(MAILET_NAME).build()); + } + + @Test + public void serviceShouldIncrementMetricCounter() throws Exception { + mailet.init(FakeMailetConfig.builder() + .mailetName(MAILET_NAME) + .setProperty(MetricsMailet.METRIC_NAME, "metricName") + .build()); + + mailet.service(FakeMail.builder().build()); + + verify(metric).increment(); + verifyNoMoreInteractions(metric); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/6ce3c8ec/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java index 6ae5aa2..48306f3 100644 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/ToRecipientFolderTest.java @@ -46,6 +46,7 @@ import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.transport.mailets.ToRecipientFolder; import org.apache.james.user.api.UsersRepository; import org.apache.mailet.Mail; @@ -85,7 +86,7 @@ public class ToRecipientFolderTest { user = mock(MailboxSession.User.class); - testee = new ToRecipientFolder(mailboxManager, usersRepository); + testee = new ToRecipientFolder(mailboxManager, usersRepository, mock(MetricFactory.class)); MailboxSession session = mock(MailboxSession.class); when(session.getPathDelimiter()).thenReturn('.'); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org